I have some code to fetch a hard drive serial number from the WMI.
SelectQuery selectQuery = new SelectQuery("Win32_PhysicalMedia");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(selectQuery);
foreach (ManagementObject wmi_PM in searcher.Get())
{
string str = wmi_PM["SerialNumber"];
}
At first I thought it was working and retrieved the correct serial number. After trying to use it with a comparison though, I found out the number the WMI reports is not exactly correct. The WMI serial number is padded with a bunch of spaces as well as the characters are transposed.
The actual drive serial number printed on the sticker and returned by some tools (probably using DeviceIoControl) is "3RH8B1BG", WMI however returns " R38H1BGB".
Real Serial#: 3RH8B1BG
WMI Serial#: R38H1BGB
Some tools like SiSoftware Sandra, return this padded and transposed number, its not actual serial number though. The WMI value is the serial number if you transpose every other position. Is this normal? should I just code to transpose it to the correct value?
I try to avoid using the WMI but it seems any search for how to do something on the net now bring back WMI examples.
The WMI value serial number for 2 different hard drive of different manufactures are both transposed so its not a single disk.
Update: found some code using DeviceIoControl
http://addressof.com/blog/archive/2004/02/14/392.aspx
Surprisingly, DeviceIoControl returns a transposed serial number as well.
In the code by CorySmith above it has a SwapChars function
Private Shared Function SwapChars(ByVal chars() As Char) As String
For i As Integer = 0 To chars.Length - 2 Step 2
chars.Reverse(chars, i, 2)
Next
Return New String(chars).Trim
End Function
The c++ code he mentions has the flip to:
// function to decode the serial numbers of IDE hard drives
// using the IOCTL_STORAGE_QUERY_PROPERTY command
char * flipAndCodeBytes (const char * str,
int pos,
int flip,
char * buf)
{
...
}
guess that's standard for DeviceIoControl and WMI, cant believe any of the other solutions or examples i ran across did not have this.