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;
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).
MessageDlg(
'Serial number: ' +
Format( '%X', [ GetDiskVolSerialID( 'E' ) ] ),
mtInformation, [mbOk], 0 );
'Serial number: ' +
Format( '%X', [ GetDiskVolSerialID( 'E' ) ] ),
mtInformation, [mbOk], 0 );
Listing #2 : Delphi code. Download diskvol2 (0.24 KB).