locked
Get serial number of USB Drive (correctly) RRS feed

  • Pregunta

  • Hi

    Need to be able to retrieve serial number of USB hard drives and flash drives

    Tried various different methods as a test (not all VB related I know, but good for testing)

    Problem is that a lot of them seem to show wrong values

    For example if I look in properties of drive in Device Manager I can see serial number of drive is:-

    0700079716000026

    But if I use WMI (just quickly from command line using "wmic diskdrive get serialnumber" for now OR using say WMI Explorer) it shows it as:-

    0700079716000020  e.g. last digit different?

    Powershell command of "get-disk | select serialnumber"

    Also shows it as 0700079716000020

    Getting it from USB controller using something like below:-

    gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Where-Object {($_.Description -like '*m
    ass*')} | Sort Description,DeviceID | ft Description,DeviceID –auto

    Seems to get correct? value of 0700079716000026

    Please can someone through some light on why different methods change last digit and perhaps offer some ideas on accurate way to get serial number from VB.NET app

    Many thanks


    Darren Rose

    martes, 30 de enero de 2018 21:24

Todas las respuestas

  • VolumeSerialNumber from Win32_LogicalDisk works for me.

    From WMI Code Creator :

    Try
        Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_LogicalDisk")
        For Each queryObj As ManagementObject In searcher.Get()
            Console.WriteLine("Caption: {0}", queryObj("Caption"))
            Console.WriteLine("VolumeSerialNumber: {0}", queryObj("VolumeSerialNumber"))
        Next
    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try

    martes, 30 de enero de 2018 21:43
  • That's gives the volume serial number of the drive NOT the hardware serial number - two completely different things unfortunately - the volume serial number is allocated when device is partitioned and formatted and changes each time you format it

    Darren Rose

    martes, 30 de enero de 2018 21:46
  • That's gives the volume serial number of the drive NOT the hardware serial number - two completely different things unfortunately - the volume serial number is allocated when device is partitioned and formatted and changes each time you format it

    Darren Rose

    Then SerialNumber from  Win32_DiskDrive, which is called by wmic

    martes, 30 de enero de 2018 21:59
  • Which as per my original post I had already tried using "wmic diskdrive get serialnumber"

    And it was returning 0700079716000020 and not 0700079716000026 which is correct serial number of drive e.g. last digit wrong


    Darren Rose

    martes, 30 de enero de 2018 22:02
  • And it was returning 0700079716000020 and not 0700079716000026 which is correct serial number of drive e.g. last digit wrong

    If your VB application is returnng incorrect data for the serial number of the device then you need to show the code that you are using, not just fragments.

    martes, 30 de enero de 2018 22:28
  • And it was returning 0700079716000020 and not 0700079716000026 which is correct serial number of drive e.g. last digit wrong

    If your VB application is returnng incorrect data for the serial number of the device then you need to show the code that you are using, not just fragments.

    Sorry thought I had explained it clearly enough in first post but obviously not - for testing I have just been running the commands directly from command line - as no point coding in VB.NET until I work out why getting different results

    So in simple steps

    1) If I run wmic diskdrive get serialnumber or look in tool such as WMI Explorer under win32_diskdrive it shows as :-

    0700079716000020 

    2) If I use powershell e.g. Powershell command of "get-disk | select serialnumber"

    It shows as

    0700079716000020 

    3) If I read serial number from the packaging the USB pen arrived in OR look in device manager it shows as:-

    0700079716000026

    So to summarize again - the correct one is from step 3 and I know that because it says so on the packaging of the drive! so why is WMI or powershell returning the value different e.g. last digit

    Hope this clears it up

    Once I know why then I want to obtain correct value from within VB.NET


    Darren Rose

    martes, 30 de enero de 2018 22:36
  • On my OS (Windows 10, VB.NET 2015), I get the same values with WMI (Win32_DiskDrive.SerialNumber) as by using more complicated methods like

    DeviceIoControl with IOCTL_STORAGE_QUERY_PROPERTY

    (STORAGE_DEVICE_DESCRIPTOR.SerialNumberOffset)



    • Editado Castorix31 martes, 30 de enero de 2018 22:51
    martes, 30 de enero de 2018 22:50
  • So to summarize again - the correct one is from step 3 and I know that because it says so on the packaging of the drive! so why is WMI or powershell returning the value different e.g. last digit

    You know the correct value - it's the one on the packaging.  You need to show the code you are using to get the value using VB so people could understand why it's wrong.  Based on the example you gave it's probably a problem of rounding/truncation.  If the problem is with the Powershell code you should aks about that in the Powershell forum, but be sure to post the complete powershell statement, not just a fragment.

    martes, 30 de enero de 2018 23:04
  • Yes but I only have packaging for the one drive I am testing with, not the 120 in my company I need to get serial numbers for

    And yes I know it is the correct value from the packaging - hence why confused other methods giving different results - and hence why asking on forum - didn't think it was going to be quite so difficult to ask a question though

    I AM NOT USING VB CODE at moment, I am doing it as explained at least three times above - until I get correct result no point in doing it in VB - as using VB I would just pull it from WMI or similar normally - but clearly it is giving me wrong value - hence asking the question - so clearly nothing to do with rounding is it

    I didn't ask in PowerShell forum as the problem covers WMI and Powershell - but I need to get a method working in VB.NET to retrieve the serial number

    Surely it is not that difficult to understand is it?


    Darren Rose

    martes, 30 de enero de 2018 23:15
  • You don't use VB.NET code and you say it gives wrong value ?!

    Win32_DiskDrive gives right value.

    martes, 30 de enero de 2018 23:25
  • but I need to get a method working in VB.NET to retrieve the serial number

    Then you need to write some VB code.  What is the point in asking for a solution before you have written the code?   No-one can test your code without seeing what it is.

    martes, 30 de enero de 2018 23:28
  • Not that you asked but I think that the manufacturer's are wrong.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    miércoles, 31 de enero de 2018 0:15
  • Below is code I have just used

     Try
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_DiskDrive")
                For Each queryObj As ManagementObject In searcher.Get()
                    Console.WriteLine("Caption: {0}", queryObj("Caption"))
                    Console.WriteLine("VolumeSerialNumber: {0}", queryObj("SerialNumber"))
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try

    Below that are so pretty little pictures showing that what device manager shows has a 6 at the end, WMI in command prompt and VB has 0 at end - the packaging has 6 at end.  Have three other brand new drives same issue - different value shown on packaging and in device mgr than in WMI/VB

    Hence posting in the first place


    Darren Rose

    miércoles, 31 de enero de 2018 0:37
  • but I need to get a method working in VB.NET to retrieve the serial number

    Then you need to write some VB code.  What is the point in asking for a solution before you have written the code?   No-one can test your code without seeing what it is.


    Win32_DiskDrive gives wrong value - as shown in images and code above - that is why I am confused

    Darren Rose

    miércoles, 31 de enero de 2018 0:37
  • Not that you asked but I think that the manufacturer's are wrong.

    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Would be surprised as if so they are wrong 4 boxes with wrong numbers on, and device manager also wrong as well then

    Darren Rose

    miércoles, 31 de enero de 2018 0:38
  • Not that you asked but I think that the manufacturer's are wrong.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Would be surprised as if so they are wrong 4 boxes with wrong numbers on, and device manager also wrong as well then

    Darren Rose

    Well someone is wrong - they're using some method the same as you are.

    What do you want it for anyway? I think you could do better on your own and ignore that number entirely.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    miércoles, 31 de enero de 2018 0:40
  • Well someone is wrong - they're using some method the same as you are.

    What do you want it for anyway? I think you could do better on your own and ignore that number entirely.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Software to allow USB Devices based on serial number  - so need to add serial number of over 120 devices so that only those devices can be used on work computers

    The serial number it works on is the one showing in device manager, which is why I need to find a way of retrieving that number

    I really don't want to plug in 1 at a time and look in device manager for them, so was hoping to code something to get correct value - at the moment everything I do gets me value where last digit is different than what device manager sees

    Is odd why different, but certainly when you plug one in and look in device manager (OR event log) it shows it as same value - but when using Powershell or WMI it shows last digit different


    Darren Rose

    miércoles, 31 de enero de 2018 0:48
  • "A problem well stated is a problem half solved.” - Charles F. Kettering

    Software to allow USB Devices based on serial number  - so need to add serial number of over 120 devices so that only those devices can be used on work computers

    The serial number it works on is the one showing in device manager, which is why I need to find a way of retrieving that number

    I really don't want to plug in 1 at a time and look in device manager for them, so was hoping to code something to get correct value - at the moment everything I do gets me value where last digit is different than what device manager sees

    Is odd why different, but certainly when you plug one in and look in device manager (OR event log) it shows it as same value - but when using Powershell or WMI it shows last digit different


    Darren Rose

    I don't know - I didn't know that media had its own S/N until you said so earlier.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    miércoles, 31 de enero de 2018 0:51
  • Not that you asked but I think that the manufacturer's are wrong.

    They might be adding a check digit which is not part of the ID but is calculated from it.

    miércoles, 31 de enero de 2018 0:54

  • I really don't want to plug in 1 at a time and look in device manager for them, so was hoping to code something to get correct value - at the moment everything I do gets me value where last digit is different than what device manager sees

    Is odd why different, but certainly when you plug one in and look in device manager (OR event log) it shows it as same value - but when using Powershell or WMI it shows last digit different


    Darren Rose

    That is a non-sequitur; you've got to plug it in either way.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    miércoles, 31 de enero de 2018 1:09
  • That is a non-sequitur; you've got to plug it in either way.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    But if I do a tool I can do it remotely in some way. If using device manager then I personally need to plug each one in turn

    Darren Rose

    • Propuesto como respuesta Mr. Monkeyboy miércoles, 31 de enero de 2018 3:56
    • Votado como útil Mr. Monkeyboy miércoles, 31 de enero de 2018 3:56
    miércoles, 31 de enero de 2018 1:22
  • That is a non-sequitur; you've got to plug it in either way.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    But if I do a tool I can do it remotely in some way. If using device manager then I personally need to plug each one in turn

    Darren Rose


    I hope you get your answer soon. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    miércoles, 31 de enero de 2018 1:27
  • Below that are so pretty little pictures showing that what device manager shows has a 6 at the end, WMI in command prompt and VB has 0 at end - the packaging has 6 at end.  Have three other brand new drives same issue - different value shown on packaging and in device mgr than in WMI/VB

    The code you have posted will return the correct hardware serial for the USB drive.  If you are seeing different results using different procedures it is likely that they refer to different devices, or it is a problem you need to take up with the manufacturer. It is not something that you will fix with changes to your code.

    miércoles, 31 de enero de 2018 1:34
  • You can check the real value with DeviceIoControl

    As I said, I get the same thing as WMI  (change G: to your drive letter (I must be Admin for C: drive on my PC)) =>

    Dim nBytesReturned As Integer = 0
    Dim hVolume As IntPtr = IntPtr.Zero
    Dim sDriveName As String = String.Format("\\.\{0}", "G:")
    hVolume = CreateFile(sDriveName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
    If (hVolume <> -1) Then
        Dim DeviceDescriptor As STORAGE_DEVICE_DESCRIPTOR = New STORAGE_DEVICE_DESCRIPTOR()
        Dim PropertyQuery As STORAGE_PROPERTY_QUERY = New STORAGE_PROPERTY_QUERY()
    
        PropertyQuery.PropertyId = STORAGE_PROPERTY_ID.StorageDeviceProperty
        PropertyQuery.QueryType = STORAGE_QUERY_TYPE.PropertyStandardQuery
        Dim nBytesPropertyQuery As Integer = Marshal.SizeOf(PropertyQuery)
        Dim pPropertyQuery As IntPtr = Marshal.AllocHGlobal(nBytesPropertyQuery)
        Marshal.StructureToPtr(PropertyQuery, pPropertyQuery, False)
    
        Dim nBytesDeviceDescriptor As Integer = Marshal.SizeOf(DeviceDescriptor) + 2048
        Dim pDeviceDescriptor As IntPtr = Marshal.AllocHGlobal(nBytesDeviceDescriptor)
    
        Dim bDeviceIoControl As Boolean = DeviceIoControl(hVolume, IOCTL_STORAGE_QUERY_PROPERTY, pPropertyQuery, Marshal.SizeOf(GetType(STORAGE_PROPERTY_QUERY)), pDeviceDescriptor, nBytesDeviceDescriptor, nBytesReturned, Nothing)
        Dim DeviceDescriptorRet As STORAGE_DEVICE_DESCRIPTOR = Marshal.PtrToStructure(pDeviceDescriptor, GetType(STORAGE_DEVICE_DESCRIPTOR))
    
        Dim nSerialNumberOffset As UInteger = DeviceDescriptorRet.SerialNumberOffset
        Dim pSerialNumber As IntPtr = IntPtr.Add(pDeviceDescriptor, CInt(nSerialNumberOffset))
        Dim sSerialNumber As String = Marshal.PtrToStringAnsi(pSerialNumber)
        Console.WriteLine("SerialNumber : {0}", sSerialNumber)
    
        Marshal.FreeHGlobal(pDeviceDescriptor)
        Marshal.FreeHGlobal(pPropertyQuery)
        CloseHandle(hVolume)
    End If

    With declarations :

    Public Const IOCTL_STORAGE_GET_DEVICE_NUMBER As Integer = &H2D1080
    Public Const IOCTL_STORAGE_QUERY_PROPERTY As Integer = &H2D1400
    
    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Friend Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSECURITY_ATTRIBUTES As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
    End Function
    
    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Friend Shared Function CloseHandle(hObject As IntPtr) As Boolean
    End Function
    
    
    Public Const FILE_SHARE_READ As Integer = 1
    Public Const FILE_SHARE_WRITE As Integer = 2
    Public Const FILE_SHARE_DELETE As Integer = 4
    Public Const FILE_ATTRIBUTE_NORMAL As Integer = &H80
    
    Public Const CREATE_NEW As Integer = 1
    Public Const CREATE_ALWAYS As Integer = 2
    Public Const OPEN_EXISTING As Integer = 3
    Public Const OPEN_ALWAYS As Integer = 4
    Public Const TRUNCATE_EXISTING As Integer = 5
    
    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>                                        
        Public Shared Function DeviceIoControl(hDevice As IntPtr,                                                  
                                                dwIoControlCode As Integer,                                        
                                                lpInBuffer As IntPtr,                                              
                                                nInBufferSize As Integer,                                          
                                                lpOutBuffer As IntPtr,                                             
                                                nOutBufferSize As Integer,                                         
                                                ByRef lpBytesReturned As Integer,                                  
                                                ByRef lpOverlapped As System.Threading.NativeOverlapped) As Boolean
        End Function
        
    <StructLayout(LayoutKind.Sequential)>                                                                                                                                                 
    Public Structure STORAGE_DEVICE_DESCRIPTOR                                               
        Public Version As UInteger                                                           
        Public Size As UInteger                                                              
        Public DeviceType As Byte                                                            
        Public DeviceTypeModifier As Byte                                                    
        <MarshalAs(UnmanagedType.I1)>                                                        
        Public RemovableMedia As Boolean                                                     
        <MarshalAs(UnmanagedType.I1)>                                                        
        Public CommandQueueing As Boolean                                                    
        Public VendorIdOffset As UInteger                                                    
        Public ProductIdOffset As UInteger                                                   
        Public ProductRevisionOffset As UInteger                                             
        Public SerialNumberOffset As UInteger                                                
        Public BusType As STORAGE_BUS_TYPE                                                   
        Public RawPropertiesLength As UInteger                                               
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=1)>                                  
        Public RawDeviceProperties As Byte()                                                 
    End Structure                                                                            
                                                                                         
    Public Enum STORAGE_BUS_TYPE                                                             
        BusTypeUnknown = &H0                                                                 
        BusTypeScsi = &H1                                                                    
        BusTypeAtapi = &H2                                                                   
        BusTypeAta = &H3                                                                     
        BusType1394 = &H4                                                                    
        BusTypeSsa = &H5                                                                     
        BusTypeFibre = &H6                                                                   
        BusTypeUsb = &H7                                                                     
        BusTypeRAID = &H8                                                                    
        BusTypeiScsi = &H9                                                                   
        BusTypeSas = &HA                                                                     
        BusTypeSata = &HB                                                                    
        BusTypeSd = &HC                                                                      
        BusTypeMmc = &HD                                                                     
        BusTypeVirtual = &HE                                                                 
        BusTypeFileBackedVirtual = &HF                                                       
        BusTypeSpaces = &H10                                                                 
        BusTypeNvme = &H11                                                                   
        BusTypeSCM = &H12                                                                    
        BusTypeMax = &H13                                                                    
        BusTypeMaxReserved = &H7F                                                            
    End Enum                                                                                 
                                                                                         
    <StructLayout(LayoutKind.Sequential)>                                                    
    Public Structure STORAGE_PROPERTY_QUERY                                                  
        Public PropertyId As STORAGE_PROPERTY_ID                                             
        Public QueryType As STORAGE_QUERY_TYPE                                               
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=1)>                                  
        Public AdditionalParameters As Byte()                                                
    End Structure                                                                            
                                                                                         
    Public Enum STORAGE_PROPERTY_ID                                                          
        StorageDeviceProperty = 0                                                            
        StorageAdapterProperty = 1                                                           
        StorageDeviceIdProperty = 2                                                          
        StorageDeviceUniqueIdProperty = 3                                                    
        StorageDeviceWriteCacheProperty = 4                                                  
        StorageMiniportProperty = 5                                                          
        StorageAccessAlignmentProperty = 6                                                   
        StorageDeviceSeekPenaltyProperty = 7                                                 
        StorageDeviceTrimProperty = 8                                                        
        StorageDeviceWriteAggregationProperty = 9                                            
        StorageDeviceDeviceTelemetryProperty = 10                                            
        StorageDeviceLBProvisioningProperty = 11                                             
        StorageDevicePowerProperty = 12                                                      
        StorageDeviceCopyOffloadProperty = 13                                                
        StorageDeviceResiliencyProperty = 14                                                 
        StorageDeviceMediumProductType = 15                                                  
        StorageAdapterRpmbProperty = 16                                                      
        StorageDeviceIoCapabilityProperty = 48                                               
        StorageAdapterProtocolSpecificProperty = 49                                          
        StorageDeviceProtocolSpecificProperty = 50                                           
        StorageAdapterTemperatureProperty = 51                                               
        StorageDeviceTemperatureProperty = 52                                                
        StorageAdapterPhysicalTopologyProperty = 53                                          
        StorageDevicePhysicalTopologyProperty = 54                                           
        StorageDeviceAttributesProperty = 55                                                 
        StorageDeviceManagementStatus = 56                                                   
        StorageAdapterSerialNumberProperty = 57                                              
        StorageDeviceLocationProperty = 58                                                   
    End Enum                                                                                 
                                                                                         
    Public Enum STORAGE_QUERY_TYPE                                                           
        PropertyStandardQuery = 0 ' Retrieves the descriptor                                 
        PropertyExistsQuery = 1 ' Used To test whether the descriptor Is supported           
        PropertyMaskQuery = 2 ' Used To retrieve a mask Of writeable fields In the descriptor
        PropertyQueryMaxDefined = 3 ' use To validate the value                              
    End Enum                                                                                 

    miércoles, 31 de enero de 2018 1:45
  • The code you have posted will return the correct hardware serial for the USB drive.  If you are seeing different results using different procedures it is likely that they refer to different devices, or it is a problem you need to take up with the manufacturer. It is not something that you will fix with changes to your code.


    Yes I know it is odd - but I have 4 pens here doing same, and they are from 3 different manufacturers which makes it even stranger

    Darren Rose


    • Editado wingers miércoles, 31 de enero de 2018 16:01
    miércoles, 31 de enero de 2018 10:01
  • @Castorix31

    Thanks, will try that method later just to see if different or not


    Darren Rose

    miércoles, 31 de enero de 2018 10:01
  • @Castorix31

    Get error on the line below because GENERIC_READ is not declared

      hVolume = CreateFile(sDriveName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)


    Darren Rose

    miércoles, 31 de enero de 2018 16:59
  • Get error on the line below because GENERIC_READ is not declared

    Sorry, I forgot it, although you can find it in the SDK headers or Google :

    Public Const GENERIC_READ As Integer = &H80000000

    miércoles, 31 de enero de 2018 17:54
  • Get error on the line below because GENERIC_READ is not declared

    Sorry, I forgot it, although you can find it in the SDK headers or Google :

    Public Const GENERIC_READ As Integer = &H80000000


    Thanks - will try it on troublesome pens later and report back - have managed to get some more new ones to try as well

    Darren Rose


    • Editado wingers miércoles, 31 de enero de 2018 17:59
    miércoles, 31 de enero de 2018 17:56
  • Interesting

    Testing first pen:-

    WMI - win32_diskdrive     gives serial number of 0700079716000020

    Castorix31 method gives serial number of 0700079716000021

    Looking in Device Manager and packaging gives serial number of 0700079716000026

    Definitely something about this last digit!!

    Will test more pens shortly


    Darren Rose

    miércoles, 31 de enero de 2018 18:33
  • Second pen:-

    WMI - win32_diskdrive = 07000701792A9AFB5C10

    Castorix31 method = 07000701792A9AFB5C11

    Device Manager and packaging = 07000701792A9AFB5C16

    So seems it is last digit again and some pattern showing e.g. 0 for WMI, 1 for Castorix and 6 for device manager and what shows on packaging


    Darren Rose

    miércoles, 31 de enero de 2018 19:01
  • When I plug a USB flash drive into my system all methods discussed above - Device Manager, WMI, Powershell, low-level DeviceIoControl all show the same hardware serial number
    miércoles, 31 de enero de 2018 19:27
  • Not sure why my results vary so much then

    Third pen:-

    WMI - win32_diskdrive = 097204B17080

    Castorix31 method = 097204B17080

    Device Manager and event viewer = 070B788E976E9241

    So this time completely different value for device manager and event viewer

    Repeated test on other computers and get same values 


    Darren Rose

    miércoles, 31 de enero de 2018 19:32
  • "For example if I look in properties of drive in Device Manager I can see serial number of drive"

    Where is that under properties ?

    Here's what I get from gwmi:

    Description                                 DeviceID
    -----------                                 --------
    USB Attached SCSI (UAS) Mass Storage Device USB\VID_0BC2&PID_AB34\MSFT30NA7ENFFG
    USB Mass Storage Device                     USB\VID_1058&PID_1230\574343344543585544524C4B
    USB Mass Storage Device                     USB\VID_1058&PID_1230\57434334454C454B4E5A3338

    The last two serial numbers are in ASCII(HEX) format

    wmic gives:
    NA7ENFFG
    WCC4ECXUDRLK
    WCC4ELEKNZ38

    I use WMI and it seems to get the proper Serial Number every time.

    I:\ PHYSICALDRIVE5 Seagate Backup+  Desk SCSI Disk Device NA7ENFFG
    K:\ PHYSICALDRIVE7 WD My Book 1230 USB Device WCC4ECXUDRLK
    J:\ PHYSICALDRIVE6 WD My Book 1230 USB Device WCC4ELEKNZ38

    viernes, 2 de febrero de 2018 3:41
  • Hi Devon

    Thanks for results

    a) the results from GWMI you say are in ASCII(HEX) format so I assume if we convert those values somehow they correlate with other results?

    b) in device manager, properties of disk, then details tab, and usually at end of device instance path - same values as you show I think from gwmi above


    Darren Rose

    viernes, 2 de febrero de 2018 10:13
  • Hi Devon

    Thanks for results

    a) the results from GWMI you say are in ASCII(HEX) format so I assume if we convert those values somehow they correlate with other results?

    b) in device manager, properties of disk, then details tab, and usually at end of device instance path - same values as you show I think from gwmi above


    Darren Rose

    Daren,

    I see WMI only able to return a volume serial number, that is of course not the same as printed serial number in the plastic shield of an USB stick.  Maybe you can also test another brand of USB stick, it can of course be that the brand you use sets  a serialnumber in the volume when creating that, slightly different from the number on the plastic. 


    Success Cor


    viernes, 2 de febrero de 2018 10:38
  • Daren,

    I see WMI only able to return a volume serial number, that is of course not the same as printed serial number in the plastic shield of an USB stick.  Maybe you can also test another brand of USB stick, it can of course be that the brand you use sets  a serialnumber in the volume when creating that, slightly different from the number on the plastic. 


    Success Cor


    Depends where you look in WMI - win32_logicaldisk shows the volume serial number, but win32_diskdrive shows serial number of the stick itself (well in most cases!)

    Darren Rose

    viernes, 2 de febrero de 2018 13:33
  • Hi Devon

    Thanks for results

    a) the results from GWMI you say are in ASCII(HEX) format so I assume if we convert those values somehow they correlate with other results?

    b) in device manager, properties of disk, then details tab, and usually at end of device instance path - same values as you show I think from gwmi above


    Darren Rose

    a) I just take two characters at a time and convert from HEX to a character-
    57 43 43 34 45 43 58 55 44 52 4C 4B
    87 67 67
    W  C  C - etc

    I inserted an 8GB USB Stick and got very strange results
    Device Manager - 
    USBSTOR\DISK&VEN_SANDISK&PROD_CRUZER&REV_4.05\0000167EB7733116&0
    WMI(Converted) - L:\ PHYSICALDRIVE8 SanDisk Cruzer USB Device (Null)(Null)~ˑs1(Null)



    Maybe the serial number actually is "0000167EB7733116" and I need to alter my conversion function (make sure each pair is an ASCII number or letter)

    Edit - The USB stick only shows these numbers on the case:
    BI0801ANJB and
    SDCZ6-8192RB
    • Editado Devon_Nullman lunes, 5 de febrero de 2018 6:46 More Info
    lunes, 5 de febrero de 2018 6:37
  • Certainly for a couple of the stick I was using the serial number shown on the packaging is the same number as appears in device manager or as shown when using the GWMI command

    Darren Rose

    lunes, 5 de febrero de 2018 9:55
  • Certainly for a couple of the stick I was using the serial number shown on the packaging is the same number as appears in device manager or as shown when using the GWMI command

    Darren Rose


    USB Flash drive Serial Numbers - "UNIQUE"?

    La vida loca

    lunes, 5 de febrero de 2018 20:08
  • Certainly for a couple of the stick I was using the serial number shown on the packaging is the same number as appears in device manager or as shown when using the GWMI command


    Darren Rose


    USB Flash drive Serial Numbers - "UNIQUE"?

    La vida loca

    Thanks, that is a very interesting article

    From my experience so far with these particular drives, they have all had unique serial number so far(!)


    Darren Rose

    lunes, 5 de febrero de 2018 21:02
  • try the following very simple code, but in c#, try your VB code

    use the namespace System.Management
    add System.Management reference

    the code in C#
            private void button3_Click(object sender, EventArgs e)
            {         
                string serialNumber;
                ManagementObjectSearcher disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
                foreach (ManagementObject disk in disks.Get())
                {

                    if (disk["InterfaceType"].ToString() == "USB")
                    {
                        serialNumber = (disk["PNPDeviceID"].ToString());
                        label1.Text += serialNumber;
                    }
                }
            }

    sábado, 9 de marzo de 2019 11:34
  • Thanks for replying to a forum discussion from over 12 months ago - as discussed many times the value from WMI / Win32_DiskDrive was not of any use - and did not tally with what was shown on product packaging and retrieved by other methods

    Darren Rose

    sábado, 9 de marzo de 2019 12:09
  • I was looking for something else and came across this - Windows has or at least had - an issue with significant digits - meaning anything over 15 was not considered significant and would round to zero. Try not to have your variable as a integer but as a string in VB .NET.
    jueves, 11 de abril de 2019 16:22
  • Does it matter, as long as VB/WMI get it consistently wrong every time the tool will still be able to tell the difference between the 120 you have that are approved and the one in my pocket that is not.
    miércoles, 17 de julio de 2019 19:50