title: Struct marshal in C#
body: <p>I have the following struct in C#</p>
  
  <pre><code>unsafe public struct control
      {
          public int bSetComPort;
          public int iComPortIndex;
          public int iBaudRate;
          public int iManufactoryID;
          public byte btAddressOfCamera;
          public int iCameraParam;
          public byte PresetNum;
          public byte PresetWaitTime;
          public byte Group;
          public byte AutoCruiseStatus;
          public byte Channel;
          public fixed byte Data[64];
      }
  </code></pre>
  
  <p>And the function i use to convert it to byte array[] is </p>
  
  <pre><code> static byte[] structtobyte(object obj)
      {
          int len = Marshal.SizeOf(obj);
          byte[] arr = new byte[len];
          IntPtr ptr = Marshal.AllocHGlobal(len);
          Marshal.StructureToPtr(obj, ptr, true);
          Marshal.Copy(ptr, arr, 0, len);
          Marshal.FreeHGlobal(ptr);
          return arr;
      }
  </code></pre>
  
  <p>When i compile it gives</p>
  
  <pre><code>Type 'System.Byte[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
  </code></pre>
  
  <p>What can be the problem?
  Thanks in advance!</p>