Thema: Delphi Case in Record

Einzelnen Beitrag anzeigen

Benutzerbild von Captnemo
Captnemo

Registriert seit: 27. Jan 2003
Ort: Bodenwerder
1.126 Beiträge
 
Delphi XE4 Architect
 
#12

AW: Case in Record

  Alt 8. Okt 2015, 10:38
Ich habe das jetzt so gemacht:
Delphi-Quellcode:
  Type
    PARTITION_STYLE = type Integer;

    PARTITION_INFORMATION_MBR = record
      PartitionType: Byte;
      BootIndicator: BOOL;
      RecognizedPartition: BOOL;
      HiddenSecorts: DWORD;
    end;

    PARTITION_INFORMATION_GPT = record
      PartitonType: TGUID;
      PartitionId: TGUID;
      Attributes: DWORD64;
      Name: WCHAR;
    end;

    _PARTITION_INFORMATION_EX = record
      PartitionStyle: PARTITION_STYLE;
      StartingOffset: LARGE_INTEGER;
      PartitionLength: LARGE_INTEGER;
      PartitionNumber: ULONG;
      RewritePartition: Boolean;
      case Integer of
       0: (Mbr: PARTITION_INFORMATION_MBR);
       1: (Gpt: PARTITION_INFORMATION_GPT);
    end;

    DRIVE_LAYOUT_INFORMATION_MBR = record
      Signature: ULONG;
    end;

    DRIVE_LAYOUT_INFORMATION_GPT = record
      DiskID: TGUID;
      StartingusableOffset: LARGE_INTEGER;
      UsableLength: LARGE_INTEGER;
      MaxPartitionCount: ULONG;
    end;

    DRIVE_LAYOUT_INFORMATION_EX = record
      PartitionStyle: DWORD;
      PartitionCount: DWORD;
      DriveLayoutInfoType: record
        case Integer of
          0: (Mbr: DRIVE_LAYOUT_INFORMATION_MBR);
          1: (Gpt: DRIVE_LAYOUT_INFORMATION_GPT);
      end;
      PartitionInfoEx: array[0..10] of _PARTITION_INFORMATION_EX;
    end;

  const
    PARTITION_STYLE_MBR = PARTITION_STYLE(0);
    PARTITION_STYLE_GPT = PARTITION_STYLE(1);
    PARTITION_STYLE_RAW = PARTITION_STYLE(2);
Und es scheint auch soweit zu funktionieren. Allerdings habe ich noch Probleme mit PartitionInfoEx: array[0..10] of _PARTITION_INFORMATION_EX . Ich habe in diversen Posts gelesen, dass es PartitionInfoEx: array[0..0] of _PARTITION_INFORMATION_EX lauten soll. Aber dann erhalte ich gar keine Informationen.

Hier mal meine Procedure, mit der ich die Daten abfrage:
Delphi-Quellcode:
procedure TForm1.btn2Click(Sender: TObject);
var
  RetBytes: DWORD;
  hDevice: Cardinal;
  Status: LongBool;
  Drive: string;
  Layout: DRIVE_LAYOUT_INFORMATION_EX;
  I,p: Integer;
begin
  for I := 0 to 15 do
  begin
    Drive:='\\.\PhysicalDrive'+IntToStr(i);
    hDevice:=CreateFile(PChar(Drive), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
    if hDevice<>INVALID_HANDLE_VALUE then
    begin
      Status:=DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, nil, 0, @Layout, SizeOf(DRIVE_LAYOUT_INFORMATION_EX), RetBytes, nil);
      if (Status=False) then
        mmo1.lines.Add(IntToStr(i)+'. Festplatte abfrage fehlgeschalten') else
      begin
        mmo1.Lines.Add(IntToStr(i)+'. Festplatte = '+inttostr(Layout.PartitionCount)+' Partitionen');
        for p := 0 to Length(Layout.partitionInfoEx) do
        begin
          if (Layout.partitionInfoEx[p].StartingOffset.QuadPart<>0) or (Layout.partitionInfoEx[p].PartitionLength.QuadPart<>0) then
          begin
            mmo1.Lines.Add(' '+inttostr(p)+'. Partition: ');
            case Layout.partitionInfoEx[p].PartitionStyle of
              PARTITION_STYLE_MBR: mmo1.Lines.Add(' Part-Typ : MBR');
              PARTITION_STYLE_GPT: mmo1.Lines.Add(' Part-Typ : GPT');
              PARTITION_STYLE_RAW: mmo1.Lines.Add(' Part-Typ : RAW');
            end;
            mmo1.lines.Add(' PartitionNr: '+IntToStr(Layout.partitionInfoEx[p].PartitionNumber));
            mmo1.Lines.Add(' StartSektor: '+IntToStr(Layout.partitionInfoEx[p].StartingOffset.QuadPart));
            mmo1.lines.Add(' Länge : '+IntToStr(Layout.partitionInfoEx[p].PartitionLength.QuadPart));
            mmo1.Lines.Add(' Größe : '+GetSizeAsString(Layout.partitionInfoEx[p].PartitionLength.QuadPart));
          end;
        end;
      end;
      CloseHandle(hDevice);
    end else mmo1.lines.Add(IntToStr(i)+'. Festplatte hat kein hDeviceHandle');
  end;

end;

function TForm1.GetSizeAsString(Size: Int64): string;
var
  new: Extended;
  Sign: String;
  c: Integer;
begin
  c:=0;
  new:=Size;
  while new>1024 do
  begin
    new:=new/1024;
    Inc(c);
  end;
  case c of
    0: Sign:=' Byte';
    1: Sign:=' KB';
    2: Sign:=' MB';
    3: Sign:=' GB';
    4: Sign:=' TB';
    5: Sign:=' PB';
    6: Sign:=' EB';
    7: Sign:=' ZB';
    8: Sign:=' YB';
  else
    Sign:=' ('+intToStr(c)+')';
  end;
  Result:=FormatFloat('#,##0.00', new)+Sign;
end;
Ich wäre nach dem was ich gelesen habe mal davon ausgegangen, dass das Array partitionInfoEx hinterher der Partitionsanzahl entsprechende Elemente beinhaltet. Dem ist aber nicht so. Wenn ich es mit 0..0 deklariere, dann ist bleibt es eben auch leer, und wenn ich z.B. 0..10 deklariere, dann erhalte ich auch 11 Elemente, die dann aber eben zum Teil uninitialisierte Werte enthalten (ist klar).
Ich will ja hinterher in partitionInfoEx eben nur die Partitionen haben, die auf dem Datenträger auch existieren.

Ich hab mal das Testprojekt angehängt.
Angehängte Dateien
Dateityp: rar PartitionTest.rar (652,8 KB, 11x aufgerufen)
Dieter
9 von 10 Stimmen in meinem Kopf sagen ich bin nicht verrückt. Die 10. summt dazu die Melodie von Supermario Bros.
MfG Captnemo
  Mit Zitat antworten Zitat