我有一个方法签名为的c++函数
MyMethod(std::vector<double> tissueData, std::vector<double> BGData, std::vector<double> TFData, std::vector<double> colMeans, std::vector<double> colStds, std::vector<double> model)
我希望通过 dllimport 在 c# 中调用此 c++ 函数。在创建 dll 库时,我从 C++ 端将函数定义为
extern "C" __declspec(dllexport) int MyMethod(double *tissue, double *bg, double *tf, double *colMeans, double *colStds, double* model);
我计划将一个 double 组从 c# 端传递给 c++ dll 函数。 但是,我不确定应该如何从 C# 端定义 DllImport,以及在将 double 组解析为 dllImport 函数时应该如何转换它?
我读了一些关于编码的内容,但我还是不太明白,我不确定它是否可以在这里应用?
最佳答案
您不能与 C++ 类(例如 std::vector
)互操作,只能与基本的 C 风格数据类型和指针互操作。 (附带说明)这是微软在发明 COM 时试图解决的问题之一。
要让它工作,您应该导出一个不同的函数,它接收纯 C 数组及其各自的长度:
C++ 方面
extern "C" __declspec(dllexport) int MyExternMethod(
double *tissue, int tissueLen,
double *bg, int bgLen,
/* ... the rest ... */
);
// implementation
int MyExternMethod(
double* tissue, int tissueLen,
double* bg, int bgLen,
/* ... the rest ... */ )
{
// call your original method from here:
std::vector<double> tissueData(tissue, tissue + tissueLen);
std::vector<double> bgData(bg, bg + bgLen);
/* ... the rest ... */
return MyMethod(tissueData, bgData, /* ...the rest... */);
}
C# 端的互操作导入将是:
C# 端
public static class MyLibMethods
{
[DllImport("MyLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int MyExternMethod(
double[] tissue, int tissueLen,
double[] bg, int bgLen,
/*...the rest...*/
);
}
您可以像这样在 C# 中调用它:
C# 端
public int CallMyExternMethod(double[] tissue, double[] bg, /*... the rest ...*/)
{
return MyLibMethods.MyExternMethod(
tissue, tissue.Length,
bg, bg.Length,
/*...the rest...*/
);
}
关于c# - 如何通过 DllImport 将 double 组从 C# 传递到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247313/