Jump to content
Belini

Get serial number from the pendrive. (Solved)

Recommended Posts

Belini
Posted (edited)

I am having problems getting the serial from the pendrive on windows 7 64 bit, can anyone help?

#include <Array.au3>
#include <String.au3>

getUsbSerial()

Func getUsbSerial()
    Local $letter[1], $disk[1], $aArray[1][2], $i, $informat = '', $espaco = ''
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
    If IsObj($objWMIService) Then
        Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", 0x10 + 0x20)
        For $objitem In $colItems
            If $objitem.interfacetype = "USB" And $objitem.DeviceId Then
                For $partition In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $objitem.DeviceId & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
                Next

                If (IsObj($partition) And $partition.DeviceId) Then
                    For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
                    Next

                    $aRegExSer = StringRegExp($objitem.pnpdeviceid, "\x5C([0-9a-fxA-FX]+)&", 3)
                    If IsArray($aRegExSer) Then
                        $i += 1
                        $informat = ""
                        _ArrayAdd($disk, $aRegExSer[0])
                        _ArrayAdd($letter, $loDisk.DeviceId)
                        $espaco = DriveSpaceTotal($letter[$i]) / 1024
                        $espaco = StringMid($espaco, 1, StringInStr($espaco, ".", 2, 1) + 2) & " GB"
                        MsgBox('', '', "Letter = " & $letter[$i] & @CRLF & "Size = " & $espaco & @CRLF & "Serial = " & _
                                $disk[$i] & @CRLF & "Name = " & $objitem.Caption & @CRLF & "ID = " & $objitem.PNPDeviceID)
                    EndIf
                EndIf
            EndIf
        Next
    EndIf
EndFunc   ;==>getUsbSerial

test.jpg

Edited by Belini

Share this post


Link to post
Share on other sites
Danyfirex

I think the device has not Unique ID so It's generate by the system. check here:

It says: Note: If the second character of the unique instance ID is a '&', then the ID was generated by the system, as the device did not have a serial number.

Saludos

  • Like 2

Share this post


Link to post
Share on other sites
Belini

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

×