Sign in to follow this  
Followers 0
vikashbitm2010

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

6 posts in this topic

Hello,

Once i connect the hardware through USB, its appearing in the device manager(Refer snapshot attached) -

Hardware name: CANCASEXL

I want to get the name of the hardware. Is it possible to get through AutoIt ?

deviceMGR.PNG

Share this post


Link to post
Share on other sites



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

 

Share this post


Link to post
Share on other sites

I want to have the hardware name which is conneted via USB. Please refer snapshot which I attached.

My requirement is not to get the drive name.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites
On 11/11/2016 at 3:23 AM, JohnOne said:

what if the usb device is not a drive?

I misread OP, or just assumed when reading "USB" they were referring to a thumb drive =\ Apologies. 

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0