User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

    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


    Top Authors