title: Convert C++ struct to C#
body: <p>I have a C++ struct below:</p>
  
  <pre><code>struct CUSTOM_DATA {
     int id;
     u_short port;
     unsigned long ip;
  } custom_data;
  </code></pre>
  
  <p>How can i convert it to C# struct, serialize it and send via tcp socket?</p>
  
  <p>Thanks!</p>
  
  <p><strong>upd</strong></p>
  
  <p>So C# code will be?</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct CustomData
  {
   public int id;
   public ushort port;
   public uint ip;
  }
  
  public void Send()
  {
   CustomData d = new CustomData();
   d.id = 12;
   d.port = 1000;
   d.ip = BitConverter.ToUInt32(IPAddress.Any.GetAddressBytes(), 0);
   IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(d));
   Marshal.StructureToPtr(d, pointer, false);
   byte[] data_to_send = new byte[Marshal.SizeOf(d)];
   Marshal.Copy(pointer, data_to_send, 0, data_to_send.Length);
   client.GetStream().Write(data_to_send, 0, data_to_send.Length);
  }
  </code></pre>