将数组从C#传递到C ++的问题

问题描述:

我有一个应用程序,我需要将一个数组从C#传递到C ++ DLL。什么是最好的方法呢?我在互联网上做了一些搜索,并想出我需要从C#使用ref传递数组。代码同:

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);

输入和输出数组长度为20.而C ++ DLL中的相应代码为

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

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

这个工作正常一次。但是如果我第二次尝试从一个循环中调用C#中的函数,C#中的输入数组显示为一个元素的数组。为什么这会发生,请帮助我如何可以从C#迭代调用此函数。

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#.

谢谢,
Rakesh。

Thanks, Rakesh.

您需要使用:

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