我正在使用 System.Runtime.InteropServices 从我的 C# 应用程序调用几个用 C++ 编写的函数。我只是遇到了返回数组的特定函数的问题。
我已经看到我的函数不应该返回任何东西,并且指向“返回变量”的指针应该是入口。但我没有设法正确地做到这一点。
例如,如果我在 C++ 中有一个函数
void func(double *y, double *x){...}
操作数组 x 并返回数组 y。
我在做:
-在我的 .h 中:
extern "C" __declspec(dllexport) void func(double *y,double *x);
-在我的 .cpp 中:
__declspec(dllexport) void func(double *y,double *x){...}
-在我的 C# 代码中:
static class AnyClass
{
[DllImport(dllPath)]
public extern static void func(out double[] y, double[] x);
int otherfunc
{
double[] x = new double[5];
double[] y = new double[5];
...
func(out y, x);
}
}
但它给了我一个 System.EntryPointNotFoundException。
有什么线索吗?
最佳答案
EntryPointNotFoundException 意味着在您的 DLL 中没有找到所谓的“函数”。
在您的 .h 文件中,您将其称为“func”。但是在您的 .cpp 文件中,您称它为“函数”。因为你的 .h 文件是唯一声明 extern "C"
的地方,实际上发生的是你的 DLL c++-style-name-mangled 导出的函数,而不是普通的-c-风格。因此,当 C# 寻找纯 C 风格的“函数”时,它找不到。
关于c# - 从 C# 包装 C++ DLL 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8660403/