1 回答
TA贡献1877条经验 获得超1个赞
您不能与 C++ 类(例如std::vector)进行互操作,只能与基本的 C 样式数据类型和指针进行互操作。(作为旁注)这是 Microsoft 在发明 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...*/
);
}
- 1 回答
- 0 关注
- 140 浏览
添加回答
举报