I need to convert a double array in c# to an IntPtr to properly send it to my c DLL. I have successfully been able to convert from IntPtr to double[,] using the method from this answer. Ive also found another answer that is close to what I need but deals with 1D arrays.
Im missing something in my logic and get the error code after a crash: Managed ' has exited with code -1073740940 (0xc0000374).
This is what I have so far
IntPtr p = Marshal.AllocHGlobal(rows * columns);
for (int i = 0; i < rows; i++) //rows
{
double[] temp = new double[columns];
for (int x = 0; x < columns; x++)
temp[x] = input.cells[i, x];
Marshal.Copy(temp, 0, p, columns);
p = (IntPtr)(p.ToInt64() + IntPtr.Size);
}
toReturn.cells = p;
Marshal.FreeHGlobal(p);
return toReturn;
toReturn.cells is my IntPtr inside a struct that I return. cells is structured as cells[rows, columns]
IntPtr's are still very new to me.
Edit: thanks to harold. their suggestions worked beautifully