Disk in Drive

From Free Pascal wiki
Jump to navigationJump to search
Windows logo - 2012.svg

This article applies to Windows only.

See also: Multiplatform Programming Guide

Deutsch (de) English (en)

The function checks whether there is a disk medium in the CD or DVD drive.

 uses
   SysUtils, ...;

 // enumeration for the return values
 grade
   byte = (enmNoDriveLetter, enmMoMedInserted, enmMedInserted, enmError);

  ...

 function funDiskInDrive (chrDrive : char) : byte;
 begin

   Result := enmNoDriveLetter;
   chrDrive := UpCase(chrDrive);

   // Checks whether the drive letter is valid.
   if not (chrDrive in ['A' .. 'Z']) then
     exit;

   // Checks if the drive contains media.
   try
     if DiskSize ( Ord ( chr drive ) - $ 40 ) <> - 1 then
       Result := enmMedInserted
     else
       Result := enmNoMedInserted;
   except
     Result := enmError;
   end;

 end;

   ...

Example of calling the function:

  ...

   case funDiskInDrive('F') of
     enmNoDriveLetter : ... ;
     enmNoMedInserted : ... ;
     enmMedInserted : ... ;
     Error : ... ;
   end; 
 
   ...