Page 2 of 2
Re: Obtain the serial number of USB key
Posted: Sat Sep 22, 2012 7:15 pm
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.
Re: Obtain the serial number of USB key
Posted: Sat Sep 22, 2012 7:42 pm
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 !

Re: Obtain the serial number of USB key
Posted: Sat Sep 22, 2012 8:13 pm
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
Re: Obtain the serial number of USB key
Posted: Sat Sep 22, 2012 9:26 pm
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.
Re: Obtain the serial number of USB key
Posted: Sat Sep 22, 2012 9:36 pm
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.
Re: Obtain the serial number of USB key
Posted: Sat Sep 22, 2012 10:00 pm
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
Re: Obtain the serial number of USB key
Posted: Sat Sep 22, 2012 10:39 pm
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
Re: Obtain the serial number of USB key
Posted: Sun Sep 23, 2012 12:39 am
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.