PDA

View Full Version : Stop Flash Memory



Armin060
پنج شنبه 27 فروردین 1388, 17:37 عصر
سلام.
چطوری ميشه Flash Memory رو Stop كرد.

lizbazar
جمعه 28 فروردین 1388, 01:11 صبح
Module Module1


Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_CDROM = 5
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 FILE_ANY_ACCESS As Long = &H0
Public Const FILE_READ_ACCESS As Long = &H1
Public Const FILE_WRITE_ACCESS As Long = &H2
Public Const OPEN_EXISTING As Long = 3

Public Const IOCTL_STORAGE_EJECT_MEDIA As Long = &H2D4808
Public Const IOCTL_STORAGE_LOAD_MEDIA As Long = &H2D480C

Structure SECURITY_ATTRIBUTES
Dim nLength As Integer
Dim lpSecurityDescriptor As Integer
Dim bInheritHandle As Integer
End Structure

Structure OVERLAPPED
Dim Internal As Integer
Dim InternalHigh As Integer
Dim offset As Integer
Dim OffsetHigh As Integer
Dim hEvent As Integer
End Structure

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

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

Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Sub Main()
DeviceEject("e:\")
End Sub


Public Function DeviceEject(ByVal sDrive As String) As Boolean

Dim hDevice As Long
Dim bytesReturned As Long
Dim success As Long

'the drive letter has to be passed
'to CreateFile without a trailing slash (ie 'G:')
'sDrive = UnQualifyPath(sDrive)

'obtain a handle to the device (javascript:void(0))
'using the correct device syntax
hDevice = CreateFile("\\.\" & sDrive, _
GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
Nothing, _
OPEN_EXISTING, _
0&, 0&)


If hDevice <> INVALID_HANDLE_VALUE Then

'If the operation succeeds,
'DeviceIoControl returns a nonzero value
success = DeviceIoControl(hDevice, _
IOCTL_STORAGE_EJECT_MEDIA, _
IntPtr.Zero, _
0, _
IntPtr.Zero, _
0&, _
bytesReturned, _
Nothing)

End If

Call CloseHandle(hDevice)
DeviceEject = success <> 0

End Function

Private Function UnQualifyPath(ByVal sPath As String) As String

'removes any trailing slash from the path
sPath = Trim$(sPath)

If Right$(sPath, 1) = "\" Then
UnQualifyPath = Left$(sPath, Len(sPath) - 1)
Else
UnQualifyPath = sPath
End If

End Function

End Module

http://www.codeguru.com/vb/gen/vb_system/win32/article.php/c14935__4/