简体   繁体   English

如何将双精度数组从C#传递到C ++(DLL)

[英]How to pass Array of doubles from C# to C++ (DLL)

the C++ function signature is: C ++函数签名为:

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

the C# function signature is: 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)

the code is compiling good but there is an "AccessViolationexception" while passing the double array to the function. 代码编译良好,但是在将双精度数组传递给函数时出现了“ AccessViolationexception”。

You cannot call that function from C#. 您不能从C#调用该函数。 It accepts a std::string which cannot be used for interop. 它接受一个不能用于互操作的std::string You also omitted two parameters from your C# translation. 您还从C#转换中省略了两个参数。

The C++ code should be: C ++代码应为:

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

The C# code should be: 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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM