Eject CD/DVD

|
Public Sub ejectCDRom()
        Dim i As Integer

        'code to eject cdrom ok
        Dim owmp As Object
        Dim colCDROMs As Object
        owmp = CreateObject("WMPlayer.OCX.7")
        colCDROMs = owmp.cdromCollection
        If colCDROMs.Count >= 1 Then
            For i = 0 To colCDROMs.Count - 1
                colCDROMs.Item(i).eject()
            Next
        End If
    End Sub
Public Class Form1

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim CDTrayOpen As Boolean = False 'this is global

'this is in the click event
If CDTrayOpen Then
	'Close CD tray
	mciSendString("set CDAudio door closed", 0, 0, 0)
	CDTrayOpen = False
Else
	'Open CD tray
	mciSendString("set CDAudio door open", 0, 0, 0)
	CDTrayOpen = True
End If
End Sub
End Class
Originally Posted on December 14, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.