Get serial num of removable usb harddrive in vb6

2.1k views Asked by At

Am trying to get the manufacturer serial number (not volume number) of usb external harddrives or disk?

[EDIT] i don't know or have any code yet on how to do this. the previous method i tried only returned the volume serial number

1

There are 1 answers

0
Cody Gray - on strike On BEST ANSWER

You can use WMI to retrieve this information. Hard drive serial numbers are in Win32_PhysicalMedia. I'm not going to take the time to write the code here; if you have any experience querying WMI in VB 6, you should be able to do it without much trouble. Otherwise, search for sample code online. You won't find much of anything specifically about hard drive serial numbers, but you'll find lots of WMI examples.

Be weary of the fact that you won't always get the serial number in the format you're expecting. For example, comments to this article indicate you might get something like:

Serial No. : 4a3532544e464137202020202020202020202020

in which case, you will have to decode the serial number:

  1. By converting to hexdecimal bytes, we get the following (0x20 is blank character and is trimmed out):

    0x4a, 0x35, 0x32, 0x54, 0x4e, 0x46, 0x41, 0x37
    
  2. Swapping the odd and even bytes gets the following:

    0x35, 0x4a, 0x54, 0x32, 0x46, 0x4e, 0x37, 0x41
    
  3. The above ASCII codes are equal to the serial number string:

    "5JT2FN7A"
    

I'm also not positive that all external/removable hard drives provide this information. Your mileage may vary, but the suggested method does work fine on internal hard drives.


Alternatively, it appears that you can do this using low-level Windows APIs like DeviceIOControl. You'll need to add declarations for the necessary functions to a module in your VB 6 app. This article on Code Project should help get you started; the code is written in native C++ and it's geared for consumption by .NET languages like C#, but I see no difficulty in adapting the code to VB 6.