Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C++
Article

Open or Close CD-ROM drive

Rate me:
Please Sign up or sign in to vote.
3.97/5 (17 votes)
28 Sep 20051 min read 151.1K   3.4K   23   22
Eject or close specified CD-ROM or DVD-ROM in the system.

Image 1

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


Written By
Web Developer
Slovakia Slovakia
Wink | ;-)

Comments and Discussions

 
GeneralMy vote of 5 Pin
IrineK21-Mar-11 10:58
IrineK21-Mar-11 10:58 
This does work!!!
GeneralDLLImport C# Version Pin
Reelix19-Aug-08 1:45
Reelix19-Aug-08 1:45 
QuestionI have a error when I debug program open/close CD ROM Pin
qv19856-Dec-07 17:30
qv19856-Dec-07 17:30 
NewsCorrection: free allocated memory Pin
Dalibor Drzik22-Dec-06 3:07
Dalibor Drzik22-Dec-06 3:07 
QuestionCompile error Pin
3dOptics21-Dec-06 15:46
3dOptics21-Dec-06 15:46 
AnswerRe: Compile error Pin
Dalibor Drzik22-Dec-06 2:47
Dalibor Drzik22-Dec-06 2:47 
GeneralDoesn't work with multiple drives Pin
sfdbgulkierwhgoierhso8gihloiwghiloe22-Sep-06 5:12
sfdbgulkierwhgoierhso8gihloiwghiloe22-Sep-06 5:12 
GeneralRe: Doesn't work with multiple drives Pin
William C Bonner24-Jun-07 7:57
William C Bonner24-Jun-07 7:57 
GeneralRe: Doesn't work with multiple drives Pin
William C Bonner24-Jun-07 8:20
William C Bonner24-Jun-07 8:20 
GeneralSuggestion Pin
Fernando A. Gomez F.13-Jun-06 12:19
Fernando A. Gomez F.13-Jun-06 12:19 
GeneralThank you! Pin
kenny_helsens18-May-06 4:13
kenny_helsens18-May-06 4:13 
GeneralThank you! It's brilliant! Pin
chipko3k28-Oct-05 20:13
chipko3k28-Oct-05 20:13 
GeneralRe: Thank you! It's brilliant! Pin
Dalibor Drzik29-Oct-05 2:21
Dalibor Drzik29-Oct-05 2:21 
Generaldoesn't work on W2K3 Pin
uhoraho27-Oct-05 4:11
uhoraho27-Oct-05 4:11 
GeneralRe: doesn't work on W2K3 Pin
Dalibor Drzik29-Oct-05 6:44
Dalibor Drzik29-Oct-05 6:44 
Generaldoesn't work Pin
bruno cortona26-Oct-05 11:03
bruno cortona26-Oct-05 11:03 
GeneralRe: doesn't work Pin
Jordanwb15-Apr-07 14:41
Jordanwb15-Apr-07 14:41 
QuestionHow To check the CD Writter,Printer, Download Size Status such as How Copies writted and printed Pin
Anonymous5-Oct-05 5:59
Anonymous5-Oct-05 5:59 
Generaldemo Pin
Anonymous28-Sep-05 9:02
Anonymous28-Sep-05 9:02 
GeneralRe: demo Pin
Anonymous28-Sep-05 10:32
Anonymous28-Sep-05 10:32 
GeneralRe: demo Pin
Dalibor Drzik30-Sep-05 0:56
Dalibor Drzik30-Sep-05 0:56 
GeneralRe: demo Pin
T1TAN4-Oct-05 4:51
T1TAN4-Oct-05 4:51 

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

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