CDR.EXE - Open/Close CD Drive(s) Programmatically | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 24, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Downloads

Download project (Source & Executable)- 32 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.