Click here to Skip to main content
15,177,535 members
Articles / Desktop Programming / MFC
Posted 27 Mar 2007


32 bookmarked

Open or Close a CD/DVD Drive Drawer

Rate me:
Please Sign up or sign in to vote.
4.83/5 (14 votes)
27 Mar 20071 min read
Provides a sample project with code to open and/or close a CD or DVD drive drawer.

Screenshot - CDOpen1.jpg


This project demonstrates how to open and close a CD or DVD drive drawer. It also works on the new Blu-ray drive I got recently.


I needed a way to open and/or close a drive drawer without regard to whether or not the drawer was already open or closed. This sounds easy, but most of the ways of doing this I tried (like using the MCI library) and I had problems determining whether or not the drawer was open or closed. Using this code, I just tell it what I want it to do. If it's already open, it stays open; if it's closed, it stays closed.

Using the code

There is nothing fancy in this code. Users can simply cut and paste the code needed from this sample into their own projects. This uses a fairly simple IOCTL call. Note: the function returns whether or not the call worked, but this is not necessarily the result the caller intended. For example, most notebook computers will report that a drive is closed, but the drive must be closed manually. Programmers can get around this by detecting that the disc is not ready in the drive.

// Opens or closes the CD or DVD drive specified in drive letter.
bool COpenCloseCDDlg::OpenCloseTray(bool bOpen, TCHAR cDrive)
    // Open the device (drive) that we want to affect
    CString cs;
    HANDLE hDrive = CreateFile(cs, GENERIC_READ, FILE_SHARE_READ, 
                               NULL, OPEN_EXISTING, 

    // Make sure the device was found and opened successfully
    if(hDrive == INVALID_HANDLE_VALUE || GetLastError() != NO_ERROR)
        return false;

    BOOL bStatus; // Let the caller know if it worked or not
    DWORD dwDummy; // We don't really need this info
    if(bOpen) // Open the tray
        bStatus = DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, NULL, 
                                  0, NULL, 0, &dwDummy, NULL);
    else // Close the tray
        bStatus = DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, 
                                  NULL, 0, NULL, 0, &dwDummy, NULL);
    return bStatus?true:false;


  • Version 1.0 - March 28, 2007


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

Thomas Serface
Software Developer (Senior)
United States United States
I have been using VC++ with MFC since it first came out circa 1993 and I saw it demonstrated at a local Software Development show. I've been working for Rimage Corporation for around 23 years developing software to make our CD and DVD publishing hardware work.

When I'm not working I enjoy hanging out with my family, playing guitar, traveling, and taking my dogs for walks. My family enjoys watching Survivor on Thursday nights and we're not even embarassed by it.

Comments and Discussions

QuestionThanks Pin
String Sharp19-Jun-12 2:19
MemberString Sharp19-Jun-12 2:19 
Generalvery good Pin
jxy3331625-Sep-08 3:29
Memberjxy3331625-Sep-08 3:29 
GeneralI want to Try it but ..... Pin
allooba6-Nov-07 20:56
Memberallooba6-Nov-07 20:56 
GeneralRe: I want to Try it but ..... Pin
Thomas Serface7-Nov-07 5:06
MemberThomas Serface7-Nov-07 5:06 
GeneralRe: I want to Try it but ..... Pin
allooba7-Nov-07 21:57
Memberallooba7-Nov-07 21:57 
GeneralRe: I want to Try it but ..... Pin
Thomas Serface9-Nov-07 7:32
MemberThomas Serface9-Nov-07 7:32 
GeneralRe: I want to Try it but ..... Pin
allooba11-Nov-07 0:24
Memberallooba11-Nov-07 0:24 
GeneralRe: I want to Try it but ..... Pin
Thomas Serface11-Nov-07 19:05
MemberThomas Serface11-Nov-07 19:05 
GeneralRe: I want to Try it but ..... Pin
Thomas Serface12-Nov-07 4:49
MemberThomas Serface12-Nov-07 4:49 
Answeropen - close Pin
Sobirari Muhomori15-May-07 4:31
MemberSobirari Muhomori15-May-07 4:31 
GeneralRe: open - close Pin
Thomas Serface15-May-07 4:52
MemberThomas Serface15-May-07 4:52 
AnswerRe: open - close Pin
Sobirari Muhomori21-May-07 2:59
MemberSobirari Muhomori21-May-07 2:59 
GeneralRe: open - close Pin
Thomas Serface21-May-07 4:32
MemberThomas Serface21-May-07 4:32 
GeneralGood Job Pin
Daniel Vlasceanu5-Apr-07 20:08
MemberDaniel Vlasceanu5-Apr-07 20:08 
GeneralGood work Pin
Emil - Gabriel28-Mar-07 3:56
MemberEmil - Gabriel28-Mar-07 3:56 
GeneralRe: Good work Pin
Thomas Serface28-Mar-07 4:50
MemberThomas Serface28-Mar-07 4:50 

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.