Results 1 to 17 of 17

Thread: Eject/Close a cd?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    UK
    Posts
    204

    Eject/Close a cd?

    Does anyone know how to Open And close a cd drive in VB??

    Many Thanks!

  2. #2
    Frenzied Member jjortiz's Avatar
    Join Date
    Mar 2001
    Location
    NYC
    Posts
    1,768
    Try this. I just grabbed it from some other code i had.

    VB Code:
    1. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLenght As Long, ByVal hwndCallback As Long) As Long
    2.  
    3. Public Sub doorOpen()
    4. Dim lngResult As Long
    5. lngResult = mciSendString("open cdaudio", "", 0, 0)
    6. lngResult = mciSendString("set cdaudio door open", "", 0, 0)
    7. lngResult = mciSendString("close cdaudio", "", 0, 0)
    8. End Sub
    9. Public Sub doorClose()
    10. Dim lngResult As Long
    11. lngResult = mciSendString("open cdaudio", "", 0, 0)
    12. lngResult = mciSendString("set cdaudio door closed", "", 0, 0)
    13. lngResult = mciSendString("close cdaudio", "", 0, 0)
    14. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    UK
    Posts
    204
    That dosnt do anything?

    using this in my form

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLenght As Long, ByVal hwndCallback As Long) As Long

    Public Sub doorOpen()
    Dim lngResult As Long
    lngResult = mciSendString("open cdaudio", "", 0, 0)
    lngResult = mciSendString("set cdaudio door open", "", 0, 0)
    lngResult = mciSendString("close cdaudio", "", 0, 0)
    End Sub

    Public Sub doorClose()
    Dim lngResult As Long
    lngResult = mciSendString("open cdaudio", "", 0, 0)
    lngResult = mciSendString("set cdaudio door closed", "", 0, 0)
    lngResult = mciSendString("close cdaudio", "", 0, 0)
    End Sub

    Private Sub Command1_Click()
    doorOpen
    End Sub

  4. #4
    Frenzied Member jjortiz's Avatar
    Join Date
    Mar 2001
    Location
    NYC
    Posts
    1,768
    Put the code in a module and then access it in the following way.
    module1.dooropen.

  5. #5
    Lively Member
    Join Date
    Jul 2001
    Posts
    67

    fool.!

    you have to call it up using:

    Call openDoor where you want it to happen, make sure that c0de is in general declerations too!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    UK
    Posts
    204
    Hey cool!! i was just bout to try it in a module!, do you know anyway to make it open from other drives?? like your e:\ drive?

  7. #7
    Frenzied Member jjortiz's Avatar
    Join Date
    Mar 2001
    Location
    NYC
    Posts
    1,768
    That's a very good question. I do not know. I will try to find out.

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274

    Re: fool.!

    Originally posted by Raptor_989
    fool!you have to call it up using:

    Call openDoor where you want it to happen, make sure that c0de is in general declerations too!
    Aww,
    Now that aint nice. And since u called the sub incorrectly, it looks a bit silly as well. You just need to have openDoor on a single line to call the routine or use Call (openDoor) with parentheses.
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9
    Frenzied Member jjortiz's Avatar
    Join Date
    Mar 2001
    Location
    NYC
    Posts
    1,768
    Man i tell you, everyone is always fast to comment. Just too funny.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    UK
    Posts
    204

    Thanks!

    Thanks beachbum for sticking up for me! im a newbie!

  11. #11
    Frenzied Member jjortiz's Avatar
    Join Date
    Mar 2001
    Location
    NYC
    Posts
    1,768
    You go beachbum.

  12. #12
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    This is a general door open/close thing. You just give it the drive letter. If you need those, use GetLogicalDriveStrings() to get the ones in the system, and then break it apart and give them to GetDriveType to determine if they're cd roms or not.
    VB Code:
    1. 'in a module
    2. Public Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal lpzCommand As String, ByVal lpzReturn As String, ByVal nReturnLength As Long, ByVal hWndCallback As Long) As Long
    3.  
    4. Public Sub DriveDoor(ByVal strDriveLetter As String, Optional ByVal blnOpenDoor As Boolean = True)
    5.    'tells the MCI to ready the device and nickname it "dooralias"
    6.    mciSendString "open " & strDriveLetter & ": type cdaudio alias dooralias", vbNullString, 0, 0
    7.    'opens/closes the given drive door based on the boolean flag
    8.    mciSendString "set dooralias door " & IIf(blnOpenDoor, "open", "closed"), vbNullString, 0, 0
    9.    'tells MCI that we're done with the device
    10.    mciSendString "close dooralias", vbNullString, 0, 0
    11. End Sub
    12.  
    13. 'to open the door of drive X
    14. DriveDoor "x", True  'true is assumed, and may be left out
    15.  
    16. 'to close the door of drive X
    17. DriveDoor "x", False
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  13. #13
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804
    Kaverin, that code caused my system to lock up, I use NT4, any ideas why?

  14. #14
    Hyperactive Member scsa20's Avatar
    Join Date
    Apr 2001
    Location
    Mars
    Posts
    456
    here, try this...Nucleus give me this.
    Last edited by scsa20; Aug 13th, 2001 at 09:47 PM.


    p|-|34|2 /\/\3 f0|2 | $p34k 1337
    My TSS quote of the day: "If your haveing a bad day, just press the restart button."

  15. #15
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    If the one I posted does cause a lock up, Nucleus's one might do it as well. They're both the same basic idea going through the MCI to do it. I have no clue what could be causing that though. I've got win98 myself. There may be something different that has to be done in NT, but I've never run across it. Thanks for mentioning it though, or I would have never known of anything like that happening.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    UK
    Posts
    204
    i used Kaverin's code and changed it about a bit, i was just wondering if anyone can make the drives list only display Cd-Rom drvies? i think you might have to use a list box??
    i attached my project so far

  17. #17
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    This will do it. You could change it just to accept either a listbox or combobox, but I made it do either.
    VB Code:
    1. Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal cchBuffer As Long, ByVal lpzBuffer As String) As Long
    2. Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal lpzDrive As String) As Long
    3.  
    4. Public Const DRIVE_CDROM As Long = 5
    5.  
    6. Public Sub AddCDDrives(ByRef ctlListOrCombo As Control)
    7.    Dim strBuffer As String
    8.    Dim lngLength As Long
    9.    Dim astrDrives As Variant
    10.    Dim i As Long
    11.    If (TypeOf ctlListOrCombo Is ComboBox) Or (TypeOf ctlListOrCombo Is ListBox) Then
    12.       'make a buffer
    13.       strBuffer = Space$(128)
    14.       'get the drive letters
    15.       lngLength = GetLogicalDriveStrings(Len(strBuffer), strBuffer)
    16.       'trim off the last null char
    17.       strBuffer = Left$(strBuffer, lngLength - 1)
    18.       'break the return into separate drives
    19.       astrDrives = Split(strBuffer, vbNullChar)
    20.       ctlListOrCombo.Clear
    21.       'go through each drive to see if it's a cdrom, and add it if so
    22.       For i = 0 To UBound(astrDrives)
    23.          If GetDriveType(astrDrives(i)) = DRIVE_CDROM Then ctlListOrCombo.AddItem astrDrives(i)
    24.       Next i
    25.    End If
    26. End Sub
    27.  
    28. 'usage
    29. AddCDDrives drivelistbox
    30. 'or
    31. AddCDDrives drivecombobox
    Last edited by Kaverin; Aug 14th, 2001 at 01:26 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width