Posts Revenge Of DriveInfo (Drive Serial Numbers)
Post
Cancel

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 ;-)

This post is licensed under CC BY 4.0 by the author.