CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008
    Posts
    27

    What am i doing wrong?

    ok i wanted to make a button to open/close the CD tray i have the code to do it but when i compile it nothing happens could somebody please tell me what i did wrong.
    Code:
    Public Class Form1
        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
    
        Function CDDriveOperation(ByVal Command As String, ByVal hWnd As Long) As String
            Dim Buff As String
            Dim dwR As Long
            Buff = Space$(100) ' Create a buffer
        dwR = mciSendString(Command, ByVal Buff, Len(Buff), hWnd)
            CDDriveOperation = Buff
        End Function
    
        'Call this Sub to Close the CD Door
        Private Sub CloseCDDoor()
            Dim ret As String
            ret = CDDriveOperation("set cdaudio door closed", 0)
        End Sub
    
        ' Call this Sub to Open the CD Door
        Private Sub OpenCDDoor()
            Dim ret As String
            ret = CDDriveOperation("set cdaudio door open", 0)
        End Sub
    
    End Class

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: What am i doing wrong?

    Probably declared it wrong...


    Code:
    Option Explicit
    ' add Microsoft Multimedia Control component
    
    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
      
    Private Sub Form_Load()
      OpenCDDoor
      CloseCDDoor
    End Sub
    
    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

    EDIT: Command is a reserved word, I think. I don't know what you are passing as command, either
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: What am i doing wrong?

    OK, firstly, my default CD ROM / DVD ROM drive is not D:\, it is F:\.

    I always have to do this :

    Code:
    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
    
    Dim sDriveLetter As String
    Dim sAlias As String
    
    
    Private Sub Command3_Click()
       sDriveLetter = "F"
       sAlias = "CD" & sDriveLetter
       mciSendString "open " & sDriveLetter & ": type cdaudio alias " & sAlias & " wait", returnstring, 0&, 0&
       mciSendString "set " & sAlias & " door open wait", returnstring, 0&, 0&
       mciSendString "close " & sAlias, returnstring, 0&, 0&
    End Sub
    
    Private Sub Command4_Click()
       sDriveLetter = "F"
       sAlias = "CD" & sDriveLetter
       mciSendString "open " & sDriveLetter & ": type cdaudio alias " & sAlias & " wait", returnstring, 0&, 0&
       mciSendString "set " & sAlias & " door closed wait", returnstring, 0&, 0&
       mciSendString "close " & sAlias, returnstring, 0&, 0&
    End Sub
    My advice is, substitute sDriveLetter = "F" with sDriveLetter = "D" or whatever your default CD / DVD ROM drive is.

    Works like a charm

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

Featured