type
TDiskGeometry = packed record
Cylinders: Int64;
MediaType: Integer;
TracksPerCylinder: DWORD;
SectorsPerTrack: DWORD;
BytesPerSector: Integer;
end;
TDrive = record
DiskGeometry: TDiskGeometry;
Handle: THandle;
end;
var
num: Cardinal;
buf: array of char;
procedure CloseDrive(var Drive: TDrive);
begin
CloseHandle(Drive.Handle);
Drive.Handle := 0;
end;
function OpenDrive(C: char; out f: byte): TDrive;
begin
zeromemory(@Result, SizeOf(TDrive));
Result.Handle := CreateFile(PChar('\\.\' + upcase(C) + ':'),
GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_NO_BUFFERING, 0);
if Result.Handle = INVALID_HANDLE_VALUE then begin
f := 1;
exit;
end;
if not DeviceIoControl(Result.handle, 458752, nil, 0,
@Result.DiskGeometry, SizeOf(TDiskGeometry), num, nil)
then begin
CloseDrive(Result);
f := 2;
exit;
end;
f := 0;
end;
function ReadSector(const Drive: TDrive; Sektor: integer): boolean;
begin
fileseek(Drive.Handle, Drive.DiskGeometry.BytesPerSector * Sektor, 0);
result := ReadFile(Drive.Handle, Buf[0], Drive.DiskGeometry.BytesPerSector,
num, nil);
end;
// Beispielaufruf
procedure TForm1.Button1Click(Sender: TObject);
var
Sektor: integer;
ok: boolean;
RD: TDrive;
P: PChar;
f: byte;
begin
Sektor := 0;
RD := OpenDrive('C', f);
case f of
1: raise exception.create('Handle-Error');
2: raise exception.create('Geometry-Error');
end;
setlength(buf, RD.DiskGeometry.BytesPerSector);
zeromemory(@buf[0], RD.DiskGeometry.BytesPerSector);
ok := ReadSector(RD, Sektor);
CloseDrive(RD);
if ok then begin
// --- mach was, z.B.: ---
p := @buf[0];
inc(p, 3);
showmessage(p);
// -----------------------
end else showmessage('Lesen fehlgeschlagen');
buf := nil;
end;