Jump to content

How to retrieve the device name which is connected via USB in windows7


Recommended Posts

An example to extract any USB's label (name). 

 

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Local $arDrives = DriveGetDrive("ALL"), $USB = Null

For $i = 1 to $arDrives[0]
   If (DriveGetType($arDrives[$i], $DT_BUSTYPE) = "USB") Then
      $USB = DriveGetLabel($arDrives[$i])
      MsgBox($MB_OK, "USB", "Drive: " & $arDrives[$i] & @CRLF & "Label: " & $USB)
   EndIf
Next

 

Link to comment
Share on other sites

This is an old script (mine? I don't know) which retrieves the letter of an inserted volume, I think you can convert it to volume name with _WinAPI_QueryDosDevice (but I'm not sure it is the right function):

Global Const $WM_DEVICECHANGE = 0x0219, $DBT_DEVICEARRIVAL = 0x8000, $DBT_DEVTYP_VOLUME = 0x00000002
$hWndGUI = GUICreate("GUI")
GUISetState(@SW_HIDE)
GUIRegisterMsg($WM_DEVICECHANGE, "WM_DEVICECHANGE")

While 1
    Sleep(1000)
WEnd

Func WM_DEVICECHANGE($hWndGUI, $MsgID, $WParam, $LParam)
    If $WParam = $DBT_DEVICEARRIVAL Then
        $s_DEV_BROADCAST_HDR = DllStructCreate("DWORD;DWORD;DWORD", $LParam)
        If DllStructGetData($s_DEV_BROADCAST_HDR, 2) = $DBT_DEVTYP_VOLUME Then
            $s_DEV_BROADCAST_VOLUME = DllStructCreate("DWORD;DWORD;DWORD;DWORD;WORD", $LParam)
            If DllStructGetData($s_DEV_BROADCAST_VOLUME, 5) = 0 Then
                $dbcv_unitmask = DllStructGetData($s_DEV_BROADCAST_VOLUME, 4)
                Return _DriveLetter($dbcv_unitmask)
            EndIf
        EndIf
    EndIf
EndFunc

Func _DriveLetter($dbcv_unitmask)
    Local $AZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", $letter, $counter = 1
    While $dbcv_unitmask > 1
        $dbcv_unitmask /= 2
        $counter += 1
    WEnd
    If $counter <= StringLen($AZ) Then
        $letter = StringMid($AZ, $counter, 1)
    Else
        $letter = "?"
    EndIf
    Return $letter
EndFunc

 

Edited by j0kky
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...