Click here to Skip to main content
Licence 
First Posted 28 Sep 2005
Views 101,891
Bookmarked 22 times

Open or Close CD-ROM drive

By | 28 Sep 2005 | Article
Eject or close specified CD-ROM or DVD-ROM in the system.

Introduction

The mciSendCommand function is very useful to handle CD-ROM drives. You can specify which drive you want to eject or close.

Code explanation

For a device initialization you have to use the mciSendCommand function with the MCI_OPEN command. The MCI_OPEN_TYPE flag must be used whenever a device is specified in the mciSendCommand function. If you open a device by specifying a device-type constant, you must specify the MCI_OPEN_TYPE_ID flag in addition to MCI_OPEN_TYPE.

The MCI_OPEN_PARMS structure contains information for the MCI_OPEN command. We will use the lpstrDeviceType and lpstrElementName members. Devices with extended command sets replace this structure with a device-specific structure so we will get the wDeviceID member.

  • lpstrDeviceType

    Name or constant identifier of the device type. If this member is a constant, it can be one of the values listed in MCI Device Types.

  • lpstrElementName

    Device element (often a path, e.g. "E:").

  • wDeviceID

    Identifier returned to the application.

To know the reason why I use the ZeroMemory function, please read this article.

After receiving the wDeviceID member we are able to send the commands (MCI_SET_DOOR_CLOSED or MCI_SET_DOOR_OPEN) to the specific CD-ROM device directly using the MCI_SET command.

The MCI_CLOSE command releases access to a device.

void CDRomOpen(BOOL bOpenDrive, char *drive)
{ 
    MCI_OPEN_PARMS open;
    DWORD flags;

    ZeroMemory(&open, sizeof(MCI_OPEN_PARMS));

    open.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
    open.lpstrElementName = drive;

    flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID;

    if (!mciSendCommand(0, MCI_OPEN, flags, (DWORD) &open)) 
    {
        mciSendCommand(open.wDeviceID, MCI_SET, (bOpenDrive) ? 
                       MCI_SET_DOOR_OPEN : MCI_SET_DOOR_CLOSED, 0);
        mciSendCommand(open.wDeviceID, MCI_CLOSE, MCI_WAIT, 0);
    }
}

Points of Interest

Also I suggest you examine the whole source code. There is a cute example of how to parse a command line and how to use the application parameters.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Dalibor Drzik

Web Developer

Slovakia Slovakia

Member

Wink | ;-)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberIrineK10:58 21 Mar '11  
GeneralDLLImport C# Version PinmemberReelix1:45 19 Aug '08  
QuestionI have a error when I debug program open/close CD ROM Pinmemberqv198517:30 6 Dec '07  
NewsCorrection: free allocated memory PinmemberDalibor Drzik3:07 22 Dec '06  
QuestionCompile error Pinmember3dOptics15:46 21 Dec '06  
AnswerRe: Compile error PinmemberDalibor Drzik2:47 22 Dec '06  
GeneralDoesn't work with multiple drives Pinmembersfdbgulkierwhgoierhso8gihloiwghiloe5:12 22 Sep '06  
GeneralRe: Doesn't work with multiple drives PinmemberWilliam C Bonner7:57 24 Jun '07  
GeneralRe: Doesn't work with multiple drives PinmemberWilliam C Bonner8:20 24 Jun '07  
GeneralSuggestion PinmemberFernando A. Gómez F.12:19 13 Jun '06  
GeneralThank you! Pinmemberkenny_helsens4:13 18 May '06  
GeneralThank you! It's brilliant! Pinmemberchipko3k20:13 28 Oct '05  
GeneralRe: Thank you! It's brilliant! PinmemberDalibor Drzik2:21 29 Oct '05  
Generaldoesn't work on W2K3 Pinmemberuhoraho4:11 27 Oct '05  
GeneralRe: doesn't work on W2K3 PinmemberDalibor Drzik6:44 29 Oct '05  
Generaldoesn't work Pinmemberbruno cortona11:03 26 Oct '05  
GeneralRe: doesn't work PinmemberJordanwb14:41 15 Apr '07  
QuestionHow To check the CD Writter,Printer, Download Size Status such as How Copies writted and printed PinsussAnonymous5:59 5 Oct '05  
Generaldemo PinsussAnonymous9:02 28 Sep '05  
GeneralRe: demo PinsussAnonymous10:32 28 Sep '05  
GeneralRe: demo PinmemberDalibor Drzik0:56 30 Sep '05  
GeneralRe: demo PinmemberT1TAN4:51 4 Oct '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 28 Sep 2005
Article Copyright 2005 by Dalibor Drzik
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid