Class EjectLoadRemovable
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As Long, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
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 Object) As Integer
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
Private Const INVALID_HANDLE_VALUE As Short = -1
Private Const OPEN_EXISTING As Short = 3
Private Const FILE_FLAG_DELETE_ON_CLOSE As Integer = 67108864
Private Const GENERIC_READ As Integer = &H80000000
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const IOCTL_STORAGE_EJECT_MEDIA As Integer = 2967560
Private Const IOCTL_STORAGE_LOAD_MEDIA As Integer = 2967564
Private Const IOCTL_STORAGE_LOAD_MEDIA2 As Integer = 2951180
Private Const VWIN32_DIOC_DOS_IOCTL As Short = 1
' To Eject Removable media just pass the drive letter to the sub
Public Sub EjectRemovable(ByVal EjectDrive As String)
Dim hDrive, DummyReturnedBytes As Integer
Dim DriveLetterAndColon As String
DriveLetterAndColon = UCase(Left(EjectDrive & ":", 2)) ' Make it all caps for easy interpretation
hDrive = CreateFile("\\.\" & DriveLetterAndColon, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If hDrive <> INVALID_HANDLE_VALUE Then
' Eject media!
Call DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, DummyReturnedBytes, 0)
Call CloseHandle(hDrive) ' Clean up after ourselves
End If
End Sub
' To Load Removable media just pass the drive letter to the sub
Public Sub LoadRemovable(ByVal EjectDrive As String)
Dim hDrive, DummyReturnedBytes As Integer
Dim DriveLetterAndColon As String
DriveLetterAndColon = UCase(Left(EjectDrive & ":", 2)) ' Make it all caps for easy interpretation
hDrive = CreateFile("\\.\" & DriveLetterAndColon, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If hDrive <> INVALID_HANDLE_VALUE Then
' Eject media!
Call DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, 0, 0, 0, 0, DummyReturnedBytes, 0)
Call CloseHandle(hDrive) ' Clean up after ourselves
End If
End Sub
End Class
Please SUBSCRIBE to get new articles directly into your Email inbox!
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As Long, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
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 Object) As Integer
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
Private Const INVALID_HANDLE_VALUE As Short = -1
Private Const OPEN_EXISTING As Short = 3
Private Const FILE_FLAG_DELETE_ON_CLOSE As Integer = 67108864
Private Const GENERIC_READ As Integer = &H80000000
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const IOCTL_STORAGE_EJECT_MEDIA As Integer = 2967560
Private Const IOCTL_STORAGE_LOAD_MEDIA As Integer = 2967564
Private Const IOCTL_STORAGE_LOAD_MEDIA2 As Integer = 2951180
Private Const VWIN32_DIOC_DOS_IOCTL As Short = 1
' To Eject Removable media just pass the drive letter to the sub
Public Sub EjectRemovable(ByVal EjectDrive As String)
Dim hDrive, DummyReturnedBytes As Integer
Dim DriveLetterAndColon As String
DriveLetterAndColon = UCase(Left(EjectDrive & ":", 2)) ' Make it all caps for easy interpretation
hDrive = CreateFile("\\.\" & DriveLetterAndColon, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If hDrive <> INVALID_HANDLE_VALUE Then
' Eject media!
Call DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, DummyReturnedBytes, 0)
Call CloseHandle(hDrive) ' Clean up after ourselves
End If
End Sub
' To Load Removable media just pass the drive letter to the sub
Public Sub LoadRemovable(ByVal EjectDrive As String)
Dim hDrive, DummyReturnedBytes As Integer
Dim DriveLetterAndColon As String
DriveLetterAndColon = UCase(Left(EjectDrive & ":", 2)) ' Make it all caps for easy interpretation
hDrive = CreateFile("\\.\" & DriveLetterAndColon, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If hDrive <> INVALID_HANDLE_VALUE Then
' Eject media!
Call DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, 0, 0, 0, 0, DummyReturnedBytes, 0)
Call CloseHandle(hDrive) ' Clean up after ourselves
End If
End Sub
End Class
Please SUBSCRIBE to get new articles directly into your Email inbox!
تعليقات
إرسال تعليق