Click here to Skip to main content
6,595,854 members and growing! (18,913 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Hardware     Intermediate

Open or Close a CD/DVD Drive Drawer

By Thomas Serface

Provides a sample project with code to open and/or close a CD or DVD drive drawer.
VC8.0Win2K, WinXP, Win2003, Vista, MFC, VS2005, Dev
Posted:27 Mar 2007
Views:26,227
Bookmarked:20 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 5.01 Rating: 4.81 out of 5

1

2

3
1 vote, 9.1%
4
10 votes, 90.9%
5

Screenshot - CDOpen1.jpg

Introduction

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.

Background

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;
    cs.Format(_T("\\\\.\\%c:"),cDrive);
    HANDLE hDrive = CreateFile(cs, GENERIC_READ, FILE_SHARE_READ, 
                               NULL, OPEN_EXISTING, 
    FILE_ATTRIBUTE_NORMAL, NULL);

    // 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);
    CloseHandle(hDrive);
    return bStatus?true:false;
}

History

  • Version 1.0 - March 28, 2007

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

Thomas Serface


Member
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.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Generalvery good Pinmemberjxy333163:29 25 Sep '08  
GeneralI want to Try it but ..... Pinmemberallooba20:56 6 Nov '07  
GeneralRe: I want to Try it but ..... Pinmembertserface5:06 7 Nov '07  
GeneralRe: I want to Try it but ..... Pinmemberallooba21:57 7 Nov '07  
GeneralRe: I want to Try it but ..... Pinmembertserface7:32 9 Nov '07  
GeneralRe: I want to Try it but ..... Pinmemberallooba0:24 11 Nov '07  
GeneralRe: I want to Try it but ..... Pinmembertserface19:05 11 Nov '07  
GeneralRe: I want to Try it but ..... Pinmembertserface4:49 12 Nov '07  
Answeropen - close Pinmembercurlyhands4:31 15 May '07  
GeneralRe: open - close Pinmembertserface4:52 15 May '07  
AnswerRe: open - close Pinmembercurlyhands2:59 21 May '07  
GeneralRe: open - close Pinmembertserface4:32 21 May '07  
GeneralGood Job PinmemberDaniel Vlasceanu20:08 5 Apr '07  
GeneralGood work Pinmembersk8er_boy2873:56 28 Mar '07  
GeneralRe: Good work Pinmembertserface4:50 28 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Mar 2007
Editor: Smitha Vijayan
Copyright 2007 by Thomas Serface
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project