Results 1 to 10 of 10

Thread: Safely Remove USB Flash Drive

Threaded View

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Safely Remove USB Flash Drive

    Thanks to rm_03 for linking this solution. I pared down the German code to the barest essentials. This will be included in my XP library next time I update it, but until then:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function CM_Get_DevNode_Status Lib "setupapi.dll" (lStatus As Long, lProblem As Long, ByVal hDevice As Long, ByVal dwFlags As Long) As Long
    4. Private Declare Function CM_Get_Parent Lib "setupapi.dll" (hParentDevice As Long, ByVal hDevice As Long, ByVal dwFlags As Long) As Long
    5. Private Declare Function CM_Locate_DevNodeA Lib "setupapi.dll" (hDevice As Long, ByVal lpDeviceName As Long, ByVal dwFlags As Long) As Long
    6. Private Declare Function CM_Request_Device_EjectA Lib "setupapi.dll" (ByVal hDevice As Long, lVetoType As Long, ByVal lpVetoName As Long, ByVal cbVetoName As Long, ByVal dwFlags As Long) As Long
    7. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    8. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    9. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal lpdwReserved As Long, lpdwType As Long, lpData As Any, lpcbData As Long) As Long
    10.  
    11. ' Safely remove USB flash drive
    12. Public Function SafelyRemove(ByVal pstrDrive As String) As Boolean
    13.     Const DN_REMOVABLE = &H4000
    14.     Dim strDeviceInstance As String
    15.     Dim lngDevice As Long
    16.     Dim lngStatus As Long
    17.     Dim lngProblem As Long
    18.     Dim lngVetoType As Long
    19.     Dim strVeto As String * 255
    20.    
    21.     pstrDrive = UCase$(Left$(pstrDrive, 1)) & ":"
    22.     strDeviceInstance = StrConv(GetDeviceInstance(pstrDrive), vbFromUnicode)
    23.     If CM_Locate_DevNodeA(lngDevice, StrPtr(strDeviceInstance), 0) = 0 Then
    24.         If CM_Get_DevNode_Status(lngStatus, lngProblem, lngDevice, 0) = 0 Then
    25.             Do While Not (lngStatus And DN_REMOVABLE) > 0
    26.                 If CM_Get_Parent(lngDevice, lngDevice, 0) <> 0 Then Exit Do
    27.                 If CM_Get_DevNode_Status(lngStatus, lngProblem, lngDevice, 0) <> 0 Then Exit Do
    28.             Loop
    29.             If (lngStatus And DN_REMOVABLE) > 0 Then SafelyRemove = (CM_Request_Device_EjectA(lngDevice, lngVetoType, StrPtr(strVeto), 255, 0) = 0)
    30.         End If
    31.     End If
    32. End Function
    33.  
    34. Private Function GetDeviceInstance(pstrDrive As String) As String
    35.     Const HKEY_LOCAL_MACHINE = &H80000002
    36.     Const KEY_QUERY_VALUE = &H1
    37.     Const REG_BINARY = &H3
    38.     Const ERROR_SUCCESS = 0&
    39.     Dim strKey As String
    40.     Dim strValue As String
    41.     Dim lngHandle As Long
    42.     Dim lngType As Long
    43.     Dim strBuffer As String
    44.     Dim lngLen As Long
    45.     Dim bytArray() As Byte
    46.    
    47.     strKey = "SYSTEM\MountedDevices"
    48.     strValue = "\DosDevices\" & pstrDrive
    49.     If RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0&, KEY_QUERY_VALUE, lngHandle) = ERROR_SUCCESS Then
    50.         If RegQueryValueEx(lngHandle, strValue, 0&, lngType, 0&, lngLen) = 234 Then
    51.             If lngType = REG_BINARY Then
    52.                 strBuffer = Space$(lngLen)
    53.                 If RegQueryValueEx(lngHandle, strValue, 0&, 0&, ByVal strBuffer, lngLen) = ERROR_SUCCESS Then
    54.                     If lngLen > 0 Then
    55.                         ReDim bytArray(lngLen - 1)
    56.                         bytArray = Left$(strBuffer, lngLen)
    57.                         strBuffer = StrConv(bytArray, vbFromUnicode)
    58.                         Erase bytArray
    59.                         If Left$(strBuffer, 4) = "\??\" Then
    60.                             strBuffer = Mid$(strBuffer, 5, InStr(1, strBuffer, "{") - 6)
    61.                             GetDeviceInstance = Replace(strBuffer, "#", "\")
    62.                         End If
    63.                     End If
    64.                 End If
    65.             End If
    66.         End If
    67.         RegCloseKey lngHandle
    68.     End If
    69. End Function
    Sample usage:
    Code:
    If SafelyRemove("F:") Then
        MsgBox "Safe to remove", vbInformation, "Notice"
    End If
    Last edited by Ellis Dee; Jun 22nd, 2008 at 11:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width