私はこのような構造体を持っています
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;
}
それで、私は何を間違っていますか?