Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to byte array received from socket, to C# structure
    primarykey
    data
    text
    <p>I have to Develop a Service (C#) which read data from Network Device via TCP Socket and convert this is C# structure.</p> <p>I am basing on existing, old Delphi application which is doing all this stuff and I have to migrate logic in C#. </p> <p>EDITED: I got a snapshot from C-Source of original data-structure:</p> <pre><code>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)); </code></pre> <p>There is Delphi Packed record with over 200 fields of different types... it look approximately like:</p> <pre><code> 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; </code></pre> <p>To move byte array to structure in Delphi there is next code:</p> <pre><code>Move(inBytesArr[StartIdx], DelphiStruct, aMsgSize) </code></pre> <p>To convert string files (e.g. SERIAL_NUMBER) there is also such code:</p> <pre><code>var pc: Pchar; ... pc := @inBytesArr[StartIdx + SerialN_Pos_Idx]; DelphiStruct.SERIAL_NUMBER := pc; </code></pre> <p>I having deal with such conversion for first time and I don't know from where to start:</p> <ul> <li><p>How to convert this structure to c#? -- Should I use <code>LayoutKind.Sequential</code> or <code>LayoutKind.Explicit</code>, with or wiyhout <code>[FieldOffset(N)]</code> attribute? -- How I have to declare array of bytes in target c# structure: as <code>fixed</code> buffer or using <code>[MarshalAs(UnmanagedType.ByValArray...)]</code> attribute?</p></li> <li><p>Which is better way to marshal input bytes array to final C# structure: using <code>Marshal.PtrToStructure</code> or GCHandle.Alloc(bytes, GCHandleType.Pinned) + <code>AddrOfPinnedObject</code>?</p></li> </ul> <p>Please help me, at least, to get start point in understating from where i need to start.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload