AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

C- union Struktur in Delphi

Ein Thema von jackie · begonnen am 30. Apr 2005 · letzter Beitrag vom 1. Mai 2005
Antwort Antwort
Seite 1 von 2  1 2      
jackie
(Gast)

n/a Beiträge
 
#1

C- union Struktur in Delphi

  Alt 30. Apr 2005, 18:15
Hallo!
Kann ich in Delphi union Strukturen erstellen (wie in C)?
Beispiel:
Code:
...
union {
    ULONGLONG Alignment;
    struct {
      ULONG Length;
      DWORD IfIndex;
    };
  };
...
  Mit Zitat antworten Zitat
rantanplan99
(Gast)

n/a Beiträge
 
#2

Re: C- union Struktur in Delphi

  Alt 30. Apr 2005, 18:45
Googlest du, findest du diesen link hier.

Zitat:
Variant Records: The equivalent to the C-union structure

Is there a way to create a C 'union'-like structure in Delphi? That is, a structure that uses the same memory area?

The Delphi (Pascal/ObjectPascal) equivalent to a C-union structure is called a Variant Record (not to be confused with the Variant "type" available in Delphi 2.0+). As with a C-union, the Pascal variant record allows several structure types to be combined into one, and all will occupy the same memory space. Look up the syntax declaration under "Records" in the help file. But here's an example:
Delphi-Quellcode:
type
 TPerson = record
   FirstName, LastName: string[40];
   BirthDate: TDate;
   case Citizen: Boolean of
     True: (BirthPlace: string[40]);
     False: (Country: string[20];
       EntryPort: string[20];
       EntryDate: TDate;
       ExitDate: TDate);
 end;
The record above is actually a single expression of two records that could describe a person:
Delphi-Quellcode:
type
 TPersonCitizen = record
   FirstName, LastName: string[40];
   BirthDate: TDate;
   BirthPlace: string[40]
 end;
and
Delphi-Quellcode:
type
 TPersonAlien = record
   FirstName, LastName: string[40];
   BirthDate: TDate;
   Country: string[20];
   EntryPort: string[20];
   EntryDate: TDate;
   ExitDate: TDate;
 end;
And as in a union, the combination of the two types of records makes for much more efficient programming, because a person could be expressed in a variety of ways.

Everything I explained above is pretty hypothetical stuff. In Delphi, the TRect structure that describes a rectangle is actually a variant record:
Delphi-Quellcode:
type
TPoint = record
  X: Longint;
  Y: Longint;
end;

TRect = record
  case Integer of
    0: (Left, Top, Right, Bottom: Integer);
    1: (TopLeft, BottomRight: TPoint);
  end;
where the coordinates of the rectangle can be expressed using either four integer values or two TPoints.

I realize this is pretty quick and dirty, so I suggest you refer to the help file for a more in-depth explanation, or go to your nearest book store or library and look at any Pascal book (not Delphi -- most won't explain this fairly esoteric structure). However, if you're familiar with the C-union, this stuff should be an absolute breeze.

Copyright © 1997 Brendan V. Delumpa All Rights Reserved
Copyright © The Delphi Corner 2001 All Rights Reserved
rantanplan
  Mit Zitat antworten Zitat
Christian Seehase
(Co-Admin)

Registriert seit: 29. Mai 2002
Ort: Hamburg
11.107 Beiträge
 
Delphi 11 Alexandria
 
#3

Re: C- union Struktur in Delphi

  Alt 30. Apr 2005, 18:46
Moin Gregor,

das sollte sich grundsätzlich über case lösen lassen.

In Deinem Beispiel müsste das so aussehen:

Delphi-Quellcode:
type
  TMyUnion = packed record
    case byte of
      1 : (Alignment : int64;)
      2 : (Length : DWORD;
           IfIndex : DWORD;)
  end;
Schau doch mal in der Hilfe unter dem Stichwort "variante Records".

Ein gravierendes Problem gibt es allerdings auf jeden Fall:
Der variante Teil muss am Ende der Record-Deklaration stehen, so dass man ziemliche Verrenkungen machen muss, wenn man so eine Struktur mittendrin braucht.
Tschüss Chris
Die drei Feinde des Programmierers: Sonne, Frischluft und dieses unerträgliche Gebrüll der Vögel.
Der Klügere gibt solange nach bis er der Dumme ist
  Mit Zitat antworten Zitat
jackie
(Gast)

n/a Beiträge
 
#4

Re: C- union Struktur in Delphi

  Alt 1. Mai 2005, 15:06
Hallo!
Also erstmal danke für die Antworten.
Die originale Struktur (C) sieht so aus:
Code:
typedef struct _IP_ADAPTER_ADDRESSES {
  union {
    ULONGLONG Alignment;
    struct {
      ULONG Length;
      DWORD IfIndex;
    };
  };
  struct _IP_ADAPTER_ADDRESSES* Next;
  PCHAR AdapterName;
  PIP_ADAPTER_UNICAST_ADDRESS FirstUnicastAddress;
  PIP_ADAPTER_ANYCAST_ADDRESS FirstAnycastAddress;
  PIP_ADAPTER_MULTICAST_ADDRESS FirstMulticastAddress;
  PIP_ADAPTER_DNS_SERVER_ADDRESS FirstDnsServerAddress;
  PWCHAR DnsSuffix;
  PWCHAR Description;
  PWCHAR FriendlyName;
  BYTE PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH];
  DWORD PhysicalAddressLength;
  DWORD Flags;
  DWORD Mtu;
  DWORD IfType;
  IF_OPER_STATUS OperStatus;
  DWORD Ipv6IfIndex;
  DWORD ZoneIndices[16];
  PIP_ADAPTER_PREFIX FirstPrefix;
} IP_ADAPTER_ADDRESSES,
*PIP_ADAPTER_ADDRESSES;
Ich habs jetzt so übersetzt:
Delphi-Quellcode:
PIP_ADAPTER_ADDRESSES = ^TIP_ADAPTER_ADDRESSES;
  TIP_ADAPTER_ADDRESSES = packed record
      {struct _IP_ADAPTER_ADDRESSES* Next;}
      AdapterName: PChar;
      FirstUnicastAddress: PIP_ADAPTER_UNICAST_ADDRESS;
      FirstAnycastAddress: PIP_ADAPTER_ANYCAST_ADDRESS;
      FirstMulticastAddress: PIP_ADAPTER_MULTICAST_ADDRESS;
      FirstDnsServerAddress: PIP_ADAPTER_DNS_SERVER_ADDRESS;
      DnsSuffix: PWCHAR;
      Description: PWCHAR;
      FriendlyName: PWCHAR;
      PhysicalAddress: Byte[MAX_ADAPTER_ADDRESS_LENGTH];
      PhysicalAddressLength: DWord;
      Flags: DWord;
      Mtu: DWord;
      IfType: DWord;
      OperStatus: IF_OPER_STATUS;
      Ipv6Index: DWord;
      ZoneIndices: DWORD[16];
      FirstPrefix: PIP_ADAPTER_PREFIX;
      {union??}
    end;
Bei den Sachen in Klammern bin ich mir nicht sicher. Ich weis auch nicht, welchen Namen ich dem union geben soll?!
Zitat:
Der variante Teil muss am Ende der Record-Deklaration stehen, so dass man ziemliche Verrenkungen machen muss, wenn man so eine Struktur mittendrin braucht.
Das sollte hier aber eigentlich kein Problem sein, oder?
  Mit Zitat antworten Zitat
Mephistopheles
(Gast)

n/a Beiträge
 
#5

Re: C- union Struktur in Delphi

  Alt 1. Mai 2005, 15:20
Wie schreibst du so schön in deiner Signatur? Wurde doch oben alles erklärt.


Delphi-Quellcode:
  PIP_ADAPTER_ADDRESSES = ^TIP_ADAPTER_ADDRESSES;
  TIP_ADAPTER_ADDRESSES = packed record
    case Integer of
      1: (Alignment: ULONGLONG);
      2: (Length: ULONG; IfIndex: DWORD; // <-- matches sizeof(ULONGLONG)
        Next: PIP_ADAPTER_ADDRESSES; // <-- start of variant part
        AdapterName: PChar;
        FirstUnicastAddress: PIP_ADAPTER_UNICAST_ADDRESS;
        FirstAnycastAddress: PIP_ADAPTER_ANYCAST_ADDRESS;
        FirstMulticastAddress: PIP_ADAPTER_MULTICAST_ADDRESS;
        FirstDnsServerAddress: PIP_ADAPTER_DNS_SERVER_ADDRESS;
        DnsSuffix: PWCHAR;
        Description: PWCHAR;
        FriendlyName: PWCHAR;
        PhysicalAddress: Byte[MAX_ADAPTER_ADDRESS_LENGTH];
        PhysicalAddressLength: DWORD;
        Flags: DWORD;
        Mtu: DWORD;
        IfType: DWORD;
        OperStatus: IF_OPER_STATUS;
        Ipv6Index: DWORD;
        ZoneIndices: DWORD[16];
        FirstPrefix: PIP_ADAPTER_PREFIX;
        )
  end;
So müßte es aussehen, was bei mir aber nicht kompiliert, wegen DWORD[16] und Byte[MAX_ADAPTER_ADDRESS_LENGTH]. Kann man umgehen, indem man dafür auch eigene Typen einführt.
  Mit Zitat antworten Zitat
jackie
(Gast)

n/a Beiträge
 
#6

Re: C- union Struktur in Delphi

  Alt 1. Mai 2005, 15:22
Danke!
  Mit Zitat antworten Zitat
Christian Seehase
(Co-Admin)

Registriert seit: 29. Mai 2002
Ort: Hamburg
11.107 Beiträge
 
Delphi 11 Alexandria
 
#7

Re: C- union Struktur in Delphi

  Alt 1. Mai 2005, 15:23
Moin Gregor,

das Problem ist, dass in der Originalstruktur die Union am Anfang steht, die kannst Du jetzt also nicht einfach an das Ende setzen.

Ich denke mal so müsste es gehen:

Code:
  PIP_ADAPTER_ADDRESSES = ^TIP_ADAPTER_ADDRESSES;
  TIP_ADAPTER_ADDRESSES = packed record
    case byte of
      0 : (Alignment : int64;
           Next : P_IP_ADAPTER_ADDRESSES;
           AdapterName: PChar;
           FirstUnicastAddress: PIP_ADAPTER_UNICAST_ADDRESS;
           FirstAnycastAddress: PIP_ADAPTER_ANYCAST_ADDRESS;
           FirstMulticastAddress: PIP_ADAPTER_MULTICAST_ADDRESS;
           FirstDnsServerAddress: PIP_ADAPTER_DNS_SERVER_ADDRESS;
           DnsSuffix: PWCHAR;
           Description: PWCHAR;
           FriendlyName: PWCHAR;
           PhysicalAddress: [color=red]array [0..MAX_ADAPTER_ADDRESS_LENGTH-1] of byte;[/color]
           PhysicalAddressLength: DWord;
           Flags: DWord;
           Mtu: DWord;
           IfType: DWord;
           OperStatus: IF_OPER_STATUS;
           Ipv6Index: DWord;
           ZoneIndices: [color=red]array [0..15] of DWORD;[/color]
           FirstPrefix: PIP_ADAPTER_PREFIX;);
      1 : (length : DWORD;
           IfIndex : DWORD;)
    end;
Nicht zu vergessen die Arrays auch in Delphi-Konvention einzubauen. (rot markiert)
Tschüss Chris
Die drei Feinde des Programmierers: Sonne, Frischluft und dieses unerträgliche Gebrüll der Vögel.
Der Klügere gibt solange nach bis er der Dumme ist
  Mit Zitat antworten Zitat
Mephistopheles
(Gast)

n/a Beiträge
 
#8

Re: C- union Struktur in Delphi

  Alt 1. Mai 2005, 15:27
Zitat von Christian Seehase:
das Problem ist, dass in der Originalstruktur die Union am Anfang steht, die kannst Du jetzt also nicht einfach an das Ende setzen.
Du darfst den varianten Teil aber auch nicht einfach an den Anfang setzen ... mein Delphi verweigert sich jedenfalls dieser Syntax

Siehe hier:
Zitat von Christian Seehase:
Ein gravierendes Problem gibt es allerdings auf jeden Fall:
Der variante Teil muss am Ende der Record-Deklaration stehen, so dass man ziemliche Verrenkungen machen muss, wenn man so eine Struktur mittendrin braucht.
Zitat von Christian Seehase:
Ich denke mal so müsste es gehen:

Code:
  PIP_ADAPTER_ADDRESSES = ^TIP_ADAPTER_ADDRESSES;
  TIP_ADAPTER_ADDRESSES = packed record
    case byte of
      0 : (Alignment : int64;
           Next : P_IP_ADAPTER_ADDRESSES;
           AdapterName: PChar;
           FirstUnicastAddress: PIP_ADAPTER_UNICAST_ADDRESS;
           FirstAnycastAddress: PIP_ADAPTER_ANYCAST_ADDRESS;
           FirstMulticastAddress: PIP_ADAPTER_MULTICAST_ADDRESS;
           FirstDnsServerAddress: PIP_ADAPTER_DNS_SERVER_ADDRESS;
           DnsSuffix: PWCHAR;
           Description: PWCHAR;
           FriendlyName: PWCHAR;
           PhysicalAddress: [color=red]array [0..MAX_ADAPTER_ADDRESS_LENGTH-1] of byte;[/color]
           PhysicalAddressLength: DWord;
           Flags: DWord;
           Mtu: DWord;
           IfType: DWord;
           OperStatus: IF_OPER_STATUS;
           Ipv6Index: DWord;
           ZoneIndices: [color=red]array [0..15] of DWORD;[/color]
           FirstPrefix: PIP_ADAPTER_PREFIX;);
      1 : (length : DWORD;
           IfIndex : DWORD;)
    end;
Definitiv nicht! Der variante Teil ist falsch plaziert.

Zitat von Christian Seehase:
Nicht zu vergessen die Arrays auch in Delphi-Konvention einzubauen. (rot markiert)
Okay, die Arrays waren deswegen ein Problem bei mir. Hast recht. Ich beschäftige mich eindeutig zuviel mit C und C++

So ist es komplett korrekt:
Code:
type
  PIP_ADAPTER_ADDRESSES = ^TIP_ADAPTER_ADDRESSES;
  TIP_ADAPTER_ADDRESSES = packed record
    case Integer of
      [color=red]1: (Alignment: ULONGLONG);
      2: (Length: ULONG;
        IfIndex: DWORD;[/color]
        [color=green]Next: PIP_ADAPTER_ADDRESSES;
        AdapterName: PChar;
        FirstUnicastAddress: PIP_ADAPTER_UNICAST_ADDRESS;
        FirstAnycastAddress: PIP_ADAPTER_ANYCAST_ADDRESS;
        FirstMulticastAddress: PIP_ADAPTER_MULTICAST_ADDRESS;
        FirstDnsServerAddress: PIP_ADAPTER_DNS_SERVER_ADDRESS;
        DnsSuffix: PWCHAR;
        Description: PWCHAR;
        FriendlyName: PWCHAR;
        PhysicalAddress: array[0..MAX_ADAPTER_ADDRESS_LENGTH - 1] of Byte;
        PhysicalAddressLength: DWORD;
        Flags: DWORD;
        Mtu: DWORD;
        IfType: DWORD;
        OperStatus: IF_OPER_STATUS;
        Ipv6Index: DWORD;
        ZoneIndices: array[0..15] of DWORD;
        FirstPrefix: PIP_ADAPTER_PREFIX;[/color]
        )
  end;
  Mit Zitat antworten Zitat
jackie
(Gast)

n/a Beiträge
 
#9

Re: C- union Struktur in Delphi

  Alt 1. Mai 2005, 15:32
Hallo!
Danke erstmal für den Code!

Im Original erstreckt sich die union Struktur ja nur über Alignment, Length und IfIndex. In eurem Code schließt die case Anweisung aber mehr als nur diese Variablen ein. Das verstehe ich nicht ganz.
  Mit Zitat antworten Zitat
Mephistopheles
(Gast)

n/a Beiträge
 
#10

Re: C- union Struktur in Delphi

  Alt 1. Mai 2005, 15:35
Zitat von jackie:
Hallo!
Danke erstmal für den Code!

Im Original erstreckt sich die union Struktur ja nur über Alignment, Length und IfIndex. In eurem Code schließt die case Anweisung aber mehr als nur diese Variablen ein. Das verstehe ich nicht ganz.
Das ist ja gerade das Problem.

sizeof(ULONGLONG) == 8
sizeof(ULONG) + sizeof(DWORD) == 8

Die Union hat also eine Größe von 8 Byte!

Dabei überlappen sich die Member nur bis IfIndex respektive Alignment. Der Rest muß an das Ende der Union geklatscht werden, weil die Delphi-Syntax es so will
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


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 05:46 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