使用委托调用C ++函数时VS2015应用程序崩溃

时间:2017-03-30 12:51:43

标签: c++ delegates

我正在使用ABBYY的FineReader引擎,在尝试初始化时,我的应用程序崩溃(在调试时)。如果我运行.exe它可以正常工作。

声明:

<DllImport("kernel32.dll")>
Private Shared Function LoadLibrary(dllToLoad As String) As IntPtr
End Function
<DllImport("kernel32.dll")>
Private Shared Function GetProcAddress(hModule As IntPtr, procedureName As String) As IntPtr
End Function
<DllImport("kernel32.dll")>
Private Shared Function FreeLibrary(hModule As IntPtr) As Boolean
End Function

' FREngine.dll functions
<UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet:=CharSet.Unicode)>
Private Delegate Function GetEngineObject(devSN As String, reserved1 As String, reserved2 As String, ByRef engine As FREngine.IEngine) As Integer
<UnmanagedFunctionPointer(CallingConvention.StdCall)>
Private Delegate Function DeinitializeEngine() As Integer
<UnmanagedFunctionPointer(CallingConvention.StdCall)>
Private Delegate Function DllCanUnloadNow() As Integer

并初始化:

 ' Load the FREngine.dll library
    dllHandle = LoadLibrary(enginePath)

    Try
        If dllHandle = IntPtr.Zero Then
            Throw New Exception("Can't load " + enginePath)
        End If

        Dim getEngineObjectPtr As IntPtr = GetProcAddress(dllHandle, "GetEngineObject")
        If getEngineObjectPtr = IntPtr.Zero Then
            Throw New Exception("Can't find GetEngineObject function")
        End If
        Dim deinitializeEnginePtr As IntPtr = GetProcAddress(dllHandle, "DeinitializeEngine")
        If deinitializeEnginePtr = IntPtr.Zero Then
            Throw New Exception("Can't find DeinitializeEngine function")
        End If
        Dim dllCanUnloadNowPtr As IntPtr = GetProcAddress(dllHandle, "DllCanUnloadNow")
        If dllCanUnloadNowPtr = IntPtr.Zero Then
            Throw New Exception("Can't find DllCanUnloadNow function")
        End If

        ' Convert pointers to delegates
        _getEngineObject = DirectCast(Marshal.GetDelegateForFunctionPointer(getEngineObjectPtr, GetType(GetEngineObject)), GetEngineObject)
        _deinitializeEngine = DirectCast(Marshal.GetDelegateForFunctionPointer(deinitializeEnginePtr, GetType(DeinitializeEngine)), DeinitializeEngine)
        _dllCanUnloadNow = DirectCast(Marshal.GetDelegateForFunctionPointer(dllCanUnloadNowPtr, GetType(DllCanUnloadNow)), DllCanUnloadNow)

        ' Call the GetEngineObject function
        Dim hresult As Integer = _getEngineObject(developerSN, Nothing, Nothing, m_engine)

最后一行是导致崩溃的行(没有错误或输出)。相同的代码在VS2010中工作正常,但在VS2015中没有。我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

崩溃的原因是因为您使用非开发人员许可证在Visual Studio中调试ABBYY,不幸的是它在没有提供有意义的异常的情况下崩溃,并且在他们的支持站点上没有关于此的信息。

确保您在调试时使用开发人员许可证,如果通过exe运行,则确保使用运行时许可证。

答案 1 :(得分:0)

在VS2010中查看项目的项目属性,并在VS2015中应用相同的设置,检查包含库路径并确保DLL位于正确的位置