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);
}

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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...