将数组从C#传递到C ++的问题 [英] Problem in passing arrays from C# to C++

查看:139
本文介绍了将数组从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);

这篇关于将数组从C#传递到C ++的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆