Home > Visual Basic > Open / Close CD ROM in Visual Basic

Open / Close CD ROM in Visual Basic

This code demonstrates how to create CD ROM open and close calls. Create a Windows Forms Application and add two buttons. I will leave default names in this example Form1 for the form and Button1 and Button2 for buttons. I’ve changed the text property only.

form1

Click F7 to go to the code view and add the folowing code:

' Form design
' - - - - - -
' Button1 – Button control
' Button2 – Button control
' - - - - - -
' Form code

Public Class Form1
Private Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Long, ByVal uReturnLength As _
Long, ByVal hwndCallback As Long) As Long

Dim vDrive As String
Dim vAlias As String

Private Sub Button1_Click() Handles Button1.Click
' Open
vDrive = "f:/"
vAlias = "vv" & vDrive
Call mciSendString("Open " & vDrive & ": Alias " & vAlias & _
" Type CDAudio", 0, 0, 0)
Call mciSendString("Set " & vAlias & " Door Open", 0, 0, 0)
End Sub

Private Sub Button2_Click() Handles Button2.Click
' Close
vDrive = "f:/"
vAlias = "vv" & vDrive
Call mciSendString("Open " & vDrive & ": Alias " & vAlias & _
" Type CDAudio", 0, 0, 0)
Call mciSendString("Set " & vAlias & " Door Closed", 0, 0, 0)
End Sub
End Class

Click F5 to Run the application.

Download sample

Categories: Visual Basic Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Spam Protection by WP-SpamFree