Mega Code Archive

 
Categories / Delphi / Examples
 

3 Drive related Functions

Title: 3 Drive related Functions Question: Check for Drive A Get Drive information Get Driver Serial# Answer: //// Check if there is a disk in drive A silently When your program accesses drive 'A', it would be handy to intercept the 'Drive Not Ready' system error message. You can create your own generic function to test any drive letter. function DiskInDrive(Drive: Char): Boolean; var ErrorMode: word; begin Drive: = UpCase(Drive); if not (Drive in ['A'..'Z']) then raise EConvertError.Create('Not a valid drive ID'); ErrorMode := SetErrorMode(SEM_FailCriticalErrors); try if DiskSize(Ord(Drive) - $40) = -1 then DiskInDrive := False else DiskInDrive := True; finally SetErrorMode(ErrorMode); end; end; Your function forces the passed drive letter to uppercase and assures it is a valid drive. Then you turn off the system error reporting and perform a disk operation. Your function will return True indicating the disk is present or False if there was an error. The last bit of housekeeping is to turn on the system error reporting. ///// How to get drive information Procedure Drives; var Drive: Char; DriveLetter: String[4]; li : TListItem; iSPC, iBPS, iFC, iTC : integer; bDataOK : boolean; sDriveType, sStatus : string; begin for Drive := 'A' to 'Z' do begin DriveLetter := Drive + ':\'; bDataOK := GetDiskFreeSpace(PChar(Drive + ':\'), iSPC, iBPS, iFC, iTC); sDriveType := ''; case GetDriveType(PChar(Drive + ':\')) of DRIVE_REMOVABLE: sDriveType := 'Floppy'; DRIVE_FIXED: sDriveType := 'Fixed'; DRIVE_REMOTE: sDriveType := 'Network'; DRIVE_CDROM: sDriveType := 'CD-ROM'; end; if sDriveType '' then begin sDriveType := sDriveType + ' Drive '; if bDataOK then sStatus := Format('%.2fMB, %.2fMB Free', [iSPC * 1.0 * iBPS * iTC / 1024.0 / 1024.0, iSPC * 1.0 * iBPS * iFC / 1024.0 / 1024.0]) else sStatus := ''; li := lv.Items.Add; li.Caption := Format('Drive %s:', [Drive]); li.SubItems.Add(sDriveType + sStatus); end; end; end; ///// Get Drive Serial Number type MIDPtr = ^MIDRec; MIDRec = Record InfoLevel: word; SerialNum: LongInt; VolLabel: Packed Array [0..10] of Char; FileSysType: Packed Array [0..7] of Char; end; var Info: MIDRec; function GetDriveSerialNum(MID: MIDPtr; drive: Word): Boolean; assembler; asm push DS { Just for safety, I dont think its really needed } mov ax,440Dh { Function Get Media ID } mov bx,drive { drive no (0-Default, 1-A ...) } mov cx,0866h { category and minor code } lds dx,MID { Load pointeraddr. } call DOS3Call { Supposed to be faster than INT 21H } jc @@err mov al,1 { No carry so return TRUE } jmp @@ok @@err: mov al,0 { Carry set so return FALSE } @@ok: pop DS { Restore DS, were not supposed to change it } end; {To read the Serial Number} function ReadSerial:string; begin if GetDriveSerialNum(@info,0) then Result := IntToStr(info.SerialNum); end; {To read the Vol Label} function ReadVol:string; begin if GetDriveSerialNum(@info,0) then Result := StrPas(Info.VolLabel); end;