Einzelnen Beitrag anzeigen

Benutzerbild von Zacherl
Zacherl

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

AW: Partition auslesen mit DeviceIoControl und IOCTL_DISK_GET_PARTITION_INFO_EX

  Alt 9. Okt 2015, 18: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