home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / PROGRAM / DELPHI16 / Calmira / Src / UTILS / DRIVES.PAS next >
Pascal/Delphi Source File  |  1997-02-15  |  6KB  |  217 lines

  1. {*********************************************************}
  2. {                                                         }
  3. {    Calmira System Library 1.0                           }
  4. {    by Li-Hsin Huang,                                    }
  5. {    released into the public domain January 1997         }
  6. {                                                         }
  7. {*********************************************************}
  8.  
  9. unit Drives;
  10.  
  11. { Disk drive detecting functions }
  12.  
  13. interface
  14.  
  15. uses FileCtrl; { defines TDriveType }
  16.  
  17. const
  18.   DriveDesc : array[TDriveType] of string[15] =
  19.     ('Unknown drive', 'No drive', 'Floppy drive', 'Hard drive', 'Network drive',
  20.      'CD-ROM drive', 'RAM disk');
  21.  
  22. type
  23.   TDriveRange = 'A'..'Z';
  24.   TDriveFlag = (dfValid, dfFloppy, dfFixed, dfNetwork, dfCDROM, dfRAM,
  25.                 dfRemoveable, dfWriteable);
  26.   TDriveFlags = set of TDriveFlag;
  27.  
  28. var
  29.   ValidDrives : set of TDriveRange;
  30.  
  31.  
  32. function DriveNumber(Drive : Char) : Integer;
  33. { Maps drive characters to Integers used by Delphi. A = 1, B = 2, ... }
  34.  
  35. function WinDriveNumber(Drive: Char): Integer;
  36. { Maps drive characters to Integers used by Windows. A = 0, B = 1, ... }
  37.  
  38. function FindDriveType(Drive: Char): TDriveType;
  39. { Detects the type of the specified drive }
  40.  
  41. function GuessDriveType(Drive : Char) : TDriveType;
  42. { Returns the type of the drive without needing disk access --
  43.   the information is obtained and stored during initialization }
  44.  
  45. function GetDriveFlags(drive: Char) : TDriveFlags;
  46. { Returns the flag set for the given drive }
  47.  
  48. function IsRAMDrive(DriveNum: Integer): Boolean;
  49. { Returns true if the drive is a RAM disk }
  50.  
  51. function IsCDROM(DriveNum: Integer): Boolean;
  52. { Returns true if the drive is a CD-ROM drive }
  53.  
  54. function GetNetworkVolume(Drive: Char): string;
  55. { Returns the network name of the drive if available, otherwise
  56.   just returns the volume label }
  57.  
  58. function GetVolumeID(Drive: Char): string;
  59. { Returns the volume label of the disk }
  60.  
  61. procedure DetectDrives;
  62. { Refreshes this unit's information about drives on the system.
  63.   Called automatically during initialization. }
  64.  
  65.  
  66. implementation
  67.  
  68. uses SysUtils, WinProcs, WinTypes;
  69.  
  70. var
  71.   DriveFlags  : array[TDriveRange] of TDriveFlags;
  72.   DriveTypes  : array[TDriveRange] of TDriveType;
  73.  
  74.  
  75. function DriveNumber(Drive : Char) : Integer;
  76. begin
  77.   Result := Ord(UpCase(Drive)) - Ord('A') + 1;
  78. end;
  79.  
  80.  
  81. function WinDriveNumber(Drive: Char): Integer;
  82. begin
  83.   Result := DriveNumber(Drive) - 1;
  84. end;
  85.  
  86.  
  87. function IsCDROM(DriveNum: Integer): Boolean; assembler;
  88. asm
  89.   MOV   AX, 1500h { look for MSCDEX }
  90.   XOR   BX, BX
  91.   INT   2Fh
  92.   OR    BX, BX
  93.   JZ    @Finish
  94.   MOV   AX, 150Bh { check for using CD driver }
  95.   MOV   CX, DriveNum
  96.   INT   2fh
  97.   OR    AX, AX
  98.   @Finish:
  99. end;
  100.  
  101.  
  102. function IsRAMDrive(DriveNum: Integer): Boolean; assembler;
  103. var
  104.   Temp: Boolean;
  105. asm
  106.   MOV   Temp, False
  107.   PUSH  DS
  108.   MOV   BX, SS
  109.   MOV   DS, BX
  110.   SUB   SP, 0200h
  111.   MOV   BX, SP
  112.   MOV   AX, DriveNum
  113.   MOV   CX, 1
  114.   XOR   DX, DX
  115.   INT   25h  { read boot sector }
  116.   ADD   SP, 2
  117.   JC    @@1
  118.   MOV   BX, SP
  119.   CMP   BYTE PTR SS:[BX+15h], 0F8h  { reverify fixed disk }
  120.   JNE   @@1
  121.   CMP   BYTE PTR SS:[BX+10h], 1     { check for single FAT }
  122.   JNE   @@1
  123.   MOV   Temp, True
  124. @@1:
  125.   ADD   SP, 0200h
  126.   POP   DS
  127.   MOV   AL, Temp
  128. end;
  129.  
  130.  
  131. function FindDriveType(Drive: Char): TDriveType;
  132. var
  133.   n : Integer;
  134. begin
  135.   n := WinDriveNumber(Drive);
  136.  
  137.   case GetDriveType(n) of
  138.     0               : Result := dtNoDrive;
  139.     DRIVE_REMOVABLE : Result := dtFloppy;
  140.     DRIVE_FIXED     : if IsRAMDrive(n) then Result := dtRAM
  141.                       else Result := dtFixed;
  142.     DRIVE_REMOTE    : if IsCDROM(n) then Result := dtCDROM
  143.                       else Result := dtNetwork;
  144.   end;
  145. end;
  146.  
  147.  
  148.  
  149. procedure DetectDrives;
  150. var
  151.   d: Char;
  152. begin
  153.   ValidDrives := [];
  154.   for d := 'A' to 'Z' do begin
  155.     DriveTypes[d] := FindDriveType(d);
  156.     case DriveTypes[d] of
  157.       dtUnknown : DriveFlags[d] := [dfValid, dfFixed, dfWriteable];
  158.       dtNoDrive : DriveFlags[d] := [];
  159.       dtFloppy  : DriveFlags[d] := [dfValid, dfFloppy, dfRemoveable, dfWriteable];
  160.       dtFixed   : DriveFlags[d] := [dfValid, dfFixed, dfWriteable];
  161.       dtNetwork : DriveFlags[d] := [dfValid, dfNetwork, dfWriteable];
  162.       dtCDROM   : DriveFlags[d] := [dfValid, dfCDROM, dfRemoveable];
  163.       dtRAM     : DriveFlags[d] := [dfValid, dfRAM, dfWriteable];
  164.     end;
  165.     if dfValid in DriveFlags[d] then Include(ValidDrives, d);
  166.   end;
  167. end;
  168.  
  169.  
  170. function GuessDriveType(Drive : Char) : TDriveType;
  171. begin
  172.   Drive := UpCase(Drive);
  173.   if Drive in ['A'..'Z'] then Result := DriveTypes[Drive]
  174.   else Result := dtNoDrive;
  175. end;
  176.  
  177.  
  178. function GetDriveFlags(Drive: Char) : TDriveFlags;
  179. begin
  180.   Drive := UpCase(Drive);
  181.   if Drive in ['A'..'Z'] then Result := DriveFlags[Drive]
  182.   else Result := [];
  183. end;
  184.  
  185.  
  186. function GetVolumeID(Drive: Char): string;
  187. var
  188.   SR: TSearchRec;
  189. begin
  190.   Result := '';
  191.   if FindFirst(Drive + ':\*.*', faVolumeID, SR) = 0 then
  192.     Result := Uppercase(SR.Name);
  193. end;
  194.  
  195.  
  196. function GetNetworkVolume(Drive: Char): string;
  197. const
  198.   LocalName: array[0..2] of Char = 'C:'#0;
  199. var
  200.   BufSize: Word;
  201.   Temp: array[0..128] of Char;
  202. begin
  203.   LocalName[0] := Drive;
  204.   BufSize := 127;
  205.   if WNetGetConnection(LocalName, Temp, @BufSize) = WN_SUCCESS then
  206.     Result := StrPas(Temp)
  207.   else
  208.     Result := GetVolumeID(Drive);
  209. end;
  210.  
  211.  
  212.  
  213.  
  214. initialization
  215.   DetectDrives;
  216. end.
  217.