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

  • wholesale jerseys from china

    Posted by Adarotupirrit on 05/06/2013 03:06pm

    NFL coaches and teams usually are mandating cost regarding time of year NFL lottery tickets. You will find very little sports adventures getting trialled now. Thus in theory enthusiasts haven't so much shed anything. Nevertheless using the NFLPA decertifying [url=http://wholesaleauthenticjerseysus.weebly.com/]wholesale authentic jerseys[/url] as a union and the owners now in court, facing potential antitrust violations, it's safe to assume that the April 28-30 draft and free agency will be affected in some way.Cheap Texas Longhorns -Jerseys are actually snowed under utilizing autograph demands.With fans going crazy over the National Football Teams of Texas Longhorns, the Texas Longhorns jerseys are in high [url=http://wholesaleauthenticjerseysus.weebly.com/]http://wholesaleauthenticjerseysus.weebly.com/[/url] demand everywhere. We sale high quality, Fans wear design jerseys online as a statement about which team they root for, and often get their favorite player's name on the back. Exactly what will probably end in losing the actual 2011 time of year? The leading situation with CBA talks features, and definately will possibly be, the actual fraction of NFL income to help battler incomes. Different big considerations comprise NFL [url=http://wholesaleauthenticnflt-shirts.webs.com/]wholesale authentic nfl T-Shirts [/url] rookie salaries and the legal inability of franchises to recoup signing bonuses from players who breach contracts or refuse to perform, NFL Game day live. In such contentious negotiations, it [url=http://wholesaleauthenticnflt-shirts.webs.com/]http://wholesaleauthenticnflt-shirts.webs.com/[/url] is very difficult, and sometimes impossible, to determine which side has a better case.As professional sports activities player fans, usually you will buy some purchase NFL authentic jerseys at wholesale prices, even use it within the exact same color selection, style because you're chosen NFL team.

    Reply
  • authentic nhl jerseys,thailand russia jersey

    Posted by enarvenow on 05/06/2013 12:19pm

    Messi once again saving the Barcelona micro-blog, they made it simpler for Barcelona triumph by simply scoring a couple of goals, and minimize the Barcelona attributable to the Winners Little league will be ill at ease outside frustration. The match up against Bettis, Barcelona stayed inadequate. Prior to the Messi play intended for Barcelona, Jesse Villa is going to be flattened the fico score only two -- only two, as well as the Argentina superstar along with his undisputed level, assist Barcelona secured the triumph. Juventus sits inside thirty-fifth Serie The League's 1-0 triumph on the Palermo, still left from the Little league three ahead of a couple of match up Key west 14 factors, ahead of defending achievements. [url=http://shirts-jerseys.bravesites.com/]cheap soccer shirts[/url] Prior to the referee blew the whistle, Juventus discipline Conti perhaps a bit stressed, continued the sidelines complain the reason why the referee failed to whack the whistle, needless to say the distance therefore in close proximity to Juventus discipline trophy, the spirits will be easy to understand, thinking that the seem implies that if you Wen ended up being ahead of defending the whistle blew, the main the judge right away converted into the seashore connected with enjoyment. The holders connected with Juventus devotees in addition played a large photo undoubtedly geared up 31 caps, the middle stadium in addition taken care of the endorsed team get ready to the substantial 31 crown great, devotees will be poured to the stadium while using workforce to be able to celebrate the name, Juventus online players could be to turn out to be the concentrate, base along with Lini, than alternative Juventus online players have 31 crown commemorative greeting card carnival, Captain Buffon in addition put on 31 crown commemorative T-shirt, devotees along with personnel include hurried onward to be able to larg along with kiss along with Buffon, at this point the stadium has been Carnival devotees "occupied", [url=http://shirts-jerseys.bravesites.com/]cheap soccer jerseys[/url] content we were holding keeping a variety of commemorative banners, the scene to be able to celebrate snap shots, keep for a long time aggregation not necessarily inside the center of the judge, as well as the persons from the holders is usually without any move forward through leaving behind, celebrate a further triumph. On the other hand, Juventus online players but not an excessive amount of to remain on the review, yet they're just from the locker space began his carnival, wine wanton spray shower space into madness, Juventus online players perhaps wearing shorts bare-skinned torso into battle, perhaps Conti ingest. After that from the locker space, Juve and will discipline Kong Diba simply briefs, along with put that, specifically to the swimming pool.

    Reply
  • wholesale jerseys from china

    Posted by alterrext on 05/04/2013 06:25am

    At wholesale prices NFL jerseys offer the danger in making some sort of contrast regarding jerseys. All of us just review identical basketball jerseys cost. Enough sites possibly be addicted to getting contrast at numerous [url=http://wholesaleauthenticjerseysus.weebly.com/]wholesale jerseys cheap[/url] goods and services, however it is actually easy to make errors. On the contrary, this unusual website be absorbed in owning unity that is World Cup wholesale jerseys. You not only can require related messages on the accessible retailers that supply with the jerseys, but also be able to show difference of the price and carriage chooses. The visitors are available [url=http://wholesaleauthenticjerseysus.weebly.com/]http://wholesaleauthenticjerseysus.weebly.com/[/url] to get the most excellent cost for all special article through comparing the entire probable selectionsin a nearby position, rather than by seeking a range of websites. Like this, you save both time and money a lot. It is so helpful for each client. Related to the development of nfl jerseys with China, China financial system features getting escalating speedily as well as China turn as a appreciable probable industry coming from 1980. Whichever a product or even some sort of preparing [url=http://wholesaleauthenticnflt-shirts.webs.com/]wholesale authentic nfl T-Shirts [/url], an even the space industry, Made In China can be found here and there. China ,which is the heart of a number of products and much cut-throat competition, constantly benefit people of ppurchasing from it.In the past 30 years, Chinese economy is on the increase at the top of speed. China is the third largest in economy, while the U.S. and Japan on 1st [url=http://wholesaleauthenticnflt-shirts.webs.com/]http://wholesaleauthenticnflt-shirts.webs.com/[/url] and 2nd correspondingly. Together with the boosting economy, China is the major trading nation and exporter of products in the globle at present. Thus, lots of people are interested in trading in China.

    Reply
  • More concessions with herveleger, more take napping!

    Posted by tradecomewcp on 03/24/2013 01:56pm

    cheap herve leger dress white herve leger dress herve leger replica dresses replica herve leger dress herve leger stores herve leger outlet herve leger cheap iphone 4s on sale iphone 4g for sale iphones on sale

    Reply
  • 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
  • Loading, Please Wait ...

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

Go Deeper

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds