Problem in passing arrays from C # to C ++

I have an application in which I need to pass an array from C # to a C ++ DLL. What is the best way to do this? I did a search on the Internet and realized that I needed to pass arrays from C # using ref. Code for it:

status = IterateCL(ref input, ref output);

Input and output arrays have a length of 20. Corresponding code in the C ++ DLL

IterateCL(int *&inArray, int *&outArray)

This works great once. But if I try to call a function from C # in a loop for the second time, the input array in C # is displayed as an array of one element. Why is this happening and please help me how can I call this function iteratively with C #.

Thanks, Rakesh.

+3
source share
2 answers

You need to use:

[DllImport("your_dll")]
public extern void IterateCL([In, MarshalAs(UnmanagedType.LPArray)] int[] arr1, [Out, MarshalAs(UnmanagedType.LPArray)] int[] arr2);
+1
source

, fixed:

fixed (int* arr1 = new int[10], arr2 = new int[10])
{
            //acting with arr1 arr2 as you wish
}

[DllImport("your_dll")]
public extern void IterateCL([In, MarshalAs(UnmanagedType.LPArray)] int[] arr1, [Out, MarshalAs(UnmanagedType.LPArray)] int[] arr2);
+2

Source: https://habr.com/ru/post/1736058/


All Articles