VS2015 应用程序在调用具有委托C++函数时崩溃

VS2015 application crashes when calling C++ function with delegates

本文关键字:C++ 函数 崩溃 应用程序 调用 VS2015      更新时间:2023-10-16

我正在使用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中则不然。我在这里错过了什么?

它崩溃的原因是因为你使用非开发人员许可证在Visual Studio中调试ABBYY,不幸的是,它崩溃了,没有提供有意义的异常,他们的支持网站上没有关于这方面的信息。

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

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