Imports System.Runtime.InteropServices
Imports System.Text
Public Class CDTray
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(<MarshalAs(UnmanagedType.LPStr)> ByVal command As String, _
ByVal returnString As StringBuilder, _
ByVal cchReturnLength As Int32, _
ByVal hwndCallback As IntPtr) As Int32
''' <summary>
''' Open the specified cdrom drives tray
''' </summary>
''' <param name="drive">In the form "X:\"</param>
''' <remarks></remarks>
Public Shared Sub OpenCDTray(ByVal drive As String)
Dim command As String
' Here we open a particular drive, and create an alias to it - cddrive:
command = String.Format("open {0} type cdaudio alias cddrive", drive)
Dim result As Int32 = mciSendString(command, Nothing, 0, Nothing)
If result > 0 Then
Throw New ApplicationException("When opening device, MCISendString returned: " & result)
End If
command = "set cddrive door open"
result = mciSendString(command, Nothing, 0, Nothing)
If result > 0 Then
Throw New ApplicationException("When ejecting device tray, MCISendString returned: " & result)
End If
command = "close cddrive"
result = mciSendString(command, Nothing, 0, Nothing)
If result > 0 Then
Throw New ApplicationException("When closing device, MCISendString returned: " & result)
End If
End Sub
' Close the tray.
' Doesn't work for my drive, but deviceIOControl does.
Public Shared Sub CloseCDTray(ByVal drive As String)
Dim command As String
command = String.Format("open {0} type cdaudio alias cddrive", drive)
Dim result As Int32 = mciSendString(command, Nothing, 0, Nothing)
If result > 0 Then
Throw New ApplicationException("When opening device, MCISendString returned: " & result)
End If
command = "set cddrive door closed"
result = mciSendString(command, Nothing, 0, Nothing)
If result > 0 Then
Throw New ApplicationException("When closing device tray, MCISendString returned: " & result)
End If
command = "close cddrive"
result = mciSendString(command, Nothing, 0, Nothing)
If result > 0 Then
Throw New ApplicationException("When closing device, MCISendString returned: " & result)
End If
End Sub
End Class