How to Open and Close the CDRom/DVDRom Tray Door using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (18 votes, average: 4.00 out of 5)
 

Opening or closing the optical disk drive(ODD) tray door in any Visual Basic and Visual Basic.NET language is easy to do thanks to the Windows API. This article will be using code based on the MCISendString Api which I made a complete tutorial for starting MCI. Click this link to check it out.

These codes are setup for VB.NET but all you have to do is change Integers to Longs which will then work pefectly fine with Visual Basic 6.0.


So the first thing you’ll want is this API call…

VB.NET

    'Api call to send the commands to the mci device
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As _
        String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As _
        Integer) As Integer

VB 6.0

    'Api call to send the commands to the mci device
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As _
        String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As _
        Long) As Long

 

Now just need to send the commands.

The code below will open and close the default CD/DVD Door and you won’t need to set/specify the ODD drive path.

'Pop it open
mciSendString("set cdaudio door open", 0, 0, 0)

'Shut it
mciSendString("set cdaudio door closed", 0, 0, 0)

 

OR

Use the codes below will setup and use the variables and the specified alias you want to use.

        '
        'This will simply get the api call return value that can be used if you use the MCIGetErrorString API.
        'This example won’t be using that API though.
        Dim retVal As Integer

        'This will contain the drive you want to open. Ex: D:\ or E:\ or whatever.
        Dim cdDrive As String
        '
        'Now setup the path to your computers cd or dvd drive you want to open or close.

        'Remember to put Quotes around any path you pass to the mci device.
        'Also specify the correct drive letter for the CD/DVD Drive.
        'I’m using drive “D:” which is the location of the DVD/CDRom on my computer.
        'You can specify the drive path like ‘D:’ or ‘D:\’
        cdDrive = Chr(34) & "d:" & Chr(34)

        'Now its time to setup the MCI Device using the CDAudio driver.

        'First setup a new device using the CDAudio driver with alias named “cd”.
        retVal = mciSendString("open " & cdDrive & " type cdaudio alias cd wait shareable", 0, 0, 0)

        'Now that the MCI Device is programmed and ready to go, you can start sending it commands.
        'The codes below will Open the CD or DVD Tray Door and Close the CD/DVD Door…

        'This will Open the door.
        retVal = mciSendString("set cd door open", 0, 0, 0)

        'This will Close the door.
        retVal = mciSendString("set cd door closed", 0, 0, 0)


Thats all there is to it! If the code executed properly, your optical drive should have opened and/or closed. The code here will work with all VBs and VB.NET languages.

Just Change all Integer types to Long types if your using VB 6.0.

If your wanting to make a complete CD Player application, I have complete libraries for both VB 5/6 and Visual Basic.NET at my Visual Basic Code Source website. Anyways, have fun!

Jason

11 thoughts on “How to Open and Close the CDRom/DVDRom Tray Door using VB and VB.NET

  1. Abbas

    This is a Fantastic Code which helps to Open and Close System Tray

    I was in Search of a Simple Code which can do this For Me

    Thank You Very Much for Your Most Useful and Important Codes Please Post More Useful Codes

    Reply
  2. VB6Programmer

    I did this code yesterday for my second DVD-ROM, but no response. My old DVD-ROM is about 3 years old, but it worked yesterday. Thanks for the codes!

    Reply
  3. Priyam

    Nice article. Actually I have been working on such application. But I have been facing a problem, can’t figure it out how to solve it. How can I detect the status of the CD door? I tried two options for mcisendstring- ‘Status CD Audio Mode’ and ‘Status CD Audio Mediapresent’. But the problem is that if there is no CD in the device, the mcisendstring is returning the value ‘Opened’ in both cases if the CD tray is opened or closed. How can I solve it? Anyone please help.

    Reply
  4. curt

    Help! I have a BACKUP subroutine on file “MB.xls” to copy a directory (to DVD or second HD). Thanks to you the “assistant bubble” now opens the DVD tray! GREAT. I do this from “MS.xls” (Main Switch) after closing this file.
    All files in my VB directory are copied, but i can’t close and backup the MB.xls itself because it needs to run the code. I can only do it manually with Explorer. Solution?

    Reply
  5. AYYAPPASHRINE

    WOW, you are great…. i have tried for a very long time , that how to access cd/dvd …

    Really you are too too good at programming in vb.net

    I REQUEST YOU VERY HUMBLY TO GIVE ME A CODE FOR WRITING AND READING CD/DVD

    PLEASE HELP ME…….

    PLS SEND ME THE LINK OR CODE TO MY MAIL….

    Reply
  6. RedShad0w

    I have the same problem that Priyam has. I have tried many ways to try and “detect” if the cd drive is open or closed. As long as there is no cd in the drive, it comes back as open, even when it is closed. Anyone have any ideas?

    Reply
  7. RIFKHAN (Laser Technologies)

    i also hav the same problem….
    I have tried many ways to try and “detect” if the cd drive is open or closed. As long as there is no cd in the drive, it comes back as open, even when it is closed. Anyone have any ideas?
    ~if found email me “mnmrifkhan@gmail.com”~
    thanx…

    Reply
  8. Sunit

    thanks a lot….
    how to know whether there is a disc inside the drive or its empty?….and how to find the drive letter of a cd drive…..? please help me…

    Reply

Leave a Reply