title: Convert array of structs to IntPtr
body: <p>I am trying to convert an array of the RECT structure (given below) into an IntPtr, so I can send the pointer using PostMessage to another application.</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct RECT
  {
      public int Left;
      public int Top;
      public int Right;
      public int Bottom;
  
      // lots of functions snipped here
  }
  
  // so we have something to send, in reality I have real data here
  // also, the length of the array is not constant
  RECT[] foo = new RECT[4]; 
  IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(foo[0]) * 4);
  Marshal.StructureToPtr(foo, ptr, true); // -- FAILS
  </code></pre>
  
  <p>This gives an ArgumentException on the last line ("The specified structure must be blittable or have layout information."). I need to somehow get this array of RECTs over to another application using PostMessage, so I really need a pointer to this data.</p>
  
  <p>What are my options here?</p>
  
  <p><em>UPDATE</em>: This seems to work:</p>
  
  <pre><code> IntPtr result = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.RECT)) * foo.Length);
   IntPtr c = new IntPtr(result.ToInt32());
   for (i = 0; i &lt; foo.Length; i++)
   {
       Marshal.StructureToPtr(foo[i], c, true);
       c = new IntPtr(c.ToInt32() + Marshal.SizeOf(typeof(Win32.RECT)));
   }
  </code></pre>
  
  <p><em>UPDATED AGAIN</em> to fix what arbiter commented on.</p>