RECT 構造体の配列 (以下を参照) を IntPtr に変換しようとしているので、PostMessage を使用してポインターを別のアプリケーションに送信できます。
[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
これにより、最後の行で ArgumentException が発生します (「指定された構造体は blittable であるか、レイアウト情報を持っている必要があります。」)。PostMessage を使用して、この RECT の配列を別のアプリケーションに渡す必要があるため、このデータへのポインタが本当に必要です。
ここでのオプションは何ですか?
更新:これはうまくいくようです:
IntPtr result = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.RECT)) * foo.Length);
IntPtr c = new IntPtr(result.ToInt32());
for (i = 0; i < foo.Length; i++)
{
Marshal.StructureToPtr(foo[i], c, true);
c = new IntPtr(c.ToInt32() + Marshal.SizeOf(typeof(Win32.RECT)));
}
アービターがコメントした内容を修正するために再度更新しました。