Opening and closing the CDROM

Hello every one i am new to Visual Basic 6 and I have a code for openiong and closing the cdrom from another program But I can not seem to get it to work the code as follows
Private Sub winoperations_Click(Index As Integer)
Select Case Index

Case 0

'To open the CD door, use this code:
lngReturn = mciSendString("set CDAudio door open", strReturn, 127, 0)


Case 1
'To close the CD door, use this code:
lngReturn = mciSendString("set CDAudio door closed", strReturn, 127, 0)
Now since I New to this program the error i get is mcisendString not defined but since i am new i really dont know what that means all your help is a preciated
FwankybAsked:
Who is Participating?
 
burnttimeConnect With a Mentor Commented:
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrrtning As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long


Private Sub winoperations_Click(Index As Integer)
Dim SendStr As String, ReturnStr As String
Select Case Index

Case 0
SendStr = mciSendString("Set cdaudio door open", ReturnStr, 0, 0)

Case 1
SendStr = mciSendString("Set cdaudio door closed", ReturnStr, 0, 0)

End Select
End Sub
0
 
vinnyd79Commented:
try this:

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

Private Sub Command1_Click()
retvalue = mciSendString("set Cdaudio door open", returnstring, 127, 0)
End Sub

Private Sub Command2_Click()
retvalue = mciSendString("set Cdaudio door closed", returnstring, 127, 0)
End Sub
0
 
brianb99999Commented:
Add this into the declarations section (above any functions/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

Brian.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
FwankybAuthor Commented:
well both of them did not work yet well i have other things in that case should i remove those things and just have this case with the cdrom or get rid of the case all together?
0
 
vinnyd79Commented:
Are the declarations at the top of your form? The case looks like its needed to determine what button was pushed in a command button array. What OS are you using?
0
 
FwankybAuthor Commented:
Ok in declerations i have one think I put the decleration in but it did not work and it is just a simple Click and im running Windows Xp Professiona and Visual Basic 6
0
 
vinnyd79Commented:
Also try declaring lngReturn as a Long


Private Sub winoperations_Click(Index As Integer)
Dim lngReturn as Long
Select Case Index

Case 0

'To open the CD door, use this code:
lngReturn = mciSendString("set CDAudio door open", strReturn, 127, 0)


Case 1
'To close the CD door, use this code:
lngReturn = mciSendString("set CDAudio door closed", strReturn, 127, 0)
0
 
FwankybAuthor Commented:
Ok i did I dim lng return and still get the error now vinny where do i put this
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

Private Sub Command1_Click()
retvalue = mciSendString("set Cdaudio door open", returnstring, 127, 0)
End Sub

Private Sub Command2_Click()
retvalue = mciSendString("set Cdaudio door closed", returnstring, 127, 0)
End Sub ?
0
 
vinnyd79Commented:
This part has to be in the General/Declarations area at the top of the form you are using it on. If you want to use it from multiple forms you can replace "Private" with "Public" and put it into a .bas module:

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



This would on the form:

Private Sub Command1_Click()
Dim retvalue as Long
retvalue = mciSendString("set Cdaudio door open", returnstring, 127, 0)
End Sub

Private Sub Command2_Click()
Dim retvalue as Long
retvalue = mciSendString("set Cdaudio door closed", returnstring, 127, 0)
End Sub
0
 
FwankybAuthor Commented:
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
Private Sub Command1_Click()
Dim retvalue As Long
retvalue = mciSendString("set Cdaudio door open", returnstring, 127, 0)
End Sub

Private Sub Command2_Click()
Dim retvalue As Long
retvalue = mciSendString("set Cdaudio door closed", returnstring, 127, 0)
End Sub
 Im getting the error
Constans,fixed-length strings, arrays, uder-defined types and Declare statements not allowed as Public membors object modules
0
 
s_sansanwalCommented:
add private in declaration of function

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
0
 
FwankybAuthor Commented:
I have done that already  does not work like that
0
 
JR2003Commented:
Have you got more than one CD drive?
Start a new project and place an EditBox and two buttons on the form.
Paste the following code into the form:




==============
*** IMPORTANT ***
==============

When you run the project enter the drive letter of the CD you want to open/close

Option Explicit

Private Type MCI_OPEN_PARMS
    dwCallback As Long
    wDeviceID As Long
    sDeviceType As String
    sElementName As String
    sAlias As String
End Type

Public Enum CDDoor_Call
    MCI_SET_DOOR_OPEN = &H100&
    MCI_SET_DOOR_CLOSED = &H200&
End Enum

Private Declare Function mciSendCommand Lib "winmm.dll" Alias "mciSendCommandA" (ByVal wDeviceID As Long, ByVal uMessage As Long, ByVal dwParam1 As Long, ByRef dwParam2 As Any) As Long
Private Const MCI_CLOSE = &H804
Private Const MCI_OPEN = &H803
Private Const MCI_OPEN_ELEMENT = &H200&
Private Const MCI_OPEN_TYPE = &H2000&
Private Const MCI_SET = &H80D


Private Sub Command1_Click()
    'Open Door
    CDDoor Text1.Text, MCI_SET_DOOR_OPEN

End Sub

Private Sub Command2_Click()
    'Close Door
    CDDoor Text1.Text, MCI_SET_DOOR_CLOSED

End Sub

Private Sub CDDoor(m_Drive As String, mCDDoor As CDDoor_Call, Optional Silent As Boolean, Optional ByRef EncounteredError As Boolean)

    Dim iRet As Long
    Dim OP As MCI_OPEN_PARMS
   
    OP.sDeviceType = "cdaudio"
    OP.sElementName = m_Drive
    iRet = mciSendCommand(0, MCI_OPEN, (MCI_OPEN_TYPE Or MCI_OPEN_ELEMENT), OP)
    If iRet = 0 Then
        mciSendCommand OP.wDeviceID, MCI_SET, mCDDoor, OP 'Open/Close the door
        mciSendCommand OP.wDeviceID, MCI_CLOSE, 0, 0      'Close the device handle
    Else
        EncounteredError = True
        If Not Silent Then
            MsgBox "Error loading drive", vbCritical, App.Title
        End If
    End If

End Sub
0
 
FwankybAuthor Commented:
no i dont but i want this program to do more then that so its not just going to be openand closing the cd drive
0
 
burnttimeCommented:
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrrtning As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long


Private Sub winoperations_Click(Index As Integer)
Select Case Index

Case 0
lngReturn = mciSendString("set CDAudio door open", strReturn, 127, 0)

Case 1
lngReturn = mciSendString("set CDAudio door closed", strReturn, 127, 0)

End Select
End Sub
0
 
FwankybAuthor Commented:
No errors for that ^ it just does not happen
0
 
FwankybAuthor Commented:
since hessiatation but did nto open but seems to be getting there ^
0
 
FwankybAuthor Commented:
I mean well it hestated the first time i tired to open it but it stoped
0
All Courses

From novice to tech pro — start learning today.