I have code to retrieve the serial number of a hard drive from 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 got the correct serial number. However, trying to use it in a comparison, I found out that the WMI data is not quite correct. The WMI serial number is complemented by a bunch of spaces, and characters are transposed.
The actual drive serial number printed on the label and returned by some tools (possibly using DeviceIoControl) is "3RH8B1BG", WMI, however, returns "; R38H1BGB".
Real Serial Number: 3RH8B1BG
WMI Serial #: R38H1BGB
Some tools, such as SiSoftware Sandra, return this filled and transposed number, but not the actual serial number. The WMI value is the serial number if you transfer each other item. This is normal? should i just copy it to the correct value?
I try to avoid using WMI, but it seems that any search for how to do something on the network now returns WMI examples.
The serial number of the WMI value for 2 different hard drives of different manufacturers is transferred so that it does not have one drive.
Update: Found code using DeviceIoControl
http://addressof.com/blog/archive/2004/02/14/392.aspx
Surprisingly, DeviceIoControl also returns the transposed serial number. In the CorySmith code 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 a flip:
Guess what the standard for DeviceIoControl and WMI is, I donβt believe that any other solutions or examples that I came across did not have this.