Obtain the serial number of USB key

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Obtain the serial number of USB key

Post by Kwai chang caine »

Hello at all

I have found a great freeware soft, USBDeView, for testing speed of a USB KEY, see all the key connected even one time on your pc, and several other usefull functions 8)

http://www.clubic.com/lancer-le-telecha ... eview.html

And what is my surprise, when i see, that nearly all the USB Key, except the generic key, have a personal serial number :shock:

Gooooood ....i say to me ... for make a style of dongle. :roll:
If the serial number of the key connected is good...my soft make a big smile :D ...otherwise he sticks out his tongue :?

So like usually, i have no idea, where i do search, for obtain this number :oops:
If someone have a way ....

I wish you a good day
ImageThe happiness is a road...
Not a destination
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Obtain the serial number of USB key

Post by Vera »

Hi Kwaï chang caïne

just being so glad to see you're back again :)

.. and besides USBDeView is done by Nir and I love most of his tools - so don't miss to visit his side - and if you like to grab most of the tools at once see his launcher.

cheers ~ Vera

ps: my favorit is RegScanner
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: Obtain the serial number of USB key

Post by Num3 »

Hi Kwai!

We have missed you....

Better take a look at this topic!

http://www.purebasic.fr/english/viewtop ... 17&t=42122 :mrgreen: :mrgreen:
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Obtain the serial number of USB key

Post by Rook Zimbabwe »

Welcome back Kwai man! 8)

This could be extremely useful! You could burn your whole software on to the USB key and encode the serial and then it wouldn't work if it isn't on the key!

EXCELLENT!!!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Obtain the serial number of USB key

Post by Perkin »

Welcome back Kwaï chang caïne

Does either of these help? Is it the sort of thing you're after?

http://www.purebasic.fr/english/viewtop ... 13&t=40483

http://www.purebasic.fr/english/viewtop ... 12&t=37566
%101010 = $2A = 42
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Obtain the serial number of USB key

Post by Kwai chang caine »

One thousand of thanks at all my friends 8)
I am really touched by your kindness, your support and your welcome :oops: 8)

Believe me, you too, you missed me :(
But sometime, the life don't give the choice .... :roll:

Yes the furuncle of the US forum, is back.....just a little bit for the time, but it's back when even :lol:
It's like TERMINATOR said "I will be back",

Image

But do not worry, .....It is always very very difficult to heal of the gonorrhea :oops:

So i take a look at all your link
Again thank you, from my heart, for your answers :oops: 8) 8)

And at the true pleasure to talk another time with you ....my second family 8)
ImageThe happiness is a road...
Not a destination
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Obtain the serial number of USB key

Post by Rook Zimbabwe »

But do not worry, .....It is always very very difficult to heal of the gonorrhea
OK but remember to WASH YOUR HANDS before you post! :wink:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Obtain the serial number of USB key

Post by Kwai chang caine »

:lol: :lol: :lol: :lol:
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Obtain the serial number of USB key

Post by Kwai chang caine »

I have watch the several link, and i don't know for you, but this is not the same number, that i obtain.
Perhaps a conversion to do ??? :roll:

He is strong this nirsoft....where he found this number ??? :roll:
ImageThe happiness is a road...
Not a destination
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 »

Good evening,

For those who would be interested by the USB key serial number, and how to find it.
if you have good knowledge in vb (i think it's vb ), the following link speaks about usb based devices...

http://www.emmet-gray.com/Articles/USB_ ... umbers.htm
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 »

wb Kwaï chang caïne :)
WMI is your friend. Try this code

Code: Select all

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

NewList WMIResult.WMIClass()

If WMIQuery("Select Caption, description, VolumeName, VolumeSerialNumber from Win32_LogicalDisk", WMIResult())
  ForEach WMIResult()
    Debug WMIResult()\Property + "=" + WMIResult()\Value
  Next
EndIf
include file : WMIQuery.pbi

Code: Select all

;WMIQuery Version 1.00
;
;Pure Basic 4.51, 4.61, 5.00 Beta
;
;Contributor : Andre (2004), DataMiner (2005) ..... others who have tried :)
;Update      : falsam (19.09.2012) 
;
;Add
;Procedure WMIQuery(WMIReqSql.s, Map WMIMap.s())
;Run a WMI query and retrieve one or all properties of a class in a map
;
;Example
;NewList WMIResult.WMIClass()

;If WMIQuery("Select Manufacturer, Model, SystemType from Win32_ComputerSystem", WMIResult())
;  ForEach WMIResult()
;    Debug WMIResult()\Property + "=" + WMIResult()\Value
;  Next
;EndIf

;WMI constants
#RPC_C_AUTHN_LEVEL_CONNECT = 2
#RPC_C_IMP_LEVEL_IDENTIFY = 2
#EOAC_NONE = 0
#RPC_C_AUTHN_WINNT = 10
#RPC_C_AUTHZ_NONE = 0
#RPC_C_AUTHN_LEVEL_CALL = 3
#RPC_C_IMP_LEVEL_IMPERSONATE = 3
#CLSCTX_INPROC_SERVER = 1
#WBEMFlagReturnImmediately = 16
#wbemFlagForwardOnly = 32
#WBEM_INFINITE = $FFFFFFFF
#IFlags = #wbemFlagReturnImmediately + #wbemFlagForwardOnly

Structure WMIClass
  Property.s
  Value.s  
EndStructure

Procedure.l ansi2bstr(ansi.s)
  Protected Size.l, i.i, tmp.s
  
  size=MultiByteToWideChar_(#CP_ACP,0,ansi,Len(ansi),0,0)
  Dim unicode.w(size)
  MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi), unicode(), size);#CP_ACP
  For i=0 To size
    tmp + Hex(unicode(i),#PB_Unicode)  
  Next
  
  ProcedureReturn SysAllocString_(@unicode())
EndProcedure

Procedure bstr2string (bstr)
  Static result.s
  result = PeekS(bstr, -1, #PB_Unicode)
  ProcedureReturn @result
EndProcedure

; Interrogate WMI Database
Procedure WMIQuery(WMIReqSql.s, List WMI.WMIClass())
  
  Protected Mem.l
  Protected hres.i
  Protected loc.IWbemLocator
  Protected svc.IWbemServices
  Protected pUnk.IUnknown
  Protected pEnumerator.IEnumWbemClassObject
  Protected pclsObj.IWbemClassObject
  Protected uReturn
  Protected sf.l
  Protected lBound.i, uBound.i, i.i
  Protected temp.l,  x.Variant, Type.i
  Protected Property.s, Val.s
  Protected nDim.i, plUbound.i, z.i, rgVar
  
 
  ClearList(WMI())
  ; --- Step 1: Initialize COM parameters with a call to CoInitializeEx  
  CoInitializeEx_(0, #COINIT_MULTITHREADED)
  
  ; --- Step 2: Initialize COM process security by calling CoInitializeSecurity.
  hres=CoInitializeSecurity_(0, -1,0,0,#RPC_C_AUTHN_LEVEL_CONNECT,#RPC_C_IMP_LEVEL_IDENTIFY,0,#EOAC_NONE,0)
  If hres <> 0
    MessageRequester("ERROR", "unable to call CoInitializeSecurity", #MB_OK)
    CoUninitialize_()
    ProcedureReturn #False
  EndIf
  
  ; --- Step 3: Obtain the initial locator to WMI by calling CoCreateInstance. 
  hres=CoCreateInstance_(?CLSID_WbemLocator,0,#CLSCTX_INPROC_SERVER,?IID_IWbemLocator,@loc.IWbemLocator) 
  If hres <> 0
    MessageRequester("ERROR", "unable to call CoCreateInstance", #MB_OK)
    loc\release()
    CoUninitialize_()
    ProcedureReturn #False
  EndIf
  
  ; --- Step 4: Obtain a pointer to IWbemServices for the root\cimv2 namespace on the local computer by calling IWbemLocator::ConnectServer. 
  ;hres=loc\ConnectServer(Ansi2Uni("root\cimv2"),0,0,0,0,0,0,@svc)
  If #PB_Compiler_Unicode = 1;we create unicode application
    hres=loc\ConnectServer(@"root\cimv2" ,0,0,0,0,0,0,@svc.IWbemServices)
  Else; non unicode
    hres=loc\ConnectServer(ansi2bstr("root\cimv2"),0,0,0,0,0,0,@svc.IWbemServices)  
  EndIf
  If hres <> 0
    MessageRequester("ERROR", "unable to call IWbemLocator::ConnectServer", #MB_OK)
    svc\release()
    loc\release()
    CoUninitialize_()
    ProcedureReturn #False
  EndIf
    
  ; --- Step 5: Set IWbemServices proxy security so the WMI service can impersonate the client by calling CoSetProxyBlanket. 
  hres=svc\queryinterface(?IID_IUnknown,@pUnk.IUnknown) 
  hres=CoSetProxyBlanket_(svc,#RPC_C_AUTHN_WINNT,#RPC_C_AUTHZ_NONE,0,#RPC_C_AUTHN_LEVEL_CALL,#RPC_C_IMP_LEVEL_IMPERSONATE,0,#EOAC_NONE)  
  If hres <> 0
    MessageRequester("ERROR", "unable to call CoSetProxyBlanket", #MB_OK)
    svc\release()
    loc\release()
    CoUninitialize_()
    ProcedureReturn #False
  EndIf
  
  hres=CoSetProxyBlanket_(pUnk,#RPC_C_AUTHN_WINNT,#RPC_C_AUTHZ_NONE,0,#RPC_C_AUTHN_LEVEL_CALL,#RPC_C_IMP_LEVEL_IMPERSONATE,0,#EOAC_NONE)
  If hres <> 0
    MessageRequester("ERROR", "unable to call CoSetProxyBlanket", #MB_OK)
    svc\release()
    loc\release()
    CoUninitialize_()
    ProcedureReturn #False
  EndIf
  
  pUnk\release()
  
  ; --- Step 6: Use the IWbemServices pointer to make requests of WMI.   
  ;hres=svc\ExecQuery(Ansi2Uni("WQL"), Ansi2Uni(WMIReqSql), #IFlags,0,@pEnumerator)
  ;hres=svc\ExecQuery(@"WQL",@WMIobj, #IFlags,0,@pEnumerator.IEnumWbemClassObject) 
  If #PB_Compiler_Unicode = 1
    hres=svc\ExecQuery(@"WQL",@WMIReqSql, #IFlags,0,@pEnumerator.IEnumWbemClassObject)  
  Else
   hres=svc\ExecQuery(ansi2bstr("WQL"),ansi2bstr(WMIReqSql), #IFlags,0,@pEnumerator.IEnumWbemClassObject) 
  EndIf
  
  If hres <> 0
    MessageRequester("ERROR", "unable to call IWbemServices::ExecQuery", #MB_OK)
    svc\Release() 
    loc\Release() 
    pEnumerator\Release() 
    CoUninitialize_() 
    ProcedureReturn #False 
  EndIf
  
  ; --- Step 7: Get the data from the WQL query. The IEnumWbemClassObject pointer is linked to the data objects that the query returned,  
  ; ---            and the data objects can be retrieved with the IEnumWbemClassObject::Next method. This method links the data objects to an IWbemClassObject pointer  
  ; ---            that is passed into the method. Use the IWbemClassObject::Get method to get the desired information from the data objects.  
  mem=AllocateMemory(1000)  
  hres=pEnumerator\reset()   
  
  Repeat
    
    hres = pEnumerator\Next(#WBEM_INFINITE, 1, @pclsObj.IWbemClassObject, @uReturn) 
    If uReturn = 0
     Break
    EndIf
        
    ; get a list with the names of the properties.
    
    hres=pclsObj\GetNames(0, 0, 0,@sf.l); get a list with the names of the properties.
    SafeArrayGetLBound_(sf, 1, @lBound)
    SafeArrayGetUBound_(sf, 1, @uBound)
    Dim indices(1)
    
    For i = lbound To ubound
      indices(0) = i
      SafeArrayGetElement_(sf, @indices(), @temp)
      If temp
         Property = PeekS(temp, -1, #PB_Unicode) 
         If #PB_Compiler_Unicode = 1
            hres=pclsObj\get(@Property, 0, mem, 0, 0)  
         Else
            hres=pclsObj\get(ansi2bstr(Property), 0, mem, 0, 0) 
         EndIf
         
         type=PeekW(mem)  
         
         Select type  
           Case 8  
             val.s=PeekS(bstr2string(PeekL(mem+8)))  
             
           Case 3  
             val.s=Str(PeekL(mem+8))  
             
           Default  
             val.s=""      
         EndSelect
        
         If FindString(WMIReqSql,"*",1) <> 0;we need everything, not just some specific properties
           AddElement(WMI())
           WMI()\Property = Property
           WMI()\Value = val
         ElseIf Left(Property,1)<>"_";we need some specific properties, not all, not these starting with '_'.           
           AddElement(WMI())
           WMI()\Property = Property
           WMI()\Value = val
         EndIf
            
      EndIf
    Next
  Until uReturn = 0
  
  ; --- Step 8: Cleanup 
  svc\release()
  loc\release()
  pEnumerator\release()
  
  If uReturn
    pclsObj\release()
  EndIf 
  
  CoUninitialize_()
  
  
  ProcedureReturn #True
EndProcedure

;- Data Section
DataSection
  CLSID_IEnumWbemClassObject:
  ;1B1CAD8C-2DAB-11D2-B604-00104B703EFD
  Data.l $1B1CAD8C
  Data.w $2DAB, $11D2
  Data.b $B6, $04, $00, $10, $4B, $70, $3E, $FD
  IID_IEnumWbemClassObject:
  ;7C857801-7381-11CF-884D-00AA004B2E24
  Data.l $7C857801
  Data.w $7381, $11CF
  Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
  CLSID_WbemLocator:
  ;4590f811-1d3a-11d0-891f-00aa004b2e24
  Data.l $4590F811
  Data.w $1D3A, $11D0
  Data.b $89, $1F, $00, $AA, $00, $4B, $2E, $24
  IID_IWbemLocator:
  ;dc12a687-737f-11cf-884d-00aa004b2e24
  Data.l $DC12A687
  Data.w $737F, $11CF
  Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
  IID_IUnknown:
  ;00000000-0000-0000-C000-000000000046
  Data.l $00000000
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
EndDataSection

➽ 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
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Obtain the serial number of USB key

Post by Kwai chang caine »

NabDa wrote:if you have good knowledge in vb (i think it's vb ), the following link speaks about usb based devices...
Unfortunately Knowledge and KCC are words that do not go very well together :oops:
It's good VB, but .NET, more strong to use :(
But you have right, it's when even a good begining
Thanks NABDA for this link 8)

@FALSAM
Hi :wink:
Excelent your code, thanks a lot 8)
The USB key are astonishing, pending find something, i use the cool freeware tool USBDeview
With it, you can have numerous information about USB 8)
I don't know where he found the serial number, but it's never the same :shock:
For the same key UsbDeview find 5B6A088E28B6 and your nice code find VolumeSerialNumber=CC6C7F88 :lol:

So thanks a lot for your code, i keep it

@You two
Have a good day 8)
ImageThe happiness is a road...
Not a destination
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 »

@Kwaï chang caïne (KCC) : I give explanations and examples of using WMI on the French forum.
Link : http://www.purebasic.fr/french/viewtopi ... =6&t=13185
See you soon :)

➽ 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
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Obtain the serial number of USB key

Post by Kwai chang caine »

Thanks a lot FALSAM, i go to read it 8)
At the pleasure to read you another time :D
ImageThe happiness is a road...
Not a destination
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Obtain the serial number of USB key

Post by IdeasVacuum »

Code: Select all

PeekL(mem+8)
Is that magic number 8 safe for 32bit and 64bit?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply