从 IntPtr 获取结构体数组

发布于 2024-11-25 01:22:37 字数 697 浏览 2 评论 0原文

我有一些像这样的结构

struct MyStruct
{
    public int field1;
    public int field2;
    public int field3;
}

,并且有指向该结构数组的指针。 所以,我需要从这个指针获取数组。 我尝试使用 Marshal.PtrToStructure,但出现内存读取错误。 这是我的方法:

public MyStruct[] GetArrayOfStruct(IntPtr pointerToStruct, int length)
{
    var sizeInBytes = Marshal.SizeOf(typeof(TCnt));
    MyStruct[] output = new MyStruct[length];

    for (int i = 0; i < length; i++)
    {
        IntPtr p = new IntPtr((pointerToStruct.ToInt32() + i * sizeInBytes));

        output[i] = (MyStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(MyStruct));
    }

    return output;
}

那么,我做错了什么?

I have some struct like this

struct MyStruct
{
    public int field1;
    public int field2;
    public int field3;
}

and I have pointer to array of this struct.
So, I need to get array from this pointer.
I'm tried to using Marshal.PtrToStructure, but i had memory reading error.
This is my methode:

public MyStruct[] GetArrayOfStruct(IntPtr pointerToStruct, int length)
{
    var sizeInBytes = Marshal.SizeOf(typeof(TCnt));
    MyStruct[] output = new MyStruct[length];

    for (int i = 0; i < length; i++)
    {
        IntPtr p = new IntPtr((pointerToStruct.ToInt32() + i * sizeInBytes));

        output[i] = (MyStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(MyStruct));
    }

    return output;
}

So, what am i doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情废物 2024-12-02 01:22:37

假设结构的大小是固定的,这个函数对我有用:

public static void MarshalUnmananagedArray2Struct<T>(IntPtr unmanagedArray, int length, out T[] mangagedArray)
{
    var size = Marshal.SizeOf(typeof(T));
    mangagedArray = new T[length];

    for (int i = 0; i < length; i++)
    {
        IntPtr ins = new IntPtr(unmanagedArray.ToInt64() + i * size);
        mangagedArray[i] = Marshal.PtrToStructure<T>(ins);
    }
 }

This function worked for me, assuming that the size of the struct is fixed:

public static void MarshalUnmananagedArray2Struct<T>(IntPtr unmanagedArray, int length, out T[] mangagedArray)
{
    var size = Marshal.SizeOf(typeof(T));
    mangagedArray = new T[length];

    for (int i = 0; i < length; i++)
    {
        IntPtr ins = new IntPtr(unmanagedArray.ToInt64() + i * size);
        mangagedArray[i] = Marshal.PtrToStructure<T>(ins);
    }
 }
流殇 2024-12-02 01:22:37

两个问题。您在 Marshal.SizeOf() 调用中使用 TCnt 而不是 MyStruct。您的 IntPtr 算术无法在 64 位计算机上运行,​​您必须使用 IntPtr.ToInt64() 或强制转换为 (long)。

当然,得到错误的 IntPtr 或长度也是有可能的。使用 Debug + Windows + Memory + Memory 1 并将“pointerToStruct”放入地址框中进行基本验证。

Two problems. You use TCnt instead of MyStruct in the Marshal.SizeOf() call. Your IntPtr arithmetic cannot work on a 64-bit machine, you must use IntPtr.ToInt64() or cast to (long).

Just getting the wrong IntPtr or length is certainly a possibility too of course. Use Debug + Windows + Memory + Memory 1 and put "pointerToStruct" in the Address box for basic verification.

怎言笑 2024-12-02 01:22:37

C 和 C# 中的结构不是一回事。区别之一是,在 C# 中,您必须明确要求您的结构应按顺序布局。如果你没写
[StructLayout(LayoutKind.Sequential)][StructLayout(LayoutKind.Explicit)] 属性到您的结构我不相信您可以通过这种方式管理它。 Microsoft 声明 PtrToStructure 用于将结构从非托管内存转换为托管内存。

您应该测试将此属性添加到结构中是否有帮助,如果还没有帮助,请尝试使用 Marshal 分配内存.AllocHGlobal(IntPtr) 并使用 Marshal.Copy 初始化您的结构,然后尝试使用 PtrToStructure。如果这有效,那么您不能将 PtrToStructure 与托管内存一起使用

Structs in C and C# are not the same thing. One of the differences is that in C# you have to explicitly demand that your struct should be sequentially laid out. If you didn't write
[StructLayout(LayoutKind.Sequential)] or [StructLayout(LayoutKind.Explicit)] attribute to your structure I don't believe that you can manage it in this way. Microsoft states that PtrToStructure is to be used to convert structures from unmanaged to managed memory

You should test if adding this attributes to your struct helps, If it doesn't yet help try allocating memory with Marshal.AllocHGlobal(IntPtr) and use Marshal.Copy to init your structure and then try using PtrToStructure. If this works then you can't use PtrToStructure with managed memory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文