微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 将结构数组转换为IntPtr

我正在尝试将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)));
 }

再次修正了仲裁员评论错误.

解决方法

StructuretoPtr需要struct对象,而foo不是数组的结构,也就是为什么会发生异常.

我可以建议你在循环中写结构(可惜的是,StructuretoPtr没有重载Index):

long LongPtr = ptr.ToInt64(); // Must work both on x86 and x64
for (int I = 0; I < foo.Length; I++)
{
    IntPtr RectPtr = new IntPtr(LongPtr);
    Marshal.StructuretoPtr(foo[I],RectPtr,false); // You do not need to erase struct in this case
    LongPtr += Marshal.SizeOf(typeof(Rect));
}

另一种选择是使用Marshal.WriteInt32将结构写入四个整数:

for (int I = 0; I < foo.Length; I++)
{
    int Base = I * sizeof(int) * 4;
    Marshal.WriteInt32(ptr,Base + 0,foo[I].Left);
    Marshal.WriteInt32(ptr,Base + sizeof(int),foo[I].Top);
    Marshal.WriteInt32(ptr,Base + sizeof(int) * 2,foo[I].Right);
    Marshal.WriteInt32(ptr,Base + sizeof(int) * 3,foo[I].Bottom);
}

最后,您可以使用不安全的关键字,并直接使用指针.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐