简体   繁体   中英

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

the C++ function signature is:

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

the C# function signature is:

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

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.

The C++ code should be:

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

The C# code should be:

[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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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