Problem getting HDD C:\ Drive Serial -- Not as easy a fix as you might think!
Posted on 2005-03-02
Okay, a bunch of you are in here thinking you're going to get a quick 500 points... Not so quick!
Here is the Problem: I have been using the code below in a client's application. The purpose of the code is to get the C:\ Drive's serial number to base a unique ID off of. How we use the Unique ID is not the issue. The issue is that approximately .01 percent of the installs return not "ESB-4321" or whatever the serial might be, but instead return only "0". Now, if this happened every time I would know I did something wrong -- but it only happens in 2-3 out of 1,000 installs. So, what could be causing this? If the case is that some systems are unable to return HDD serial, then the problem is not with the code. But if all systems absolutely return HDD serial, then the problem is in the code, but would be hard to pinpoint since it is so anomalous.
Here is the code I am using:
Public Function GetSerialNumber() As String
Dim Serial As Long, VName As String, FSName As String, TempSerial As String
VName = String$(255, Chr$(0))
FSName = String$(255, Chr$(0))
'Get the volume information -- yes, I already declared this function in the module
GetVolumeInformation "C:\", VName, 255, Serial, 0, 0, FSName, 255
'Strip the extra chr$(0)'s
VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
TempSerial = Trim(Str$(Serial))
GetSerialNumber = TempSerial
It has also been suggested that I use the FileSystemObject to get the HDD serial, like this (not my code):
Dim fso As New FileSystemObject
Dim drv As Drive
Set drv = fso.GetDrive(fso.GetDriveName("c:"))
MsgBox "Serial number of c: " & drv.SerialNumber
The problem is, if I don't figure out what the problem is with my original code, then I can't assure the client that the problem is fixed just because I used a whole new untested code.