Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Read Absolute Disk Sector (https://www.delphipraxis.net/186800-read-absolute-disk-sector.html)

tcoman 30. Sep 2015 17:41

Read Absolute Disk Sector
 
hello dear DP people,
is there anybody how can me tell,
what i must do to read the data from
a disk sector ?
Wbw,
terence

Zacherl 30. Sep 2015 19:05

AW: Read Absolute Disk Sector
 
You can use MSDN-Library durchsuchenCreateFile with "\\.\PhysicalDrive0" as filename argument (requires administrator privileges). This would open the first physical drive on your computer.

Sir Rufo 30. Sep 2015 23:45

AW: Read Absolute Disk Sector
 
http://stackoverflow.com/questions/7...ce-with-delphi

Luckie 1. Okt 2015 00:01

AW: Read Absolute Disk Sector
 
http://michael-puff.de/Programmierung/Delphi/Programme/-> DiskImageNT

tcoman 3. Okt 2015 13:15

AW: Read Absolute Disk Sector
 
Liste der Anhänge anzeigen (Anzahl: 2)
Hello and thanks for your answers.
In the meanwhile i have check out
some freeware stuff from other people.

It works fine on floppy drives and
usb drives. Under win vista i've to
run the executable with admin rights
to read drive c: without errors.
But that's okay.

Here are my results.
Wbw,
terence.

tcoman 3. Okt 2015 17:00

AW: Read Absolute Disk Sector
 
Liste der Anhänge anzeigen (Anzahl: 4)
Zitat:

Zitat von Zacherl (Beitrag 1317367)
You can use MSDN-Library durchsuchenCreateFile with "\\.\PhysicalDrive0" as filename argument (requires administrator privileges). This would open the first physical drive on your computer.

Thank you dear Zacherl,
have some tries at this afternoon,
using your idea of "createfile()".

Works fine with "\\.\A:" for floppy
and "\\.\F:" for mounted usb drive under
win vista.
Failed on hdd drives like "\\.\C:" using
administrator privileges, of coz.

But i'm still on the way to solve this.
Here my attempts with a simple test program.

Wbw,
have a nice weekend,
terence.

Zacherl 4. Okt 2015 16:04

AW: Read Absolute Disk Sector
 
If hDevice is valid, the static sector size might be your problem. You should call RaiseLastOSError() after a failed API call to display a specific error message.

Zitat:

Zitat von MSDN
You can open a physical or logical drive using the CreateFile() application programming interface (API) with these device names provided that you have the appropriate access rights to the drive (that is, you must be an administrator). You must use both the CreateFile() FILE_SHARE_READ and FILE_SHARE_WRITE flags to gain access to the drive.

Once the logical or physical drive has been opened, you can then perform direct I/O to the data on the entire drive. When performing direct disk I/O, you must seek, read, and write in multiples of sector sizes of the device and on sector boundaries. Call DeviceIoControl() using IOCTL_DISK_GET_DRIVE_GEOMETRY to get the bytes per sector, number of sectors, sectors per track, and so forth, so that you can compute the size of the buffer that you will need.


hathor 4. Okt 2015 16:23

AW: Read Absolute Disk Sector
 
Liste der Anhänge anzeigen (Anzahl: 2)
EXE appended.

Delphi-Quellcode:
procedure TForm1.bReadBootSectorClick(Sender: TObject);
begin
prvSector:=0;
EBSectorNumber.Text:=IntToStr(prvSector);
_read_sector2(prvSector); // <------------------
end;

function ReadNTSectors(Drive: Byte; FirstSector: DWord; SectorCount: Word; Buffer: Pointer): Boolean;
var
  hDrive: THandle;
  DriveRoot: string;
  BytesRead: Cardinal;
  ReadCount: Cardinal;
begin
  Result := False;
  ReadCount := 512 * SectorCount;
  DriveRoot := '\\.\' + Chr(64 + Drive) + ':';
  hDrive := CreateFile(PChar(DriveRoot), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
    nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (hDrive <> INVALID_HANDLE_VALUE) then
  begin
    FileSeek(hDrive, Int64(FirstSector * 512), 0);
    BytesRead := FileRead(hDrive, Buffer^, ReadCount);
    Result := (BytesRead = (512 * SectorCount));
    FileClose(hDrive);
  end;
end;

procedure TForm1._read_sector2(mySector: cardinal);
var dr : byte;
        Buf : array[0..511] of byte;
        i : integer;
        s : string[32];
        ok : boolean;
begin
    LBSectorData.Clear;
    FillChar(Buf, SizeOf(Buf), 0);
    dr:=Ord(UpCase(DCBDrive.Drive)) - Ord('A') + 1;
    ok:= ReadNTSectors(dr, mySector, 1, @Buf);
    if ok then begin
       s:='';
       for i:=0 to 511 do begin
           if ((buf[i] > 31) and (buf[i] < 128)) then s:=s+Chr(buf[i]) else s:=s+'.';
           case i of
               31,63,95,127,159,191,223,255,287,319,351,383,415,447,479,511:
               begin LBSectorData.Items.Add(s); s:=''; end;
           end; {case}
       end;
    end else begin
       LBSectorData.Items.Add('Error Read Sector '+IntToStr(mySector));
    end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 22:33 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz