我試圖轉換RECT結構(以下給出)轉換成一個IntPtr的陣列,所以我可以發送使用PostMessage的到另一個應用程序的指針。轉換陣列結構到IntPtr的
[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或具有佈局信息。」)。我需要以某種方式將這個RECT數組傳遞給另一個使用PostMessage的應用程序,所以我確實需要一個指向這些數據的指針。
我在這裏有什麼選擇?
UPDATE:這似乎工作:
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)));
}
修訂AGAIN解決什麼仲裁者評論。
什麼消息,你張貼的自動的4個RECTS數組做跨進程封送處理? – 2009-07-06 10:39:42
我試圖告訴一個DLL(它是在另一個進程中託管的,因爲它是64位)忽略屏幕的某些區域。它不一定是4個RECT。 – 2009-07-06 10:45:06