Hi all!
I have jagged array which I need to pass to external method.
[DllImport(...)]
private static extern int NativeMethod(IntPtr[] ptrArray);
...
fixed (ulong* ptr = array[0])
{
for (int i = 0; i < array.Length; i++)
{
fixed (ulong* p = &array[i][0])
{
ptrArray[i] = new IntPtr(p);
}
}
NativeMethod(ptrArray);
}
The problem is that ptr is unused and is removed due compilation. Than fixed statement according to it is removed too. So array be moved by GC in that way that ptrArray elements become invalid.
What is the best way for passing jagged arrays as single-dimensional arrays of pointers to native methods?
Update:
Here is the C++ code for NativeMethod:
NativeClass::NativeMethod(const int* array)