Welcome to AddressOf.com Sign in | Join | Help

Revenge Of DriveInfo (Drive Serial Numbers)

Although WMI allows you to gain access to a lot of interesting information, it's obnoxiously slow.  Also, the previous example would only get me the serial numbers for my first two hard drives.  It was now showing the serial number for my third hard drive (which is physically installed on the second controller) or the fourth hard drive (which is installed as a USB drive).  After further investigation, it doesn't appear that you can get the serial number from a USB device.  However, why doesn't WMI give me the serial number for my third drive?

Well, since I'm not happy with the speed (or rather the lack of) that WMI has, I set out to use Win32 Interop to accomplish the same task.  After a ton of searching via Google, I came across that you could use named pipes to gain access to this information on Windows NT based machines.  Well, since that's all I care about (Win9x is dead in my book), this was an acceptable solution.  I found the source code for a C++ console application that does pretty much what I was looking for.  Now came the job of porting it to VB.NET.  After using VS.NET's help, MSDN and Google, I translated all of the API calls and structures.  (Bear in mind, this is not a complete port, just the portion that works with Windows NT based operating systems using named pipes.)  Here is a screen shot of the Revenge Of DriveInfo using nothing but P/Invoke.  As you will see, it's worlds faster than the WMI version, it is setup as a reusable class... and it actually gets the serial number for my third drive ;-)

<>

Download

So for those people where were impressed by the previous example, be sure to let me know what you think of this one ;-)

Published Saturday, February 14, 2004 8:37 PM by CorySmith
Filed under: ,

Comments

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Thursday, July 08, 2004 10:41 AM by ronhts
it does not work on asp.net webforms

createfile always return handle = -1

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Sunday, March 06, 2005 5:49 PM by DS
Works fantastic! Pulled my hard drive and much to my verify it and there sits the same number that was just on my screen. Freaking great!!

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Tuesday, March 22, 2005 11:35 AM by Pin0
Woks very good! Also under Win2000 !!

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Wednesday, March 23, 2005 6:16 AM by UltraSoft3D
It's necessary to modify SwapChars to avoid null terminated string : VB null terminated string concatenation don't work

Private Shared Function SwapChars(ByVal chars() As Char) As String
Dim i%, k%
Dim S$ = ""
k = chars.Length
For i = 0 To k - 2 Step 2
chars.Reverse(chars, i, 2)
Next
For i = 0 To k - 1
If (Asc(chars(i)) = 0) Then Exit For
S &= chars(i)
Next i
Return S.Trim
End Function

# He-He, I also has been hit by null terminated string

Tuesday, July 05, 2005 1:27 PM by flor
' My version
'Modified to resolve WD serial nrs
Private Shared Function SwapChars(ByVal chars() As Char) As String
Dim sb As StringBuilder = New StringBuilder
For i As Integer = 0 To chars.Length - 2 Step 2
chars.Reverse(chars, i, 2)
Next
For i As Integer = 0 To chars.Length - 1 Step 1
If (Not chars(i) = Nothing) Then
sb.Append(chars(i))
End If
Next
Return sb.ToString().Trim()
End Function

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Thursday, October 06, 2005 1:11 PM by Cory Smith
Please see the Gathering computer information using WMI (http://addressof.com/blog/archive/2005/10/06/8486.aspx) example for how to read mother board, cpu and bios information.

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Sunday, January 15, 2006 8:13 AM by fedo
Cannot retrieve info about USB mass storage under WinXP..
CreateFile returns well, but DeviceIOControl returns "Unknow driveType".
Bye

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Friday, December 08, 2006 3:59 AM by Karthik

Does not work for windows 64-bit. Any Help? Thanks.

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Saturday, July 07, 2007 1:42 PM by g b
chars.Reverse is not valid in visual studio 2005 using vb.net

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Monday, July 23, 2007 8:27 AM by Dave Hopkins
Have you tried anything like this on Vista?

# re: Revenge Of DriveInfo (Drive Serial Numbers)

Tuesday, September 25, 2007 11:05 AM by pascal
It seems not working on vista.... Arrrrgh ! Any solution ? I'm not able to built one myself .... help
Anonymous comments are disabled