Private Const INVALID_HANDLE_VALUE As Short = -1
Private Const OPEN_EXISTING As Short = 3
Private Const FILE_ATTRIBUTE_NORMAL As Short = &H80S
Private Const FILE_SHARE_READ As Short = &H1S
Private Const FILE_SHARE_WRITE As Short = &H2S
Private Const GENERIC_READ As Integer = &H80000000
'Similar to the one to lock the drive above, but we send this message:
Private Const IOCTL_STORAGE_EJECT_MEDIA = &H2D4808
'oh, load aswell whilst I'm here...
Private Const IOCTL_STORAGE_LOAD_MEDIA = &H2D480C
Private Declare Function DeviceIoControl Lib "kernel32" _
(ByVal hDevice As Integer, _
ByVal dwIoControlCode As Integer, _
ByRef lpInBuffer As Object, _
ByVal nInBufferSize As Integer, _
ByRef lpOutBuffer As Object, _
ByVal nOutBufferSize As Integer, _
ByRef lpBytesReturned As Integer, _
ByRef lpOverlapped As OVERLAPPED) As Integer
'We use CreateFile to get a handle to the drive
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
'Required structure, wil be passed blank
Private Structure SECURITY_ATTRIBUTES
Dim nLength As Integer
Dim lpSecurityDescriptor As Integer
Dim bInheritHandle As Integer
End Structure
'Close the handle we got by using CreateFile to open the drive
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Integer) As Integer
'Required structure, wil be passed blank
Private Structure OVERLAPPED
Public Internal As Integer
Public InternalHigh As Integer
Public offset As Integer
Public OffsetHigh As Integer
Public hEvent As Integer
End Structure
Private Function EjectMedia(ByRef DriveLetter As String) As Boolean
'convert drive letter to a device path
Dim FullDrivePath As String = "//./" & DriveLetter
Dim hDrive As Integer
hDrive = CreateFile(FullDrivePath, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
New SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
Dim lpBytesReturned As Integer
If hDrive <> INVALID_HANDLE_VALUE Then
EjectMedia = CBool(DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, _
0, 0, 0, 0, lpBytesReturned, New OVERLAPPED))
Call CloseHandle(hDrive)
End If
End Function
Private Function LoadMedia(ByRef DriveLetter As String) As Boolean
'convert drive letter to a device path
Dim FullDrivePath As String = "//./" & DriveLetter
Dim hDrive As Integer
hDrive = CreateFile(FullDrivePath, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
New SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
Dim lpBytesReturned As Integer
If hDrive <> INVALID_HANDLE_VALUE Then
LoadMedia = CBool(DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, _
0, 0, 0, 0, lpBytesReturned, New OVERLAPPED))
Call CloseHandle(hDrive)
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
EjectMedia("D:")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
LoadMedia("D:")
End Sub