I have been planning to write on that topic since October, but never got the time since I was pretty busy with the releases of MetaSAN and stoneGate. Now during the holidays I finally have time to post.
The issue that I came upon was (at the beginning) looking quite simple – get the serial number of a locally attached hard disk. My first approach was to simply use IOCTL_STORAGE_QUERY_PROPERTY and hope that Microsoft did all the work for me. Unfortunately this was behaving strange. On half the hard disks in our lab it would succeed and return the correct disk serial, on others it will fail (SerialNumberOffset being zero).
Ok, I think to myself, someone must have had the same problem – lets ask google. Soon I figured that 90% of my searches were ending into a commercially sold DLL named GetDiskSerial. Someone had solved the problem, and was selling the solution. By that time I had enough budget for a $19 toolkit, but could not accept that something I thought should be a simple API/IOCTL call should cost money at all. I downloaded an eval, it worked fine on all the machines I had at my disposal.
I did not give up, and after some more hours I found a utility called diskid32 and downloaded it. It did not work, but I found that the author published the source here. I extracted the part that was doing the IDENTIFY, compiled it and it worked fine for me. I did not figure out why their executable failed.
Anyway, here is a free alternative to the GetDiskSerial DLL – getdiskserial.zip.
The source has two functions with identical interfaces – GetDiskSerial1 and GetDiskSerial2. The first is using the MS IOCTL, the second – the method by Lynn McGuire (diskid32). You can easily make a super-function of the two, to always get the disk serial.
I am not claiming the source is my creation – I just wanted to share my experience and give you the result I came up with – simply combining the two approaches and making the interfaces/results match (the diskid32 returns the serial as text, while the MS IOCTL returns it as a string containing the hexadecimal values of the ASCII code of the disk serial).
I still think that the commercial DLL has some benefits – it has versions for various languages and development platforms and probably works with a wider variety of disks. I have not yet found a disk that neither IOCTL_STORAGE_QUERY_PROPERTY nor diskid32 will work on, but if you find one – drop me a line please.