Reputation: 1301
I tried to use wmi to get the hard disk's serial number. What I did is this:
Start ipython and use this way to get serial num:
import wmi
c = wmi.WMI()
for x in c.Win32_PhysicalMedia():
print x
The result is like this:
instance of Win32_PhysicalMedia
{
SerialNumber = "2020202020202020202020205635514d385a5856";
Tag = "\\\\.\\PHYSICALDRIVE0";
};
But my computer is win7, I start ipython with administrator privilege and do the same thing again, but now the result is different:
instance of Win32_PhysicalMedia
{
SerialNumber = " 5VMQZ8VX";
Tag = "\\\\.\\PHYSICALDRIVE0";
};
I guess the second result is more like a correct result. So can some one show me a correct way to get the serial number on windows, include XP, Vista, Win7, Win8?
I found that many people use CreateFileA and DeviceIoControl to get the serial number.
Upvotes: 4
Views: 9907
Reputation: 31
Better late than ever, i hope this help someone else. i took this from i don't know where.
import wmi
c = wmi.WMI()
hddSerialNumber = c.Win32_PhysicalMedia()[0].wmi_property('SerialNumber').value.strip()
print(hddSerialNumber)
Upvotes: 3
Reputation: 35269
>>> import binascii
>>> binascii.a2b_hex("2020202020202020202020205635514d385a5856")
' V5QM8ZXV'
alternate characters are swapped... looks like they are probably the same serial number.
Upvotes: 0
Reputation: 249123
If you Google "Win32_PhysicalMedia", the second hit is a bug report which now that I read everything more carefully looks like it describes exactly your problem (but offers no fix from what I see): http://connect.microsoft.com/VisualStudio/feedback/details/623282/win32-physicalmedia-returns-incorrect-serial-number-on-vista-or-higher-when-run-as-standard-user
So you may have to take matters into your own hands. This is what I wrote in this answer originally:
Look at those two strings:
2020202020202020202020205635514d385a5856
5VMQZ8VX (yes there's a space in front)
Notice that the first appears to be a hexadecimal number. 0x20 is a space character. So the first one has many spaces followed by a few bytes of real data, which makes the two serial numbers comparable in size.
Now, use a Hex-to-ASCII converter like http://www.dolcevie.com/js/converter.html and punch in the first number. You get:
V5QM8ZXV
See how similar that looks? The only difference now is byte ordering (endianness).
To settle this once and for all, you need to tell us which of the three you think is the "proper" representation of the serial number (ideally it will match what's printed on the drive). It will be simple enough to convert between the three representations once you figure out where you are (depends on the platform apparently--possibly on the version of Python, WMI, or the Python WMI module you're using).
Upvotes: 4