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:
I've managed to create the T_UDP_pkt and T_UDP_Header strutures in C# with
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
I get an error about using a type when a variable is expected, or if I use
I get an error about the value not being constant. And if I use
I get an error saying I can't get the size of a managed type. Finally if I use
s
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?!
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));
Code:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[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)
Code:
Marshal.Sizeof(typeof(T_UDP_pkt))
Code:
sizeof(T_UDP_pkt)
s
Code:
izeof(typeof(T_UDP_pkt))
So I'm a bit stumped. Any help would be muich appreciated, as I'm sure it must be reasonably easy?!
Comment