CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

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

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Windows Programming >> System >> CD-ROM


CDR.EXE - Open/Close CD Drive(s) Programmatically
Rating:

Chris Morse Sebrell (view profile)
November 24, 2000


(continued)



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

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Is it possible to check the door status? - comomolo (08/03/2007)
Excellent! - Ajay Vijayvargiya (01/04/2005)
Write a simple program that can read/write files from/into a CD - Legacy CodeGuru (10/06/2003)
Thanks: Lovely and simple to understand - Legacy CodeGuru (08/08/2003)
Another way to do it - Legacy CodeGuru (06/06/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES