Get Physical Drive Serial Number – Part 2

The previous article shows how to get manufacturer-provided serial number for a physical drive by calling DeviceIoControl function.
Now, let’s see how can it be made by using WMI (Windows Management Instrumentation).

Get serial number by using Win32_PhysicalMedia WMI class

To get the physical drive serial number by using Win32_PhysicalMedia class, follow these steps:

  1. Initialize COM.
  2. Set the default process security level.
  3. Create a connection to WMI namespace.
  4. Set the security levels on WMI connection.
  5. Execute a WQL (WMI Query Language) query to get a list of physical media. Each list element contains a tag as unique identifier (e.g. PHYSICALDRIVE0)  and the manufacturer-provided serial number.
  6. Get each enumerator element until find the desired physical drive. For detailed code, see the complete demo application, below.

Putting all together with some helpful ATL stuff, we can make now a simple console demo application.

See also

Resources

Later edit

We can simplify a little bit the above example by adding WHERE clause in the WQL query.

2 thoughts on “Get Physical Drive Serial Number – Part 2

  1. Bogdan Musat

    Frumos, foarte frumos. Bina ca mai sunt oameni care stiu ATL, ca sa nu mai zic ca impresionant de mic este si numarul celor care stiu ce este ala WMI.
    Apropo WMI, apelurile merg bine din scripting si din C++, ca din .NET sunt multe dude pe zona marshaling la care iese cu virgula si crapa.

    Reply
  2. VictoryCoorg@hotmail.com

    I followed your code. On my machine, the serial number is coming out to be some weird long string “2020202057202d….30337″. Also, the serial number returned by WMI is the same for both the hard disk and CD drive. My OS: Windows 7.

    Reply

Leave a Reply