Results 1 to 8 of 8

Thread: Help about safely removing USB flash drives

  1. #1

    Thread Starter
    Addicted Member bulletrick's Avatar
    Join Date
    Jul 2005
    Location
    Philippines
    Posts
    248

    Question Help about safely removing USB flash drives

    Hi. I am developing a system that uses a computer that has a USB port as input with no keyboard/mouse. After the program scanned the flash drive, the user will now remove it. How can I duplicate the action of clicking the "Safely Remove Hardware" in my program?

    Thanks a lot!

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Help about safely removing USB flash drives

    As soon as activity ceases, you can show a messagebox telling them to remove the device. This was discussed before in the General section. As long as it isn't reading or writing data, it can be removed.

  3. #3

    Thread Starter
    Addicted Member bulletrick's Avatar
    Join Date
    Jul 2005
    Location
    Philippines
    Posts
    248

    Re: Help about safely removing USB flash drives

    Thanks a lot! Can you give me a link on that discussion? Thanks!

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Help about safely removing USB flash drives

    I was wrong. It was right in this forum a few months ago.

    http://www.vbforums.com/showthread.p...ght=remove+usb

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Help about safely removing USB flash drives

    Bear in mind, however, if it is pre Windows XP, simply pulling the drive out will cause an "Unsafe removal of hardware" message to be shown. And I don't know myself how you would unmount the device programmatically.

  6. #6
    Lively Member rm_03's Avatar
    Join Date
    Aug 2004
    Posts
    92

    Re: Help about safely removing USB flash drives


  7. #7

    Thread Starter
    Addicted Member bulletrick's Avatar
    Join Date
    Jul 2005
    Location
    Philippines
    Posts
    248

    Talking Re: Help about safely removing USB flash drives

    Quote Originally Posted by rm_03
    Ok. now, who can translate that?

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Help about safely removing USB flash drives

    Quote Originally Posted by bulletrick
    Ok. now, who can translate that?
    Babelfish did a fine job.

    This is all the code you need:
    Code:
    Option Explicit
    
    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
    Private Declare Function CM_Get_Parent Lib "setupapi.dll" (hParentDevice As Long, ByVal hDevice As Long, ByVal dwFlags As Long) As Long
    Private Declare Function CM_Locate_DevNodeA Lib "setupapi.dll" (hDevice As Long, ByVal lpDeviceName As Long, ByVal dwFlags As Long) As Long
    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
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    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
    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
    
    ' Safely remove USB flash drive
    Public Function SafelyRemove(ByVal pstrDrive As String) As Boolean
        Const DN_REMOVABLE = &H4000
        Dim strDeviceInstance As String
        Dim lngDevice As Long
        Dim lngStatus As Long
        Dim lngProblem As Long
        Dim lngVetoType As Long
        Dim strVeto As String * 255
        
        pstrDrive = UCase$(Left$(pstrDrive, 1)) & ":"
        strDeviceInstance = StrConv(GetDeviceInstance(pstrDrive), vbFromUnicode)
        If CM_Locate_DevNodeA(lngDevice, StrPtr(strDeviceInstance), 0) = 0 Then
            If CM_Get_DevNode_Status(lngStatus, lngProblem, lngDevice, 0) = 0 Then
                Do While Not (lngStatus And DN_REMOVABLE) > 0
                    If CM_Get_Parent(lngDevice, lngDevice, 0) <> 0 Then Exit Do
                    If CM_Get_DevNode_Status(lngStatus, lngProblem, lngDevice, 0) <> 0 Then Exit Do
                Loop
                If (lngStatus And DN_REMOVABLE) > 0 Then SafelyRemove = (CM_Request_Device_EjectA(lngDevice, lngVetoType, StrPtr(strVeto), 255, 0) = 0)
            End If
        End If
    End Function
    
    Private Function GetDeviceInstance(pstrDrive As String) As String
        Const HKEY_LOCAL_MACHINE = &H80000002
        Const KEY_QUERY_VALUE = &H1
        Const REG_BINARY = &H3
        Const ERROR_SUCCESS = 0&
        Dim strKey As String
        Dim strValue As String
        Dim lngHandle As Long
        Dim lngType As Long
        Dim strBuffer As String
        Dim lngLen As Long
        Dim bytArray() As Byte
        
        strKey = "SYSTEM\MountedDevices"
        strValue = "\DosDevices\" & pstrDrive
        If RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0&, KEY_QUERY_VALUE, lngHandle) = ERROR_SUCCESS Then
            If RegQueryValueEx(lngHandle, strValue, 0&, lngType, 0&, lngLen) = 234 Then
                If lngType = REG_BINARY Then
                    strBuffer = Space$(lngLen)
                    If RegQueryValueEx(lngHandle, strValue, 0&, 0&, ByVal strBuffer, lngLen) = ERROR_SUCCESS Then
                        If lngLen > 0 Then
                            ReDim bytArray(lngLen - 1)
                            bytArray = Left$(strBuffer, lngLen)
                            strBuffer = StrConv(bytArray, vbFromUnicode)
                            Erase bytArray
                            If Left$(strBuffer, 4) = "\??\" Then
                                strBuffer = Mid$(strBuffer, 5, InStr(1, strBuffer, "{") - 6)
                                GetDeviceInstance = Replace(strBuffer, "#", "\")
                            End If
                        End If
                    End If
                End If
            End If
            RegCloseKey lngHandle
        End If
    End Function
    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