mciSendCommand function is very useful to handle CD-ROM drives. You can specify which drive you want to eject or close.
For a device initialization you have to use the
mciSendCommand function with the
MCI_OPEN command. The
MCI_OPEN_TYPE flag must be used whenever a device is specified in the
mciSendCommand function. If you open a device by specifying a device-type constant, you must specify the
MCI_OPEN_TYPE_ID flag in addition to
MCI_OPEN_PARMS structure contains information for the
MCI_OPEN command. We will use the
lpstrElementName members. Devices with extended command sets replace this structure with a device-specific structure so we will get the
Name or constant identifier of the device type. If this member is a constant, it can be one of the values listed in MCI Device Types.
Device element (often a path, e.g. "E:").
Identifier returned to the application.
To know the reason why I use the
ZeroMemory function, please read this article.
After receiving the
wDeviceID member we are able to send the commands (
MCI_SET_DOOR_OPEN) to the specific CD-ROM device directly using the
MCI_CLOSE command releases access to a device.
void CDRomOpen(BOOL bOpenDrive, char *drive)
open.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
open.lpstrElementName = drive;
flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID;
if (!mciSendCommand(0, MCI_OPEN, flags, (DWORD) &open))
mciSendCommand(open.wDeviceID, MCI_SET, (bOpenDrive) ?
MCI_SET_DOOR_OPEN : MCI_SET_DOOR_CLOSED, 0);
mciSendCommand(open.wDeviceID, MCI_CLOSE, MCI_WAIT, 0);
Points of Interest
Also I suggest you examine the whole source code. There is a cute example of how to parse a command line and how to use the application parameters.