Obtain the serial number of USB key

Just starting out? Need help? Post your questions and find answers here.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Obtain the serial number of USB key

Post by falsam »

IdeasVacuum wrote:Is that magic number 8 safe for 32bit and 64bit?
For me yes, but I'm still too noob in programming and I prefer to let other people (Pure Basic Mentor) to answer this question.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
NabDa
New User
New User
Posts: 3
Joined: Fri Sep 14, 2012 3:58 pm
Location: France

Re: Obtain the serial number of USB key

Post by NabDa »

Thanks a lot falsam for your WMI Tutorial, before I read it, i had no ideas of how to use WMI at all !!
WMI was an unreachable micro$oft secret for me...
Now , i think that i would be able play with my usb keys and compare the result with usbdeview
but i doubt of the value given as serial number by this software....
I have several keys from the same manufacturer and the vendorID given seems ok ! the modelID too, but not the serial...

Great JOB falsam ! A Great MERCI ! :D
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Obtain the serial number of USB key

Post by RASHAD »

Good shot falsam
But WMI is not always enabled
Beside next is more simple
Tested with PB x86 - PB x64 Win 7 x64

Code: Select all

Global Result,SerialNum.q , a.q , b.q , VolumeName.s
Drives$ = Space(#MAX_PATH)
Result = GetLogicalDriveStrings_(#MAX_PATH,@Drives$)

For x = 0 To Result - 4 Step 4
    Type$ = PeekS(@Drives$+x,3)
    Debug Type$
    If GetDriveType_(Type$) = 0
       Debug "The drive type cannot be determined"
    ElseIf GetDriveType_(Type$) = 2
       Debug "REMOVABLE DRIVE"
    ElseIf GetDriveType_(Type$) = 3
       Debug "FIXED DRIVE	"
    ElseIf GetDriveType_(Type$) = 4
       Debug "REMOTE DRIVE"
    ElseIf GetDriveType_(Type$) = 5
       Debug "CDROM DRIVE"
    ElseIf GetDriveType_(Type$) = 1
       Debug "RAMDISK DRIVE"
    EndIf	
    GetVolumeInformation_(Type$, @VolumeName, Len(VolumeName), @SerialNum, a, b, 0, 0)
    Debug "Serial Num            :"+RSet(Hex(SerialNum),8,"0")
    Debug " "
    SerialNum = 0
Next

Egypt my love
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Obtain the serial number of USB key

Post by falsam »

Hello Rashad. Thank you for your code which in this case is better. The result is the same as the WMI query.

With your code on my computer
C:\
FIXED DRIVE
Serial Num :5CFBA1A6

D:\
FIXED DRIVE
Serial Num :A057F37A

E:\
FIXED DRIVE
Serial Num :44DD6D54

F:\
CDROM DRIVE
Serial Num :00000000

G:\
CDROM DRIVE
Serial Num :3343FF32

H:\
REMOVABLE DRIVE
Serial Num :100C1840

Z:\
REMOTE DRIVE
Serial Num :00000000
Result With WMI
C:
Description=Disque fixe local
VolumeSerialNumber=5CFBA1A6

D:
Description=Disque fixe local
SerialNumber=A057F37A

Caption=E:
Description=Disque fixe local
VolumeSerialNumber=44DD6D54

Caption=F:
Description=Disque CD-ROM
VolumeSerialNumber=

Caption=G:
Description=Disque CD-ROM
VolumeSerialNumber=3343FF32

Caption=H:
Description=Disque amovible
VolumeSerialNumber=100C1840

Caption=Z:
Description=Connexion réseau
VolumeSerialNumber=
This forum is very reactive and ...... I love it :)

@NabDa : Welcome to the forum. I have not seen your speudo on the French forum. I hope to see you also on the French forum. A bientôt. :)

PS : WMI also allows to know information on remote computers.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Obtain the serial number of USB key

Post by jassing »

It should be noted that those are VOLUME ID's -- NOT drive serial #'s.
Volume id's can be changed; and if you format a drive, the volume id will change, however, a drives serial # will not.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Obtain the serial number of USB key

Post by falsam »

jassing wrote:It should be noted that those are VOLUME ID's -- NOT drive serial #'s.
Volume id's can be changed; and if you format a drive, the volume id will change, however, a drives serial # will not.
If you formatted your usb, serial number changes.

with the same usb key.
Caption=G:
Description=Disque amovible
VolumeName=USBDISKPRO
VolumeSerialNumber=04EC52B0
after formatting
Caption=G:
Description=Disque amovible
VolumeName=Data 2
VolumeSerialNumber=8A745A86

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Obtain the serial number of USB key

Post by falsam »

I'm not sure that the process is good, but the result is consistent with those that I find with other software that detects serial numbers. I tested this code with 5 usb.

If possible, test this code and feedback :) Thanks.

Code: Select all

;Usb drive : Read Serial Number 
IncludeFile "WMIQuery.pbi"

Global Buffer.s

NewList WMIResult.WMIClass()

If WMIQuery("SELECT PNPDeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'", WMIResult())
  ForEach WMIResult()
    Debug WMIResult()\Property + "=" + WMIResult()\Value
    Buffer = StringField(WMIResult()\Value, CountString(WMIResult()\Value, "\")+1, "\")
    Debug "Serial number " + Left(Buffer, Len(Buffer)-2)
  Next
EndIf

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
swan
Enthusiast
Enthusiast
Posts: 226
Joined: Sat Jul 03, 2004 9:04 am
Location: Sydney Australia
Contact:

Re: Obtain the serial number of USB key

Post by swan »

Similar using Commate Plus:

Code: Select all

XIncludeFile "COMatePLUS.pbi"

Procedure DD_Info1()

Define.COMateObject objWMIService, DDInfo
colDDInfo.COMateEnumObject
strComputer.s = "." 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colDDInfo = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_USBHub')")
  ;colDDInfo = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_DiskDrive WHERE InterfaceType='USB')")

  If colDDInfo
    DDInfo = colDDInfo\GetNextObject()
    While DDInfo
        
      Debug "Description : " + DDInfo\GetStringProperty("Description")
      Debug "Name : " + DDInfo\GetStringProperty("Name")
      Debug "DeviceID : " +DDInfo\GetStringProperty("PNPDeviceID")

      DDInfo\Release()
      DDInfo = colDDInfo\GetNextObject()
    Wend
    colDDInfo\Release()
  EndIf
  objWMIService\Release()
  Else
      MessageRequester("Error", "DDInfo")  
EndIf

EndProcedure
I remember getting this code from the forum but can't remember who to give credit to, sorry.
Post Reply