CD Drive

 

a.c
#include <windows.h>
#include <mmsystem.h>
MCI_OPEN_PARMS op;
int main()
{
op.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
mciSendCommand (0, MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID,(unsigned long)&op);
printf("%d\n",op.wDeviceID);
mciSendCommand(op.wDeviceID,MCI_SET, MCI_SET_DOOR_OPEN ,0);
getch();
mciSendCommand(op.wDeviceID,MCI_SET, MCI_SET_DOOR_CLOSED ,0);
}
 
cl a.c winmm.lib

When we run the above program, it simply opens the CD drive door for us. When we press any key, the CD drive door closes on us by magic.  The mci or Media Control Interface provides us with a standard way of interacting with any multimedia device. Some of these devices include waveform and CD audio devices, MIDI sequencers  and digital-video (video playback) devices.

We first include the header file windows.h and then mmsystem.h. This file contains all the macros we need when we work with multimedia under windows. We use just one function mciSendCommand to communicate with the CD drive or more specifically any mci device. An mci device is any device that plays multimedia.

The first parameter is the mci device id which we are not aware of as yet and  thus we pass zero. The second parameter is a mci command that we want executed and believe me there are a large number. The MCI_OPEN command first initializes the device for us before we can use it. The flags MCI_OPEN_TYPE and ID tells us the system that the last parameter is a pointer to a structure of type MCI_OPEN_PARMS and the lpstrDeviceType member contains the device type constant or name. Each unique device has a know constant and the value of MCI_DEVTYPE_CD_AUDIO is 516 and for a VCR it is 513.

At the end of this device the wDeviceID member of the op structure contains the value of 1 which we use in subsequent calls to the mciSendCommand function. Now that we have the device ID we would now like to programmatically open and close the drive door. We use the same mciSendCommand with the device Id as 1 and not 0.

We then use the MCI_SET mci command to set device information. The macro MCI_SET_DOOR_OPEN has  a value of 0x00000100. This simple act of ours opens the CD drive door and a value MCI_SET_DOOR_CLOSED or 0x00000200 closes the drive door when we press an key. The file mmsystem.h contains a list of all the macros we can use and there are over a million options.

We have to first open the device and then only are we allowed to send mci commands to it.

Back to the main page