Convert C++ struct to C#

后端 未结 2 1438
遥遥无期
遥遥无期 2020-12-17 00:11

I have a C++ struct below:

struct CUSTOM_DATA {
   int id;
   u_short port;
   unsigned long ip;
} custom_data;

How can i convert it to C#

相关标签:
2条回答
  • 2020-12-17 00:59

    The C# version of this struct would be:

    [StructLayout(LayoutKind.Sequential)]
    public struct CustomData
    {
        public int id;
        public ushort port;
        public uint ip;
    }
    

    As for sending this via a socket, you can just send the binary data directly. The Marshal class has methods for getting a pointer (IntPtr) from the structure and copying into a byte array.

    0 讨论(0)
  • 2020-12-17 01:09
    [StructLayout(LayoutKind.Sequential)]
    struct CUSTOM_DATA {
       int id;
       ushort port;
       uint ip;
    };
    CUSTOM_DATA cData ; // use me 
    

    edit: thx reed

    0 讨论(0)
提交回复
热议问题