In short, the serial number of a (logical) drive is generated every time a drive is formatted. When Windows formats a drive, a drive's serial number gets calculated using the current date and time and is stored in the drive's boot sector. (The odds of two disks getting the same number are virtually nil on the same machine.)
Here's a simple Delphi routine that gets the serial number of a disk (not the hard-coded manufacturer's hard drive serial number):
function FindVolumeSerial(const Drive : PChar) : string;
VolumeSerialNumber : DWORD;
MaximumComponentLength : DWORD;
FileSystemFlags : DWORD;
SerialNumber : string;
IntToHex(HiWord(VolumeSerialNumber), 4) +
' - ' +
IntToHex(LoWord(VolumeSerialNumber), 4) ;
Result := SerialNumber;
Usage is simple:
C_DriveSerNumber : string;
C_DriveSerNumber := FindVolumeSerial('c:\') ;
Note: the GetVolumeInformation API function is declared in the Windows unit, hence there's no need to add any additional units in the uses list.
Why would I need this number in my applications?
You could use the volume serial number to enforce a weak form of application protection - create your application so that it refuses to run if the current disk (from where the application is executed) has a volume serial number that was different from the number of the hard disk on which it was first installed. Note, however, that users who upgrade their systems, or who restore from backups after a disk crash will have their volumes with different numbers.