使用 C++/CLR dll 时 C# 应用崩溃

C# app crashes when using C++/CLR dll

本文关键字:应用 崩溃 dll C++ CLR 使用      更新时间:2023-10-16

我有 3 个应用程序,一个是输出静态库的本机C++应用程序,一个是此静态库的 C++/CLR 包装器,最后是使用 C++/CLR 包装器的 C# Windows 窗体应用程序。

当项目为空时,一切都可以编译并运行良好,但是当我在本机C++应用程序中向我的类添加一些代码时,C# winforms应用程序中出现崩溃。

这是错误消息:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in      
WindowsFormsApplication1.exe
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in  
WindowsFormsApplication1.exe
Additional information: Could not load file or assembly 'stenCLR.dll' or one of its 
dependencies. The specified module could not be found.

我确定stenCLR.dll在正确的文件夹中。

这是来自视觉工作室的完整错误日志:

'WindowsFormsApplication1.exe': Loaded 'E:ST Projectssten_svntrunkWindowsFormsApplication1binDebugWindowsFormsApplication1.exe', No native symbols in symbol file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64ntdll.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64mscoree.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64kernel32.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64KernelBase.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64advapi32.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64msvcrt.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64sechost.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64rpcrt4.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64sspicli.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64cryptbase.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsMicrosoft.NETFrameworkv4.0.30319mscoreei.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64shlwapi.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64gdi32.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64user32.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64lpk.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64usp10.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64imm32.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64msctf.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsMicrosoft.NETFrameworkv4.0.30319clr.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64msvcr110_clr0400.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsassemblyNativeImages_v4.0.30319_32mscorlibdb19194dcf61188e0e1bb9b6b2dba362mscorlib.ni.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe' (Managed (v4.0.30319)): Loaded 'C:WindowsMicrosoft.NetassemblyGAC_32mscorlibv4.0_4.0.0.0__b77a5c561934e089mscorlib.dll'
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64ole32.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64uxtheme.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:Program Files (x86)Common Filesmicrosoft sharedinktiptsf.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64oleaut32.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64version.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe' (Managed (v4.0.30319)): Loaded 'E:ST Projectssten_svntrunkWindowsFormsApplication1binDebugWindowsFormsApplication1.exe', Symbols loaded.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsMicrosoft.NETFrameworkv4.0.30319clrjit.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsassemblyNativeImages_v4.0.30319_32System529187426262f016595cb4b92bc83241System.ni.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsassemblyNativeImages_v4.0.30319_32System.Drawing6007a55278ac4372ab91f08ead86756eSystem.Drawing.ni.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsassemblyNativeImages_v4.0.30319_32System.Windows.Forms1242e9bc90b42762b6c7f2a5052a2280System.Windows.Forms.ni.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe' (Managed (v4.0.30319)): Loaded 'C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Windows.Formsv4.0_4.0.0.0__b77a5c561934e089System.Windows.Forms.dll'
'WindowsFormsApplication1.exe' (Managed (v4.0.30319)): Loaded 'C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Drawingv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Drawing.dll'
'WindowsFormsApplication1.exe' (Managed (v4.0.30319)): Loaded 'C:WindowsMicrosoft.NetassemblyGAC_MSILSystemv4.0_4.0.0.0__b77a5c561934e089System.dll'
'WindowsFormsApplication1.exe': Loaded 'ImageAtBase0x50d00000', Loading disabled by Include/Exclude setting.
'WindowsFormsApplication1.exe': Unloaded 'ImageAtBase0x50d00000'
'WindowsFormsApplication1.exe': Loaded 'ImageAtBase0x519b0000', Loading disabled by Include/Exclude setting.
'WindowsFormsApplication1.exe': Loaded 'ImageAtBase0x7f0000', Loading disabled by Include/Exclude setting.
'WindowsFormsApplication1.exe': Unloaded 'ImageAtBase0x519b0000'
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64cryptsp.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64rsaenh.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64RpcRtRemote.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'E:ST Projectssten_svntrunkWindowsFormsApplication1binDebugstenCLR.dll', Symbols loaded.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64msvcr110d.dll', Symbols loaded.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64msvcp110d.dll', Symbols loaded.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64d3d11.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64dxgi.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Loaded 'C:WindowsSysWOW64dwmapi.dll', Cannot find or open the PDB file.
'WindowsFormsApplication1.exe': Unloaded 'E:ST Projectssten_svntrunkWindowsFormsApplication1binDebugstenCLR.dll'
'WindowsFormsApplication1.exe': Unloaded 'C:WindowsSysWOW64d3d11.dll'
'WindowsFormsApplication1.exe': Unloaded 'C:WindowsSysWOW64dxgi.dll'
'WindowsFormsApplication1.exe': Unloaded 'C:WindowsSysWOW64dwmapi.dll'
'WindowsFormsApplication1.exe': Unloaded 'C:WindowsSysWOW64msvcp110d.dll'
'WindowsFormsApplication1.exe': Unloaded 'C:WindowsSysWOW64msvcr110d.dll'
First-chance exception at 0x753eb9bc in WindowsFormsApplication1.exe: Microsoft C++ exception: EEFileLoadException at memory location 0x003bcce4.
First-chance exception at 0x753eb9bc in WindowsFormsApplication1.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x753eb9bc in WindowsFormsApplication1.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x753eb9bc in WindowsFormsApplication1.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x753eb9bc in WindowsFormsApplication1.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x753eb9bc in WindowsFormsApplication1.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
A first chance exception of type 'System.IO.FileNotFoundException' occurred in WindowsFormsApplication1.exe
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in WindowsFormsApplication1.exe
Additional information: Could not load file or assembly 'stenCLR.dll' or one of its dependencies. The specified module could not be found.
The program '[2000] WindowsFormsApplication1.exe: Native' has exited with code 0 (0x0).
The program '[2000] WindowsFormsApplication1.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

我建议看看Dependency Walker和Fusion Log Viewer。

依赖关系Walker会告诉你是否有任何DLL丢失你尝试加载的DLL。

Fusion Log Viewer 将告诉您尝试从何处加载 DLL。

这些通常是调试库加载问题的最简单方法,因为它们使问题非常明显:)

一种可能性是你有一台 64 位机器,编译了 C# 应用程序为"任何 CPU"或 64 位,并且 c++ 本机库或 C++/CLI 是 32 位。在这种情况下,系统查找 64 位库。

确保所有应用程序都是针对相同的体系结构(x86 或 x64)构建的。