Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8597117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:50:31+00:00 2026-06-12T00:50:31+00:00

I have to Develop a Service (C#) which read data from Network Device via

  • 0

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.Sequential or LayoutKind.Explicit, with or wiyhout [FieldOffset(N)] attribute?
    — How I have to declare array of bytes in target c# structure: as fixed buffer or using [MarshalAs(UnmanagedType.ByValArray...)] attribute?

  • Which is better way to marshal input bytes array to final C# structure: using Marshal.PtrToStructure or GCHandle.Alloc(bytes, GCHandleType.Pinned) + AddrOfPinnedObject?

Please help me, at least, to get start point in understating from where i need to start.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:50:32+00:00Added an answer on June 12, 2026 at 12:50 am

    By default, Delphi’s packed records align fields by single byte boundary.
    Hence, you should use something like this:

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    struct TREC_DATA
    {
          public byte ID;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
          public byte[] MAC_ADDRESS;
          public uint fieldCard;
          public short fieldSI;
          public ushort fieldW;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
          public byte[] SERIAL_NUMBER;
          public float fieldSingle;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
          public byte[] fieldArrOfB;    
    } 
    

    The only thing I’m not sure (and can’t test now without Deplhi), is a SERIAL_NUMBER field.

    After your update: in original, SERIAL_NUMBER is just a null-terminated string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I develop service with WebApi and Client which send to this service gzipped data
i have to develop windows service which will copy files to different servers. So
I have to develop a application in .net technology which calls an external service
I have to develop an application, which will request to a web service developed
I need to develop a service which can send and receive fax. I have
I have to develop a service which will continuously listen for broadcasted messages over
I have developed win service program which reads a excel file from my local
I have to develop a file server service that transfer file form a specified
I started using emacs and slime to develop some little service. I have found
I have to develop a application (Winforms-application) which is connected to a database within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.