01-10-12, 11:47 PM
كاتب الموضوع : samerselo
يمكن ذلك باستخدام الكود التالي
كود :
Public Module 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
'Make it all caps for easy interpretation
DriveLetterAndColon = UCase(Left(EjectDrive & ":", 2))
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
'Make it all caps for easy interpretation
DriveLetterAndColon = UCase(Left(EjectDrive & ":", 2))
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 Module
كود :
EjectRemovable("h:\")
LoadRemovable("h:\")
كود :
Module EjectLoadUsingMCI
<DllImport("winmm", EntryPoint:="mciSendStringA")> _
Private Sub mciSendString(ByVal lpszCommand As String, _
ByVal lpszReturnString As String, ByVal cchReturnLength As Integer, _
ByVal hwndCallback As Integer)
End Sub
Public Sub OpenCDDoor(ByVal Drive As String)
mciSendString("open cdaudio!" & Drive, "", 0, 0)
mciSendString("Set " & Drive & " door open wait", "", 0, 0)
End Sub
Public Sub CloseCDDoor(ByVal drive As String)
mciSendString("open cdaudio!" & drive, "", 0, 0)
mciSendString("Set " & drive & " door closed wait", "", 0, 0)
End Sub
End Module