我试图转换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