CDR.EXE - Open/Close CD Drive(s) Programmatically
In Windows Explorer, you can right-click on a CD-Drive and select the "Eject" option to open the CD Drive. Unfortunately, there is no "Close" equivelant to Close the drive. This project builds a command-line program that lets you open or close any or all of your CD-Drives.
To display the program's usage, run the program with no parameters:
C:\> CDR CDR by Chris M. Sebrell usage: CDR [open|close] [Drive-Letter|ALL] example: CDR open E: CDR close ALL C:\>
To OPEN or CLOSE the first logical CD Drive:
C:\> CDR open C:\> CDR close
If you have more than one CD Drive, you can specify a drive letter:
C:\> CDR open g: C:\> CDR close g:
If you have more than one CD Drive, you can specify ALL drives:
C:\> CDR open all C:\> CDR close all
The main work routine in the program is CD_OpenClose(BOOL bOpen, TCHAR cDrive)
//Open or Close CD Drive
//cDrive is Drive Letter to Open, or 0x01 for 'Default' drive
//Examples:
//CD_OpenCloseDrive(TRUE, 'G'); //Open CD Door for Drive G:
//CD_OpenCloseDrive(FALSE, 'G'); //Close CD Door for Drive G:
//CD_OpenCloseDrive(TRUE, 1); //Open First Logical CD Door
void CD_OpenCloseDrive(BOOL bOpenDrive, TCHAR cDrive)
{
MCI_OPEN_PARMS op;
MCI_STATUS_PARMS st;
DWORD flags;
TCHAR szDriveName[4];
strcpy(szDriveName, "X:");
::ZeroMemory(&op, sizeof(MCI_OPEN_PARMS));
op.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
if(cDrive > 1)
{
szDriveName[0] = cDrive;
op.lpstrElementName = szDriveName;
flags = MCI_OPEN_TYPE
| MCI_OPEN_TYPE_ID
| MCI_OPEN_ELEMENT
| MCI_OPEN_SHAREABLE;
}
else flags = MCI_OPEN_TYPE
| MCI_OPEN_TYPE_ID
| MCI_OPEN_SHAREABLE;
if (!mciSendCommand(0,MCI_OPEN,flags,(unsigned long)&op))
{
st.dwItem = MCI_STATUS_READY;
if(bOpenDrive)
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);
else
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);
mciSendCommand(op.wDeviceID,MCI_CLOSE,MCI_WAIT,0);
}
}
Next, to facilitate operating on ALL CD Drive Doors, add this routine (which calls the CD_OpenCloseDrive() function above):
void CD_OpenCloseAllDrives(BOOL bOpenDrives)
{
// Determine All CD Drives and Open (or Close) each one
int nPos = 0;
UINT nCount = 0;
TCHAR szDrive[4];
strcpy(szDrive, "?:\\");
DWORD dwDriveList = ::GetLogicalDrives ();
while (dwDriveList) {
if (dwDriveList & 1)
{
szDrive[0] = 0x41 + nPos;
if(::GetDriveType(szDrive) == DRIVE_CDROM)
CD_OpenCloseDrive(bOpenDrives, szDrive[0]);
}
dwDriveList >>= 1;
nPos++;
}
}
That's all! The download includes source code & the compiled CDR.EXE program. If anyone has additions, corrections, or a whole new approach to this, I'd love to hear about it. I'll update this project with any new useful information I receive.

Comments
Is it possible to check the door status?
Posted by comomolo on 08/03/2007 12:51amWhat I want to get is this simple command: if the door is open, close it; if the door is closed, open it. Is that possible at all? Thanks for any help.
ReplyExcellent!
Posted by Ajay Vijay on 01/04/2005 01:26amWhile browsing through articles randomly I found your article. This is the thing I was looking for long...! Thanks for the great effort!
-
ReplyHow to detect the presence of CD
Posted by kanul.chugh on 06/15/2006 08:17amThis thread is what i was searching for. I also want to detect the presence of CD in CD drive.So if any one is having any information about it plz share. Regards, Kanul
ReplyWrite a simple program that can read/write files from/into a CD
Posted by Legacy on 10/06/2003 12:00amOriginally posted by: James
How to write a program that can write files to a CD?
(Just like Nero or other CD burning software)
-
Reply
-
ReplyCodes in java for opening, closing and copying content from cd drive
Posted by csmani on 11/21/2006 09:48amUse Adaptec ASPI layer
Posted by sekhar77 on 03/09/2005 08:33pmInstall Adaptec ASPI layer. Use ASPI driver calls to send commands to device. It is possible to write data to CD-R CD-R/W etc. The total code is a bit complex. ASPI help document is available in internet.
ReplyThanks: Lovely and simple to understand
Posted by Legacy on 08/08/2003 12:00amOriginally posted by: Nishikant Kulkarni
Congrats. This code is very well written. Simple to understand and explains things beautifully. Served my purpose well.
Regards,
ReplyNishikant.
Another way to do it
Posted by Legacy on 06/06/2003 12:00amOriginally posted by: kakan
-
ReplyAdministrative Privilege
Posted by nAlam on 03/14/2006 03:59amis it possible to do so without having administrative privilege?
Replyit's very funny if u add this in a trojan and send to a friend
Posted by Legacy on 06/02/2003 12:00amOriginally posted by: cnk
it's exactly what I was looking for some time..
Replythanks...
how to prevent copying files from a CDROM
Posted by Legacy on 05/23/2003 12:00amOriginally posted by: Naveen
How can I prevent copying files from a CDROM to a harddisk??
ReplyCDR command not recognised in XP
Posted by Legacy on 11/03/2002 12:00amOriginally posted by: Missingpresumedgone
Is there an alternate command that will do this instead????
Reply
Shell Integration
Posted by Legacy on 09/29/2002 12:00amOriginally posted by: Avi Goldberg
Hello
Doe's anybody have any idea how to integrate this code into the shell ??
What I meen is that I want to right click the CD in MyComputer and have a Close option just like the built in Eject option in the contaxt menu, just like the auther ment.
Thanks
Replyhow to play
Posted by Legacy on 09/21/2002 12:00amOriginally posted by: jayashree
u had made open/close very easy,
Replynow how do i play/stop a CD
Loading, Please Wait ...