I have an IntPtr which is a pointer to an array of structs, and I'm trying to convert it to the array.
I tried
Marshal.Copy(srcIntPtr, destIntPtrArray, 0, destIntPtrArray.Length);
but after doing the copy, I'm not able to convert the IntPtr's inside of the destination array to the desired structure, while I can convert the srcIntPtr to the structure, which of course only gives me the data of the first index. It looks like after doing the copy, the destination array contains a bunch of broken IntPtr's
I also tried
var size = Marshal.SizeOf(Marshal.ReadIntPtr(myIntPtr));
for (int i = 0; i < length; i++)
{
IntPtr iP = new IntPtr(myIntPtr.ToInt64() + i * size);
MyStruct ms =
(MyStruct ) Marshal.PtrToStructure(iP, typeof(MyStruct ));
}
which doesn't throw any error, but the data of the array of structs that I get out of my source IntPtr are not accurate.
This is the struct that I'm trying to convert to
struct MyStruct
{
public Categories categories;
public Dimensions dimensions;
}
public struct Categories {
public double d;
public double c;
public double b;
public double a;
}
struct Dimensions {
public double v;
public double e;
}
I have [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] on top of the structs, but removing that doesn't break my code.
Thanks in advance
IntPtrto one single struct can be done without any problemIntPtrintoIntPtr[]and then looped through thatIntPtr[]and tried toMarshal.PtrToStructureeach of them. but it throws an exception at the beginning of the loop and saysObject reference is not set to an object (unknown wrapper)a null exception basically. shall I update the question?