DllImport期间的FatalExecutionEngineError

时间:2014-04-09 15:14:46

标签: c# c++ pinvoke dllimport

这是我得到执行错误的行:

NimbieImportedFunctions.BS_Robots_Connect(
    out robotCount, out robotIDs, out robotTypes, out robotReadys);

DllImport本身:

[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern UInt32 BS_Robots_Connect(
    out int nRobotCount,
    out int[] pnRobotIDS,
    out int[] pnRobotTypes,
    out bool[] pbRobotReadys);

来自头文件:

extern "C" __declspec(dllexport) DWORD BS_Robots_Connect(
        int *nRobotCount,
        int *pnRobotID,
        int *pnRobotType,
        bool *pnRobotReady
    );
//
//  Description:
//      Function to connect all online robots.
//
//  Parameters:
//      nRobotCount  [out]  Pointer to an integer that receives the number of robots.
//      pnRobotID    [out]  Pointer to an array of integer that receives the robot IDs.
//      pnRobotType  [out]  Pointer to an array of integer that receives the robot types.
//
//  Return:
//      BS_ROBOTS_OK     If the function succeeds with no error.
//      BS_ROBOTS_ERROR  If the function fails with any error occurred.
//
///////////////////////////////////////////////////////////////////////////////

我得到的错误:

The runtime has encountered a fatal error. The address of the error was 
at 0x72c4898e, on thread 0xdf0. The error code is 0xc0000005. 
This error may be a bug in the CLR or in the unsafe or non-verifiable portions
of user code. Common sources of this bug include user marshaling errors for
COM-interop or PInvoke, which may corrupt the stack.

有时候,我也会遇到AccessViolationException。我非常不确定发生了什么。请帮帮我!

1 个答案:

答案 0 :(得分:1)

您的p / invoke不正确。它或许应该是:

[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern uint BS_Robots_Connect(
    out int nRobotCount,
    [Out] int[] pnRobotIDS,
    [Out] int[] pnRobotTypes,
    [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)]
    [Out] bool[] pbRobotReadys
);

nRobotCount应该是ref而不是out,这似乎是合理的。根据您是否需要将信息传递给函数来确定。你确定pbRobotReadys是一个数组吗?如果没有,请使用out boolref bool

所以也许你需要的是:

[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern uint BS_Robots_Connect(
    ref int nRobotCount,
    [Out] int[] pnRobotIDS,
    [Out] int[] pnRobotTypes,
    [MarshalAs(UnmanagedType.U1)]
    out bool pbRobotReadys
);

要了解这一点,您需要更多地学习文档,或者参考您可以找到的任何C ++示例代码。

在调用函数之前,您需要分配数组。我无法从这里看出你需要如何分配数组。大概你知道他们需要多大。


为什么你的版本错了?好吧,考虑这个问题的方法是C#数组已经是一个参考。通过使用out传递数组,您将传递指针指针。换句话说,一个间接层面太远了。

另一种思考方式是,您要求非托管代码创建托管数组。这是显而易见的事情。