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 關注
- 187 瀏覽
添加回答
舉報