1. Computing

Retrieving volume's (disk / drive) serial number

Here's a handy piece of Delphi code to read the serial number of a (disk) drive.

From , former About.com Guide

What is Volume's Serial Number?
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;
var
   VolumeSerialNumber : DWORD;
   MaximumComponentLength : DWORD;
   FileSystemFlags : DWORD;
   SerialNumber : string;
begin
   Result:='';

   GetVolumeInformation(
        Drive,
        nil,
        0,
        @VolumeSerialNumber,
        MaximumComponentLength,
        FileSystemFlags,
        nil,
        0) ;
   SerialNumber :=
         IntToHex(HiWord(VolumeSerialNumber), 4) +
         ' - ' +
         IntToHex(LoWord(VolumeSerialNumber), 4) ;

   Result := SerialNumber;
end; (*FindVolumeSerial*)

~~~~~~~~~~~~~~~~~~~~~~~~~

Usage is simple:

~~~~~~~~~~~~~~~~~~~~~~~~~
var
   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.

©2013 About.com. All rights reserved.