Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Case in Record (https://www.delphipraxis.net/186870-case-record.html)

Uwe Raabe 8. Okt 2015 11:11

AW: Case in Record
 
Zitat:

Zitat von p80286 (Beitrag 1318019)
Ich meine mich zu erinnern, daß es auch so gehen könnte:

Delphi-Quellcode:
    DRIVE_LAYOUT_INFORMATION_EX = record
      PartitionStyle: DWORD;
      PartitionCount: DWORD;
      case Integer of
        0: (Mbr: DRIVE_LAYOUT_INFORMATION_MBR;
            partitionInfoEx: array of _PARTITION_INFORMATION_EX; );
        1: (Gpt: DRIVE_LAYOUT_INFORMATION_GPT
            partitionInfoEx: array of _PARTITION_INFORMATION_EX;);
    end;
Bin mir über die korrekte Syntax nicht im Klaren.

Rein theoretisch könnte das nur funktionieren, wenn der Speicher für Mbr und Gpt exakt gleich groß ist. Andernfalls hätten wir zwei verschiedene Speicheradressen für partitionInfoEx. Aber hier schlägt noch ein anderes Constraints zu (nach Zurechtrücken der Semikoli): Dynamische Arrays sind in varianten Teilen nicht erlaubt! Wie sollte der Compiler denn auch das Finalize ausführen, wenn er nicht weiß, welche Variante gültig ist?

Konsequenterweise bemängelt der Compiler dann auch das zweite partitionInfoEx als "Bezeichner redeklariert".

Captnemo 8. Okt 2015 11:38

AW: Case in Record
 
Liste der Anhänge anzeigen (Anzahl: 1)
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
Delphi-Quellcode:
PartitionInfoEx: array[0..10] of _PARTITION_INFORMATION_EX
. Ich habe in diversen Posts gelesen, dass es
Delphi-Quellcode:
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.

Captnemo 8. Okt 2015 11:51

AW: Case in Record
 
Ich mache dafür mal einen neuen Thread (Hier) auf, weil es ja nicht mehr so unmittelbar zum Thema passt.


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:03 Uhr.
Seite 2 von 2     12   

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