如何将C#中的双精度数组传递给C ++(DLL)

时间:2015-01-27 11:07:45

标签: c# c++ pinvoke marshalling dllimport

C ++函数签名是:

int Eye_GetPositionSC2(std::string fname_mob, double sensors[9], int &map_x, int &map_y)

C#函数签名是:

[DllImport(@"eyeWhere.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern int Eye_GetPositionSC2([MarshalAs(UnmanagedType.LPWStr)]string filename, [In , MarshalAs(UnmanagedType.LPArray)]double[] sensors)

代码编译良好,但在将double数组传递给函数时存在“AccessViolationexception”。

1 个答案:

答案 0 :(得分:0)

您无法从C#调用该函数。它接受std::string,不能用于互操作。您还省略了C#转换中的两个参数。

C ++代码应为:

int Eye_GetPositionSC2(
    const wchar_t* filename, 
    double sensors[9], 
    int &map_x, 
    int &map_y
)

C#代码应为:

[DllImport(@"eyeWhere.dll", CallingConvention = CallingConvention.Cdecl,
    CharSet = CharSet.Unicode)]
public static extern int Eye_GetPositionSC2(
    string filename, 
    [In, MarshalAs(UnmanagedType.LPArray, SizeConst = 9)]
    double[] sensors,
    ref int map_x,
    ref int map_y
)