I have a structure like this
struct MyStruct { public int field1; public int field2; public int field3; }
and I have a pointer to an array of this structure. So, I need to get an array from this pointer. I am trying to use Marshal.PtrToStructure, but I have a memory read error. This is my method:
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?
Moroz
source share