Solved

Opening and closing the CDROM

Posted on 2004-08-03
18
2,238 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:Fwankyb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 2
  • +3
18 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 11710918
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
 
LVL 5

Expert Comment

by:brianb99999
ID: 11710921
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
 

Author Comment

by:Fwankyb
ID: 11710958
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 28

Expert Comment

by:vinnyd79
ID: 11711034
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
 

Author Comment

by:Fwankyb
ID: 11711045
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 11711049
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
 

Author Comment

by:Fwankyb
ID: 11711089
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 11711122
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
 

Author Comment

by:Fwankyb
ID: 11711167
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
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11711212
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
 

Author Comment

by:Fwankyb
ID: 11711234
I have done that already  does not work like that
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11711262
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
 

Author Comment

by:Fwankyb
ID: 11711273
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
 
LVL 1

Expert Comment

by:burnttime
ID: 11711315
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
 

Author Comment

by:Fwankyb
ID: 11711337
No errors for that ^ it just does not happen
0
 
LVL 1

Accepted Solution

by:
burnttime earned 125 total points
ID: 11711347
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
 

Author Comment

by:Fwankyb
ID: 11711375
since hessiatation but did nto open but seems to be getting there ^
0
 

Author Comment

by:Fwankyb
ID: 11712210
I mean well it hestated the first time i tired to open it but it stoped
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vbModal 12 75
Advice in Xamarin 21 121
Set email body to html using vbscript 6 96
VBA- How to change font, color and format in outlook e-mail sent from Access 2 173
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question