Solved

How to Find USB device serial Number in VB.NET

Posted on 2009-07-15
6
7,891 Views
Last Modified: 2013-11-07
I have found a VB code to find the serial number of an USB device at http://omnibus.uni-freiburg.de/~albers/snippets/usbinfo10.zip. I have not tried it in VB as I want to use the code in VB.NET. However converting it in VB.NET, I had to change many of things in it. Some how I managed to resolve all the complier errors. But executing the application gives runtime error. "arithmetic operation result in an overflow" at the place shown in the following extract of code. Possibly it may be overflow caused by constant value of "&HC30C" assigned to a SHORT datatype.
So, whether the "&HC30C" value is correct ?
whether VB.NET compitible code of USBINFO10 is available ?
Can experts help me to convert USBINFO10 code in to VB.NET ?
Private Structure typGUID ' 10h/16 bytes, no paddings
        Public lData1 As Integer ' +00-03:  DWORD
        Public iData2 As Short  ' +04-05:  WORD
        Public iData3 As Short ' +06-07:  WORD
        <VBFixedString(8), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=8)> Public abData4() As Byte		
    End Structure
.....
..... 
GUID_CLASS_USBHUB = DEFINE_GUID(&HF18A0E88, &HC30C, &H11D0, &H88, &H15, &H0, &HA0, &HC9, &H6, &HBE, &HD8)
....
.....
Private Function DEFINE_GUID(ByVal lData1 As Integer, ByVal iData2 As Integer, ByVal iData3 As Short, ByVal bData4_1 As Byte, ByVal bData4_2 As Byte, ByVal bData4_3 As Byte, ByVal bData4_4 As Byte, ByVal bData4_5 As Byte, ByVal bData4_6 As Byte, ByVal bData4_7 As Byte, ByVal bData4_8 As Byte) As typGUID
 
        Dim GUIDTMP As typGUID
 
        'following 1 line added to resolve null pointer error 
        GUIDTMP.abData4 = New Byte() {0, 0, 0, 0, 0, 0, 0, 0}
 
        With GUIDTMP
            .lData1 = lData1
            .iData2 = iData2    ' <-----  geting overflow error here
            .iData3 = iData3
            .abData4(0) = bData4_1
            .abData4(1) = bData4_2
            .abData4(2) = bData4_3
            .abData4(3) = bData4_4
            .abData4(4) = bData4_5
            .abData4(5) = bData4_6
            .abData4(6) = bData4_7
            .abData4(7) = bData4_8
        End With
 
               DEFINE_GUID = GUIDTMP
 
    End Function

Open in new window

0
Comment
Question by:kkp01
  • 2
6 Comments
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24864850
Try the code below from

http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/a0baf62e-124d-482f-9cce-5ee9f0aced99/
    Private Function GetVolumeSerial(ByVal DriveLetter As String) As String
 
        'Check for valid drive letter argument. 
        Dim ValidDriveLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        If ValidDriveLetters.IndexOf(DriveLetter) <> -1 Then
            If DriveLetter.Length = 1 Then
                Dim Disk As New System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=""" & DriveLetter & ":""")
                Dim DiskProperty As System.Management.PropertyData
                For Each DiskProperty In Disk.Properties
                    If DiskProperty.Name = "VolumeSerialNumber" Then
                        Return DiskProperty.Value.ToString  '.ToString 'Return the volume serial number. 
                    End If
                Next DiskProperty
            End If
        End If
        Return Nothing 'Invalid drive letter.
    End Function

Open in new window

0
 

Author Comment

by:kkp01
ID: 24866645
No VOLUME serial number needed. It gets changed by formatting the disk / usb drive.
I am concerned about Hardware/manufacturer serial number that exist in HDDs and USB drives..
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 24916475
Try this (converted from C# code):

            Dim serialNumber As String = UsbDriveHelper.GetSerialNumberFromDriveLetter("E:")



' Add a reference to the System.Management.dll to the project. 
 
' Source: 
' http://downloads.cfdan.com/csharp/usbserialnumber.cs.txt 
 
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Management
 
Friend Class UsbDriveHelper
 
    Public Shared Function GetSerialNumberFromDriveLetter(ByVal driveLetter As String) As String
        If Not driveLetter.EndsWith(":") Then
            driveLetter += ":"
        End If
 
        Return matchDriveLetterWithSerial(driveLetter)
    End Function
 
    Private Shared Function matchDriveLetterWithSerial(ByVal driveLetter As String) As String
        Dim serialNumber As String = ""
        Dim searcher1 As New ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition")
        For Each dm As ManagementObject In searcher1.[Get]()
            Dim diskArray As String() = Nothing
            Dim currentDrive As String = getValueInQuotes(dm("Dependent").ToString())
            diskArray = getValueInQuotes(dm("Antecedent").ToString()).Split(","c)
            Dim driveNumber As String = diskArray(0).Remove(0, 6).Trim()
            If currentDrive = driveLetter Then
                ' This is where we get the drive serial 
 
                Dim disks As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
                For Each disk As ManagementObject In disks.[Get]()
 
                    If disk("Name").ToString() = ("\\.\PHYSICALDRIVE" & driveNumber) And disk("InterfaceType").ToString() = "USB" Then
                        serialNumber = parseSerialFromDeviceID(disk("PNPDeviceID").ToString())
                    End If
                Next
            End If
        Next
        Return serialNumber
    End Function
 
    Private Shared Function parseSerialFromDeviceID(ByVal deviceId As String) As String
        Dim splitDeviceId As String() = deviceId.Split("\"c)
        Dim arrayLen As Integer = splitDeviceId.Length - 1
 
        Dim serialArray As String() = splitDeviceId(arrayLen).Split("&"c)
 
        Return serialArray(0)
    End Function
 
    Private Shared Function getValueInQuotes(ByVal inValue As String) As String
        Dim parsedValue As String = ""
 
        Dim posFoundStart As Integer = 0
        Dim posFoundEnd As Integer = 0
 
        posFoundStart = inValue.IndexOf("""")
        posFoundEnd = inValue.IndexOf("""", posFoundStart + 1)
 
        parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1)
 
        Return parsedValue
    End Function
 
End Class

Open in new window

0
 

Author Closing Comment

by:kkp01
ID: 31604026
you are Genius indeed. Right on target. As I got a required solution, the question tend to end. I would still desire common code to find HDD, and USB manufacturer's serial number.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 20
Where to download and how to install sqldmo.dll 5 81
vb.net 1 month apart 11 26
location of a form 2 10
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question