AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi Partition auslesen mit DeviceIoControl und IOCTL_DISK_GET_PARTITION_INFO_EX
Thema durchsuchen
Ansicht
Themen-Optionen

Partition auslesen mit DeviceIoControl und IOCTL_DISK_GET_PARTITION_INFO_EX

Ein Thema von Captnemo · begonnen am 8. Okt 2015 · letzter Beitrag vom 11. Okt 2015
Antwort Antwort
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#1

AW: Partition auslesen mit DeviceIoControl und IOCTL_DISK_GET_PARTITION_INFO_EX

  Alt 9. Okt 2015, 17:36
Deine Structs waren an einigen Stellen falsch, deshalb hast du bei den späteren Einträgen an den falschen Offsets gelesen. So funktioniert es:
Delphi-Quellcode:
type
  PARTITION_INFORMATION_MBR = record
    PartitionType: Byte;
    BootIndicator: Boolean;
    RecognizedPartition: Boolean;
    HiddenSectors: DWord;
  end;

  PARTITION_INFORMATION_GPT = record
    PartitionType: TGUID;
    PartitionId: TGUID;
    Attributes: DWORD64;
    Name: array[0..35] of WCHAR;
  end;

  PARTITION_STYLE = type DWord;

  PARTITION_INFORMATION_EX = record
    PartitionStyle: PARTITION_STYLE;
    StartingOffset: LARGE_INTEGER;
    PartitionLength: LARGE_INTEGER;
    PartitionNumber: DWord;
    RewritePartition: Boolean;
    case Integer of
      0: (Mbr: PARTITION_INFORMATION_MBR);
      1: (Gpt: PARTITION_INFORMATION_GPT);
  end;
  TPartitionInformationEx = PARTITION_INFORMATION_EX;
  PPartitionInformationEx = ^TPartitionInformationEx;

  DRIVE_LAYOUT_INFORMATION_MBR = record
    Signature: DWord;
  end;

  DRIVE_LAYOUT_INFORMATION_GPT = record
    DiskId: TGUID;
    StartingUsableOffset: LARGE_INTEGER;
    UsableLength: LARGE_INTEGER;
    MaxPartitionCount: DWord;
  end;

  DRIVE_LAYOUT_INFORMATION_UNION = record
  case Integer of
    0: (Mbr: DRIVE_LAYOUT_INFORMATION_MBR);
    1: (Gpt: DRIVE_LAYOUT_INFORMATION_GPT);
  end;

  DRIVE_LAYOUT_INFORMATION_EX = record
    PartitionStyle: DWord;
    PartitionCount: DWord;
    DriveLayoutInformation: DRIVE_LAYOUT_INFORMATION_UNION;
    PartitionEntry: array[0..0] of PARTITION_INFORMATION_EX;
  end;
  TDriveLayoutInformationEx = DRIVE_LAYOUT_INFORMATION_EX;
  PDriveLayoutInformationEx = ^TDriveLayoutInformationEx;

procedure PrintVolumeInformation(PhysicalDriveId: Integer);
var
  hDevice: THandle;
  LayoutInfo: PDriveLayoutInformationEx;
  LayoutInfoSize,
  BytesReturned: DWord;
  I: Integer;
begin
  hDevice := CreateFile(PChar('\\.\PhysicalDrive' + IntToStr(PhysicalDriveId)), 0,
    FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
  if (hDevice = INVALID_HANDLE_VALUE) then RaiseLastOSError;
  try
    LayoutInfoSize := SizeOf(TDriveLayoutInformationEx) + SizeOf(TPartitionInformationEx) * 3;
    GetMem(LayoutInfo, LayoutInfoSize);
    try
      while (not DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, nil, 0, LayoutInfo,
        LayoutInfoSize, BytesReturned, nil)) do
      begin
        if (GetLastError <> ERROR_INSUFFICIENT_BUFFER) then RaiseLastOSError;
        LayoutInfoSize := LayoutInfoSize + SizeOf(TPartitionInformationEx) * 4;
        ReallocMem(LayoutInfo, LayoutInfoSize);
      end;
      for I := 0 to LayoutInfo^.PartitionCount - 1 do
      begin
        WriteLn(LayoutInfo^.PartitionEntry[I].PartitionNumber);
        // ..
      end;
    finally
      FreeMem(LayoutInfo);
    end;
  finally
    CloseHandle(hDevice);
  end;
end;
Projekte:
- GitHub (Profil, zyantific)
- zYan Disassembler Engine ( Zydis Online, Zydis GitHub)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:54 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz