将数组从C#传递到C++;库并返回数组
我试图包装C++库,用于在C.W.WPF项目中使用。作为其中的一部分,我需要通过并返回从C到C++的大数组,并返回一个大数组。通过阵列似乎是这样实现的:将数组从C#传递到C++;库并返回数组,c#,c++,arrays,interop,C#,C++,Arrays,Interop,我试图包装C++库,用于在C.W.WPF项目中使用。作为其中的一部分,我需要通过并返回从C到C++的大数组,并返回一个大数组。通过阵列似乎是这样实现的: namespace CppWrapper { public ref class CppWrapperClass { public: void DoStuff(int* pInt, int arraySize); private: computingClass* p
namespace CppWrapper {
public ref class CppWrapperClass
{
public:
void DoStuff(int* pInt, int arraySize);
private:
computingClass* pCC;
};
}
int noElements = 1000;
int[] myArray = new int[noElements];
for (int i = 0; i < noElements; i++)
myArray[i] = i * 10;
unsafe
{
fixed (int* pmyArray = &myArray[0])
{
CppWrapper.CppWrapperClass controlCpp = new CppWrapper.CppWrapperClass();
controlCpp.DoStuff(pmyArray, noElements);
}
}
然后这样叫:
namespace CppWrapper {
public ref class CppWrapperClass
{
public:
void DoStuff(int* pInt, int arraySize);
private:
computingClass* pCC;
};
}
int noElements = 1000;
int[] myArray = new int[noElements];
for (int i = 0; i < noElements; i++)
myArray[i] = i * 10;
unsafe
{
fixed (int* pmyArray = &myArray[0])
{
CppWrapper.CppWrapperClass controlCpp = new CppWrapper.CppWrapperClass();
controlCpp.DoStuff(pmyArray, noElements);
}
}
int-noElements=1000;
int[]myArray=新的int[noElements];
for(int i=0;i
如何返回数组
注:如能就实现这一目标的最佳方式提供任何反馈,我将不胜感激。很奇怪,从C到C++,这并不容易。我发现用JNI包装库要容易得多一般来说,C++C++有几种接口方式。使用C++/CLI,您可以使用IJW“Itjustworks”来避免大量的样板文件。同时也要注意STD库问题,所以你不使用一个版本的STDLIB来进行MALOC/NEW,然后免费与另一个版本进行删除。你的C++代码是什么样的库?这个问题使我有点困惑。第一个代码块看起来像C++类声明,但是第二个代码块看起来是C。但是你不能直接这样称呼它。C++代码管理的C++还是C++ + CLI?将它更改为<代码>无效DoStuff(数组^ ARR);<代码>去掉所有不必要的C代码,只需添加对DLL的引用并直接调用DoStuff()。足够简单吗?如果由于外部依赖而无法使用CLI,您仍然可以像这样传递数组: