Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Bitte um Mithilfe bei Umwandlugn von struct to Delphi record (https://www.delphipraxis.net/170076-bitte-um-mithilfe-bei-umwandlugn-von-struct-delphi-record.html)

JasonClark 29. Aug 2012 17:59

Bitte um Mithilfe bei Umwandlugn von struct to Delphi record
 
Hallo Zusammen

Ich bräuchte mal von einem Experten Hilfe wie ich folgende struct aus C++ in je ein korrektes (unter Delphi XE äquivalentes) Record umwandeln kann?

Code:
typedef struct inter2 {
    char               ipaddr[64];
    unsigned long      port;
    unsigned short     retry;
    unsigned short     timeout;
    unsigned short     alivetime;
    char               dummy1[8];
    inter2_type_prm    cntrl;
    unsigned short     transnum;
    char               dummy2[14];
    inter2_type_prm    trans[3];
} INTER2;

typedef struct inter2_type_prm {
    unsigned short     type;
    char               dummy1[2];
    union {
        typedef {
            unsigned short path;
            short          addr;
            unsigned long  no;
            unsigned long  size;
        } first;
        typedef {
            unsigned short path;
            char           dummy2[2];
            unsigned long  no;
            unsigned long  num;
        } second;
    } prm
} INTER2_TYPE_PRM;
Ich tue mich leider einfach noch schwer an den union bzw. an den verschachtelten typdef nach dem union. :(

Hier ist auch mein Versuch das umzuwandeln:

Delphi-Quellcode:
  TFIRST = record
    path : Word;
    addr : Smallint;
    no   : Cardinal;
    size : Cardinal;
  end;

  TSECOND = record
    path  : Word;
    dummy2 : array[1..2] of AnsiChar;
    no    : Cardinal;
    num   : Cardinal;
  end;

  TINTER2_TYPE_PRM = record
    typ      : Word;
    dummy1    : array[0..1] of AnsiChar;
    case Integer of
      0: ( first: TFIRST );
      1: ( second: TSECOND );
  end;

  TINTER2 = record
    ipaddr   : array[0..63] of AnsiChar;
    port     : Cardinal;
    retry    : Word;
    timeout  : Word;
    alivetime : Word;
    dummy1    : array[0..7] of AnsiChar;
    cntrl    : TINTER2_TYPE_PRM;
    transnum : Word;
    dummy2    : array[0..13] of AnsiChar;
    trans    : array[0..2] of TINTER2_TYPE_PRM;
  end;

Ich frage mich ob ich damit ich mit Namen bei First und Second arbeiten kann, hier unbedingt auch noch die zwei zwischen Records (TFIRST und TSECOND) erstellen muss?

Ich danke euch für eure schnelle Hilfe im Voraus!

Furtbichler 29. Aug 2012 18:05

AW: Bitte um Mithilfe bei Umwandlugn von struct to Delphi record
 
Grundsätzlich ok, Nomenklatur ist eh deine Sache. Separate Records musst Du nicht erstellen, es geht auch so

Delphi-Quellcode:
Type
  TMyUnion = Record
    Foo : Integer;
    Case integer of
      0 : (Bar1, Bar2 : Byte);
      1 : (Bar3       : Word);
  End;

JasonClark 29. Aug 2012 19:49

AW: Bitte um Mithilfe bei Umwandlugn von struct to Delphi record
 
Hallo Furtbichler

Also wenn es grundsätzlich ok ist, heisst das es müsste so auch funktionieren? Ich denke mal ich muss die zwei Zusatzrecords bauen damit ich sie über Ihren Namen ansprechen kann wie z.B.

Delphi-Quellcode:
var
  test : TINTER2;

begin
  ZeroMemory(@test, SizeOf(test));
  Move(FIP[1], test.ipaddr[0], Length(FIP));
  test.port               := 8196;
  test.retry              := 3;
  test.timeout            := 10;
  test.alivetime          := 5;

  test.cntrl.typ          := 2;
  test.cntrl.first.path     := 1;
  test.cntrl.first.addr     := 1;
  test.cntrl.first.no       := 1;
  test.transnum           := 3;
  // parameter[0]
  test.trans[0].typ       := 1;
  test.trans[0].first.path := 1;
  test.trans[0].first.addr := 5;
  test.trans[0].first.no  := 2100;
  test.trans[0].first.size := 50;
  // parameter[1]
  test.trans[1].typ        := 3;
  test.trans[1].second.path := 1;
  test.trans[1].second.no  := 3011;
  test.trans[1].second.num := 1;
  // parameter[2]
  test.trans[2].typ        := 4;
  test.trans[2].second.path := 1;
  test.trans[2].second.no  := 0;
  test.trans[2].second.num := 5;
end;
Oder wie kann ich sie sonst über den Namen ansprechen? Es ist ja sonst auch das problem das path und no etc. doppelt vom Namen im Record vergeben sind und das geht ja folglich nicht wirklich. Wie kann ich denn ohne die zwei Zusatzrecords das in eine Definition schreiben?

Folgendes habe ich probiert und klappt leider nicht:

Delphi-Quellcode:
 
  TINTER2_TYPE_PRM = record
    typ : Word;
    dummy1 : array[0..1] of AnsiChar;
    case Integer of
      0: ( first = record
            path : Word;
            addr : Smallint;
            no : Cardinal;
            size : Cardinal;
           end; );
      1: ( second = record
            path : Word;
            dummy2 : array[1..2] of AnsiChar;
            no : Cardinal;
            num : Cardinal;
           end; );
  end;
Danke nochmals für deine Hilfe!

Furtbichler 30. Aug 2012 07:09

AW: Bitte um Mithilfe bei Umwandlugn von struct to Delphi record
 
Hast schon recht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:35 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