1 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
您不能與 C++ 類(例如std::vector)進(jìn)行互操作,只能與基本的 C 樣式數(shù)據(jù)類型和指針進(jìn)行互操作。(作為旁注)這是 Microsoft 在發(fā)明 COM 時(shí)試圖解決的問題之一。
為了使其工作,您應(yīng)該導(dǎo)出一個(gè)不同的函數(shù),該函數(shù)接收純 C 數(shù)組及其各自的長度:
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# 端的互操作導(dǎo)入為:
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# 中這樣調(diào)用它:
C#端
public int CallMyExternMethod(double[] tissue, double[] bg, /*... the rest ...*/)
{
return MyLibMethods.MyExternMethod(
tissue, tissue.Length,
bg, bg.Length,
/*...the rest...*/
);
}
- 1 回答
- 0 關(guān)注
- 191 瀏覽
添加回答
舉報(bào)