
If anybody is interested I have worked this out, it seems to function but I will
need to do some proper tests before I use it for real.
All I am trying to find out is if a drive is probably removeable, I think I
might be able to simplify it a bit such as getting rid of the
STORAGE_PROPERTY_QUERY variable, but let us get it working first.
Regards
Dave O.
'####### Watch out for Mr. Line Wrap.
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const FILE_SHARE_READ As Long = &H1
Private Const OPEN_EXISTING As Long = 3
Private Const FILE_SHARE_WRITE As Long = &H2
Private Const IOCTL_STORAGE_QUERY_PROPERTY As Long = &H2D1400
Private Type STORAGE_PROPERTY_QUERY
PropertyId As Integer
QueryType As Integer
AdditionalParameters(7) As Byte
End Type
Private 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
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Private 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
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA"
(ByVal nDrive As String) As Long
Private Function IsDriveRem(DrvLet As String) As Boolean
Dim r As Long
Dim s As String
Dim hDevice As Long
Dim Query As STORAGE_PROPERTY_QUERY
Dim OutBuf(0 To 1024) As Byte
Dim ReturnedLength As Long
s = Left$(DrvLet, 1) & ":"
r = GetDriveType(s)
If (r = 2) Or (r = 5) Then ' Removeable or optical
IsDriveRem = True
Else
hDevice = CreateFile("\\.\" & s, GENERIC_READ Or GENERIC_WRITE,
FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0&, 0&)
r = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, Query,
Len(Query), ByVal VarPtr(OutBuf(0)), 512, ReturnedLength, ByVal 0&)
CloseHandle hDevice
r = CLng(OutBuf(28))
IsDriveRem = (r = 1) Or (r = 4) Or (r = 7) 'SCSI, Firewire or USB
End If
End Function