Mega Code Archive

 
Categories / Delphi / Examples
 

Detecting cd-rom disc and other disk change using serial numbers

A simple way to find out if a disc (or disk) was changed is to check its volume serial number. For example, you can use the following function to get the volume serial number of a disc. If your CD-ROM drive name is E:, "GetDiskVolSerialID( 'E' )" will return the serial number we're looking for. You can then store this number in your program and compare it to the serial number returned by the next call to "GetDiskVolSerialID()" function. If they are different, you can safely assume that the disc was changed. function GetDiskVolSerialID( cDriveName : char ) : DWord; var dwTemp1, dwTemp2 : DWord; begin GetVolumeInformation( PChar( cDriveName + ':' ), Nil, 0, @Result, dwTemp2, dwTemp2, Nil, 0 ); end; Listing #1 : Delphi code. Download diskvol (0.29 KB). If need to display the serial number as it's displayed by most original DOS and Windows programs, simply convert the number returned by "GetDiskVolSerialID()" to a hex string: MessageDlg( 'Serial number: ' + Format( '%X', [ GetDiskVolSerialID( 'E' ) ] ), mtInformation, [mbOk], 0 );