Opening and closing the coffee holder

by Mark G. Wiseman

You may have heard the story about a computer user who calls tech support and says that the holder for his coffee mug has broken. When the dismayed technician questions this, the user explains that he just pushes the button on the front of his computer and the coffee mug holder slides out. When he’s finished, pushing the button causes the holder to retract back into the computer.

While I wouldn’t recommend using your CD-ROM drawer has a coffee mug holder, I will tell you how to open and close the CD-ROM drive through software.

 

Open sesame

Unlike Ali Baba, you don’t need to know magic words to control the CD-ROM door. All you need to know is one of the functions in the Windows Media Control Interface (MCI). This function, mciSendString(), is declared in the mmsystem.h header file.

The simplest way to open the CD-ROM door is to use mciSendString() like this:

 

mciSendString("set cdaudio door open", 0, 0, 0);

 

The first argument to this function, a command string, is the only argument you need to worry about. A minor change to the command string will close the door:

 

mciSendString("set cdaudio door closed", 0, 0, 0);

 

If your computer has only one CD-ROM drive, these two function calls are all you need to know.

However, my computer has both a DVD drive and a CD-RW drive. The first of these two drives will open and close with the function calls above, but the second will not.

 

Open sesame, please

With just a little more effort, you can open and close any CD-ROM-type drive on a computer.

Using the MCI, you open an interface to the CD-ROM drive in question and give it an alias of “cd”. Then you send the command string to open or close the door. The final step is to close the interface to the drive. The code below will open the door on the F drive:

 

mciSendString("open f: type cdaudio alias cd", 0, 0, 0);

mciSendString("set cd door open", 0, 0, 0);

mciSendString("close cd", 0, 0, 0);

 

Use this code to close the door on the F drive:

 

mciSendString("open f: type cdaudio alias cd", 0, 0, 0);

mciSendString("set cd door closed", 0, 0, 0);

mciSendString("close cd", 0, 0, 0);

 

Which drives?

If you are wondering how to determine if Windows recognizes a drive as a CD-ROM drive, the code below will populate a combo box, named DriveCombo, with a list of CD-ROM drives:

 

DWORD ld = GetLogicalDrives();
for (int i = 0; i < 26; i++)
{
  if ((ld & (1 << i)) != 0)
  {
    String drv = 
      String(char('A' + i)) + ":";
    if (GetDriveType(drv.c_str()) 
        == DRIVE_CDROM) 
      DriveCombo->Items->Add(drv);
  }
}

 

Conclusion

Don’t drink too much coffee, don’t set your mug on the CD-ROM door, and don’t forget to check the example program on the Bridges Publishing Web site. Close sesame.