Solved

How to Find USB device serial Number in VB.NET

Posted on 2009-07-15
6
7,711 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now