Getting Array of struct from IntPtr

前端 未结 3 1280
囚心锁ツ
囚心锁ツ 2020-12-10 02:47

I have some struct like this

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

and I have pointer to

相关标签:
3条回答
  • 2020-12-10 03:30

    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

    0 讨论(0)
  • 2020-12-10 03:32

    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.

    0 讨论(0)
  • 2020-12-10 03:45

    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);
        }
     }
    
    0 讨论(0)
提交回复
热议问题