I have to Develop a Service (C#) which read data from Network Device via TCP Socket and convert this is C# structure.
I am basing on existing, old Delphi application which is doing all this stuff and I have to migrate logic in C#.
EDITED: I got a snapshot from C-Source of original data-structure:
struct _RequestMsgStruct
{
UCHAR request_ver; //In DELPHI it is represented as Byte
USHORT mac_addr[3]; /* MAC Address */
UINT product_type; //In DELPHI - Cardinal
UCHAR supply_type; //In DELPHI - Byte
short reserved0; //In DELPHI - SmallInt
UCHAR oper_ver[4]; //In DELPHI - CARDINAL !!!
USHORT brd_id; //In DELPHI - WORD
unsigned short exp_id1; //In DELPHI - WORD
//In DELPHI - string[15]; //Array [0..15] of char;
UCHAR serial_no[16]; /* Serial Number. 16th char have to be NULL */
UCHAR _name[32]; /* Name */ //Length of payload may vary //In DELPHI - string[31]
float data_avg; //In DELPHI - Single
ULONG key[5]; //In DELPHI - array [0..19] of Byte
}__attribute__ ((packed));
There is Delphi Packed record with over 200 fields of different types... it look approximately like:
TREC_DATA = packed record
ID : Byte;
MAC_ADDRESS : array [0..5] of Byte;
fieldCard : cardinal;
fieldSI : SmallInt;
fieldW : WORD;
SERIAL_NUMBER : string[15]; //Array [0..15] of char;
fieldSingle : Single;
fieldArrOfB : array [0..19] of Byte;
end;
To move byte array to structure in Delphi there is next code:
Move(inBytesArr[StartIdx], DelphiStruct, aMsgSize)
To convert string files (e.g. SERIAL_NUMBER) there is also such code:
var
pc: Pchar;
...
pc := @inBytesArr[StartIdx + SerialN_Pos_Idx];
DelphiStruct.SERIAL_NUMBER := pc;
I having deal with such conversion for first time and I don't know from where to start:
How to convert this structure to c#? -- Should I use
LayoutKind.SequentialorLayoutKind.Explicit, with or wiyhout[FieldOffset(N)]attribute? -- How I have to declare array of bytes in target c# structure: asfixedbuffer or using[MarshalAs(UnmanagedType.ByValArray...)]attribute?Which is better way to marshal input bytes array to final C# structure: using
Marshal.PtrToStructureor GCHandle.Alloc(bytes, GCHandleType.Pinned) +AddrOfPinnedObject?
Please help me, at least, to get start point in understating from where i need to start.
string[]types.