How To Open and Close CD/DVD Drives Via Software
Environment: VS6, Win9x, NT, 2000, XP, ME
This project was created for use as a testbed of what methods to use while inserting and ejecting a CD via software. Through trial and error, along with quite a bit of help from CodeGuru, we managed to develop this little app and decided to share it with others.
NOTE: This program has been tested on Win9x, NT4, 2000, XP, ME, but there is no guarantee it will work for you. I am not a professional, and as an amateur, chances are that the application has errors. I've done the best I could to weed them out, but I am definitely open to any and all suggestions. Please e-mail me if you have problems with the program, as I would love to get it running correctly.
The program is designed to be used as follows:
Enter a CD/DVD drive letter in the textbox. Select a command to perform (either insert or eject). Click the Go button and voila! The drive magically does just what it is commanded to do. If there is a CD in the tray when the drive is inserted, a check box will appear in the "CD Present" checkbox. If there is no CD available, the box remains unchecked. Currently, the app does not check for a CD when the program is started.
On systems where there is only one CD/DVD drive, no letter needs to be entered. One can just select an action and then click go. For multi-drive systems, a particular letter can be specified, and the appropriate drive will respond.
Note that there is very little error checking involved. Eg: if an invalid drive letter is specified, there is no response from the program; it will just wait for your next entry.
Also included in the program are some samples of how to create functional colored hyperlinks quickly and easily. For more details on that subject, take a look at this post on CodeGuru by us.
Here are a few snippets of code:
BOOL CCdejectDlg::OnDeviceChange( UINT nEventType, DWORD dwData )
{
if (nEventType == DBT_DEVICEARRIVAL)
{
m_Present = TRUE;
UpdateData (FALSE);
MCIDEVICEID mciID;
mciID = mciGetDeviceID (CD_AUDIO);
}
return TRUE;
}
if (!mciSendCommand(0,MCI_OPEN,flags,(unsigned long)&op))
{
st.dwItem = MCI_STATUS_READY;
if(bOpenDrive) // decides whether to open or close
{
// open drive
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);
}
else
{
// close drive
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);
}
// release access to the device
mciSendCommand(op.wDeviceID,MCI_CLOSE,MCI_WAIT,0);
}
Downloads
Download demo project - 21 KbDownload source - 9 Kb

Comments
How to know the CD door is currently Open or Closed.
Posted by nangluc on 06/15/2005 02:37amI want to know if the CD door is currently Open or Closed. How to do that?? Thank you in advance! --------------- Vu Nang Luc
ReplyCD ROM Access problem.Very Urgent.
Posted by Legacy on 04/01/2003 12:00amOriginally posted by: Amandeep
I wan the cd user to be unable to browse the contents of a CD-ROM from 'My Computer' or anywhere else.I have a .exe file that verifies the Login ID and password from the user.
I want this Login exe to be executed when someone tries to browse the CD-ROM.
Another alternative that will help me out is to force 'Autorun' for the CD.The user should not be able to bypass the Autorun with the Shift Keys.
Or maybe the CD-ROM gets ejected when the user tries to explore the CD ROM or tries to bypass the Autorun.
Any of the above solutions will help me a lot.
Very Urgent.
ReplyThanks.
How to lock the CD tray
Posted by Legacy on 11/08/2002 12:00amOriginally posted by: Rolf Kary-Ehlers
How can I lock the CD-Rom tray during read or (write)operations. So that you can not open the CD tray if you push the button on the CD-player.
Replyhyperlink in About Dialog
Posted by Legacy on 09/07/2002 12:00amOriginally posted by: Davinder Pal Singh
Regards to Travis Thompson and Shane Metzler for i have used their code regarding hyperlinking in my project successfully.
Any one who wishes to add hyperlinks in the about dialog of their application can contact me through e-mail.
Regards
Davinder.
-
ReplyInterested in your hyperlink code
Posted by jimmydinh on 12/18/2007 02:23amHi Davinder, I'm interested in your hyperlink code. Please be kind to send your code to me at phicong@gmail.com Thanks.
ReplyOpening drives using other platforms
Posted by Legacy on 06/06/2002 12:00amOriginally posted by: dwiz
Just wondering, is there any use using the mci commands if you're trying to write portable code? I know the MCI is well established under Windows, but what about say Linux or Mac OS?
Cheers
Replydwiz
programa CDs
Posted by Legacy on 05/06/2002 12:00amOriginally posted by: Nestor Novoa
ReplyIt's Showing the "CD Present Check Box 'Selected ' even if there is no CD inside"
Posted by Legacy on 05/06/2002 12:00amOriginally posted by: Fazil
Hi...
This is really a good application that u have developed..
When I tried to run this App on my PC It's Showing the "CD Present Check Box 'Selected ' Even if there is no CD inside"
Any way..
Thanks lot for posting it.
Fazil
7th of May
ReplyKool Stuff
Posted by Legacy on 05/05/2002 12:00amOriginally posted by: Shuja
Hi, this is really some kool stuff you did. I have a question, how can i check the status of CDRom tray that its open or closed? I want to open and close cdrom tray from a single button by checking its status.
your article was really useful for me.
thanks again.
Shuja
ReplyThanks!
Posted by Legacy on 05/04/2002 12:00amOriginally posted by: Travis and Shane
Thanks for your comments everyone. We really appreciate the feedback from CodeGuru, and wouldn't have been able to do our app without the site! We will look into DeviceIOControl and see what we can come up with.
Thanks again!
/Trav
ReplyOpen/Close CD Drives from a Remote Machine.
Posted by Legacy on 05/03/2002 12:00amOriginally posted by: Babu Aboobacker
Hi Travis and Shane,
This code is very useful and nice...
Do you have any idea about how we can control a CD drive ,or anyother devices, from a remote machine ?
Thanx and Regards
Babu Aboobacker E.I
Reply