14. august 2003 - 16:04Der er
4 kommentarer og 1 løsning
skub drev ud
Jeg sidder med en access database. Jeg skal have en knap der laver backup på et flytbart drev (usb stick) og derefter skubber sticken ud (unmounter ell) således at jeg ikke mister data... er der nogen der har forsøgt sig med det før?
er det for nemt, eller skal der flere point til? Jeg har forsøgt mig med nedenstående, men den slukker bare drevet. Det jeg skal bruge, er at drevet bliver stoppet...
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_MEDIA_REMOVAL As Long = &H2D4804
Public Type PREVENT_MEDIA_REMOVAL PreventMediaRemoval As Byte End Type
Public Declare Function GetLogicalDriveStrings Lib "kernel32" _ Alias "GetLogicalDriveStringsA" _ (ByVal nBufferLength As Long, _ ByVal lpBuffer As String) As Long
Public Declare Function GetDriveType Lib "kernel32" _ Alias "GetDriveTypeA" _ (ByVal lpRootPathName As String) 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 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 CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long
Public Function DeviceLock(sDrive As String, fLock As Boolean) As Boolean
Dim hDevice As Long Dim PMR As PREVENT_MEDIA_REMOVAL 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 'using the correct device syntax hDevice = CreateFile("\\.\" & sDrive, _ GENERIC_READ, _ FILE_SHARE_READ Or FILE_SHARE_WRITE, _ ByVal 0&, _ OPEN_EXISTING, _ 0&, 0&)
If hDevice <> INVALID_HANDLE_VALUE Then
'assign the fLock value to the 'PREVENT_MEDIA_REMOVAL type PMR.PreventMediaRemoval = CByte(Abs(fLock))
'If the operation succeeds, 'DeviceIoControl returns a nonzero value success = DeviceIoControl(hDevice, _ IOCTL_STORAGE_MEDIA_REMOVAL, _ PMR, _ Len(PMR), _ ByVal 0&, _ 0&, _ bytesReturned, _ ByVal 0&)
End If
Call CloseHandle(hDevice) DeviceLock = success
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
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.