我有一些像这样的结构
struct MyStruct
{
public int field1;
public int field2;
public int field3;
}
Run Code Online (Sandbox Code Playgroud)
我有指向此结构数组的指针.所以,我需要从这个指针获取数组.我试图使用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;
}
Run Code Online (Sandbox Code Playgroud)
那么,我做错了什么?