Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in passing arrays from C# to C++

Tags:

c++

c#

I have an application in which I need to pass an array from C# to a C++ DLL. What is the best method to do it? I did some search on Internet and figured out that I need to pass the arrays from C# using ref. The code for the same:

status = IterateCL(ref input, ref output);

The input and output arrays are of length 20. and the corresponding code in C++ DLL is

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

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

Thanks, Rakesh.

like image 758
Rakesh K Avatar asked Jan 21 '26 18:01

Rakesh K


1 Answers

You need to use:

[DllImport("your_dll")]
public extern void IterateCL([In, MarshalAs(UnmanagedType.LPArray)] int[] arr1, [Out, MarshalAs(UnmanagedType.LPArray)] int[] arr2);
like image 103
logicnp Avatar answered Jan 23 '26 07:01

logicnp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!