2

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);
  1. 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?
  2. If it's not automatically pinned, what should I do to pin it?
  3. If it is pinned automatically, when is it unpinned again?
hattenn
  • 4,371
  • 9
  • 41
  • 80

0 Answers0