如何将双精度数组从C#传递到C ++(DLL) [英] How to pass Array of doubles from C# to C++ (DLL)

查看:371
本文介绍了如何将双精度数组从C#传递到C ++(DLL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++函数签名为:

the C++ function signature is:

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".

the code is compiling good but there is an "AccessViolationexception" while passing the double array to the function.

推荐答案

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

You cannot call that function from C#. It accepts a std::string which cannot be used for interop. You also omitted two parameters from your C# translation.

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
)

这篇关于如何将双精度数组从C#传递到C ++(DLL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆