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.

Downloads

Download project (Source & Executable)- 32 Kb

IT Offers

Comments

  • Is it possible to check the door status?

    Posted by comomolo on 08/03/2007 12:51am

    What 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.

    Reply
  • Excellent!

    Posted by Ajay Vijay on 01/04/2005 01:26am

    While browsing through articles randomly I found your article. This is the thing I was looking for long...! Thanks for the great effort!

    • How to detect the presence of CD

      Posted by kanul.chugh on 06/15/2006 08:17am

      This 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

      Reply
    Reply
  • Write a simple program that can read/write files from/into a CD

    Posted by Legacy on 10/06/2003 12:00am

    Originally posted by: James

    How to write a program that can write files to a CD?
    (Just like Nero or other CD burning software)

    • Codes in java for opening, closing and copying content from cd drive

      Posted by csmani on 11/21/2006 09:48am

      I am new to Java. I have done simple programs using Vb 6. please help me. I also want to know I have seen somany codes in vb for working with CD drive, but how to make these codes working in java.

      Reply
    • Use Adaptec ASPI layer

      Posted by sekhar77 on 03/09/2005 08:33pm

      Install 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.

      Reply
    Reply
  • Thanks: Lovely and simple to understand

    Posted by Legacy on 08/08/2003 12:00am

    Originally posted by: Nishikant Kulkarni

    Congrats. This code is very well written. Simple to understand and explains things beautifully. Served my purpose well.

    Regards,
    Nishikant.

    Reply
  • Another way to do it

    Posted by Legacy on 06/06/2003 12:00am

    Originally posted by: kakan

    #include <Winioctl.h>
    
    

    // Usage of the diskDoor function
    // Eject the cd in drive f:
    diskDoor('f', true);
    // Close the door in drive f:
    diskDoor('f', false);
    // That's it.


    bool diskDoor(char drive, bool eject)
    {
    // Open or close the CD tray.
    // drive == 'e'
    // eject == true: Eject it. eject == false: Close the door

    char Drive[MAX_PATH];
    HANDLE hDrive = INVALID_HANDLE_VALUE;
    DWORD dwErr;
    BOOL bRetVal = TRUE;
    DWORD dwDummy;

    sprintf(Drive,"\\\\.\\%c:", cdUnit);

    // Try this first.
    hDrive = CreateFile(Drive, GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, NULL);

    if(hDrive == INVALID_HANDLE_VALUE) {
    SetLastError(NO_ERROR);
    hDrive = CreateFile(Drive, GENERIC_READ,
    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, NULL);
    }

    dwErr = GetLastError();

    if(hDrive != INVALID_HANDLE_VALUE && dwErr == NO_ERROR) {
    // The drive is Open OK
    if(eject) {
    // Eject it.
    bRetVal = DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, NULL,
    0, NULL, 0, &dwDummy, NULL);
    }
    else {
    // retract it
    bRetVal = DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, NULL,
    0, NULL, 0, &dwDummy, NULL);
    }

    if(bRetVal == FALSE) {
    // Couldn't do it, error
    // Do error handling
    bRetVal = FALSE;
    }
    }
    else {
    // Error vid �ppningen
    bRetVal = FALSE;
    }

    if(hDrive != INVALID_HANDLE_VALUE) CloseHandle(hDrive);
    return bRetVal;
    }

    • Administrative Privilege

      Posted by nAlam on 03/14/2006 03:59am

      is it possible to do so without having administrative privilege?

      Reply
    Reply
  • it's very funny if u add this in a trojan and send to a friend

    Posted by Legacy on 06/02/2003 12:00am

    Originally posted by: cnk

    it's exactly what I was looking for some time..
    thanks...

    Reply
  • how to prevent copying files from a CDROM

    Posted by Legacy on 05/23/2003 12:00am

    Originally posted by: Naveen

    How can I prevent copying files from a CDROM to a harddisk??

    Reply
  • CDR command not recognised in XP

    Posted by Legacy on 11/03/2002 12:00am

    Originally posted by: Missingpresumedgone

    Is there an alternate command that will do this instead????

    Reply
  • Shell Integration

    Posted by Legacy on 09/29/2002 12:00am

    Originally 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

    Reply
  • how to play

    Posted by Legacy on 09/21/2002 12:00am

    Originally posted by: jayashree

    u had made open/close very easy,
    now how do i play/stop a CD

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Whitepapers and More

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds