Open/Close CD-ROM Tray with VB

Discussion in 'Visual Basic [VB]' started by pradeep, Apr 23, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    When a program makes use of the CD-ROM drive, it can be a nice touch to open and close the door under program control.

    The technique that allows you to do this relies on the mciSendString function from the Windows API. This function provides a general purpose interface to Windows' multimedia capabilities. This is its declaration:

    Code:
    Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
       (ByVal lpCommandString As String, ByVal lpReturnString As String, _
       ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    Because the CD-ROM is considered to be a multimedia device, you can use this API function to control it. The first argument tells the device what you want to do. For example, pass the string "set CDAudio door open" to open the door, like this:

    Code:
    retval = mciSendString("set CDAudio door open", "", 0, 0)
    You can see that the second through the fourth arguments aren't used in this case and are passed either a blank string or the value zero. Likewise, the function's return value can be ignored. Along with the function declaration shown above, you can put the following two procedures in a code module in your program to provide control of the CD-ROM door.

    Code:
    Public Sub OpenCDDoor()
         Dim retval As Long
         retval = mciSendString("set CDAudio door open", "", 0, 0)
     End Sub
     
     
     Public Sub CloseCDDoor()
         Dim retval As Long
         retval = mciSendString("set CDAudio door closed", "", 0, 0)
     End Sub
    Note: Calling OpenCDDoor when the door is already open, or CloseCDDoor when it is closed, has no effect.
     
  2. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    Ilike yourconecpt. AS I am new to VB. Ijust tried yourcode just cpoyand paste to VB, but it is giving compile time error. Can you help me to rectify this.
    Error:

    Compile Time Error:
    Constants,Fixed-length strings,arrays,user-defined types and Declare statements not allowed as public members of object modules
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    DLL procedures declared in standard modules are public by default and can be called from anywhere in your application. DLL procedures declared in any other type of module are private to that module, and you must identify them as such by preceding the declaration with the Private keyword.

    Code:
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
    (ByVal lpCommandString As String, ByVal lpReturnString As String, _
    ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    rizwan6feb, No self promotion.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice