Let's say I'd like to call an unmanaged C++ function from C#. C++ function has the following prototype:
int doSomething(int arraySize, int* array)
And I call the function from C# as following:
[DllImport(MyLibrary, CallingConvention = CallingConvention.Cdecl)]
public static int doSomething(int arraySize, int[] array)
int[] myArray = new int[] { 1, 2, 3 };
doSomething(myArray.Length, myArray);
- When I do this, is
myArray
pinned? Is there any chance that garbage collector may move the array somewhere else while the unmanaged library is working on it? - If it's not automatically pinned, what should I do to pin it?
- If it is pinned automatically, when is it unpinned again?