Joshua, you rule! That's great code and it works so well. I would like to paste the code that I extracted in case someone else wants to put the CD eject function into their own program. Please note, this is not code for a GUI program... it simple ejects the CD tray. If you want a GUI interface, you need Joshua's code.
CODE
'***Here are things you need to first declare***
' // Constants for DeviceSetMedia
Public Const IOCTL_STORAGE_EJECT_MEDIA As Long = &H2D4808
Public Const INVALID_HANDLE_VALUE As Long = -1&
Public Const GENERIC_READ As Long = &H80000000
Public Const FILE_SHARE_READ As Long = &H1
Public Const FILE_SHARE_WRITE As Long = &H2
Public Const OPEN_EXISTING As Long = 3
' // Public Functions for DeviceSetMedia
Public Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
lpSecurityAttributes As Any, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Public Declare Function DeviceIoControl Lib "kernel32" _
(ByVal hDevice As Long, _
ByVal dwIoControlCode As Long, _
lpInBuffer As Any, _
ByVal nInBufferSize As Long, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Long, _
lpBytesReturned As Long, _
lpOverlapped As Any) As Long
Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
CODE
'***Here's the function to eject the CD***
Public Function DeviceSetMedia(sDrive As String, ctrlCode As Long) As Boolean
' // Function used to open CD tray
Dim hDevice As Long
Dim bytesReturned As Long
Dim success As Long
'obtain a handle to the device
hDevice = CreateFile("\\.\" & sDrive, _
GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
ByVal 0&, _
OPEN_EXISTING, _
0&, 0&)
If hDevice <> INVALID_HANDLE_VALUE Then
'If the operation succeeds,
'DeviceIoControl returns zero
success = DeviceIoControl(hDevice, _
ctrlCode, _
0&, _
0&, _
ByVal 0&, _
0&, _
bytesReturned, _
ByVal 0&)
End If
Call CloseHandle(hDevice)
End Function
CODE
'***Here's how to call the function***
sDrive = "D:"
Call DeviceSetMedia(sDrive, IOCTL_STORAGE_EJECT_MEDIA)
'I attempt to open both D and E drive
sDrive = "E:"
Call DeviceSetMedia(sDrive, IOCTL_STORAGE_EJECT_MEDIA)
Thanks again Jushua! I would have been lost without your help.