sizeof structure at compile time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AliSmith
    New Member
    • Mar 2010
    • 2

    sizeof structure at compile time

    Hi,

    I'm new to C#, and am converting a C++ app which interfaces with a legacy Delphi app. We pass messages between the two apps over UDP, and my colleague is writing a conversion utility to take his Delphi message structure definitions and produce the equivalent in C#. However we have hit upon several issues, which I can't find a way round. The main one seems though to be when using the size of a previously defined message structure to define a constant for use elsewhere. So, in Delphi we have:

    Code:
    type
      T_UDP_pkt = array[0..MAX_UDP_PACKET_SIZE - 1] of char;
      T_UDP_header = packed record
        From_MAC: T_MAC;
        From_Area_ID: word; // 0= PC
        Signature: array[0..5] of char;
        Pkt_count: word; // Total number of packets in this set
        Pkt_number: word; // Packet Number
        Transaction: longword;
        spare1: array[0..5] of byte;
        AC_to_AC: array[0..3] of byte;
        datalen: longword; // length of data block following header
        Code: longword;
        Last_received_transaction: longword;
        Hash: TDigest;
        AC_WAN_Port: word;
        Spare2: array[0..7] of byte;
      end;
    const MAX_UDP_DATA_SIZE = (sizeof(T_UDP_pkt) - sizeof(T_UDP_header));
    I've managed to create the T_UDP_pkt and T_UDP_Header strutures in C# with

    Code:
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    and
    [MarshalAs(Unman agedType.ByValA rray, SizeConst = x)] commands,

    but I can't define the constant MAX_UDP_DATA_SI ZE as if I use

    Code:
    Marshal.Sizeof(T_UDP_pkt)
    I get an error about using a type when a variable is expected, or if I use

    Code:
    Marshal.Sizeof(typeof(T_UDP_pkt))
    I get an error about the value not being constant. And if I use

    Code:
    sizeof(T_UDP_pkt)
    I get an error saying I can't get the size of a managed type. Finally if I use

    s
    Code:
    izeof(typeof(T_UDP_pkt))
    I get an error that typeof(T_UDP_pk t) is not a type.

    So I'm a bit stumped. Any help would be muich appreciated, as I'm sure it must be reasonably easy?!
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • AliSmith
      New Member
      • Mar 2010
      • 2

      #3
      Thanks, sorry about that, tlhintoq, next time!

      Comment

      Working...