本机 C Dll 调用 C++/CLI 混合模式 Dll - 未处理的异常
我有一个由旧应用程序动态加载的本机 C Dll。该 dll 的目的是允许基于某些应用程序事件覆盖应用程序行为。我有一个 C# dll,其中包含我通过混合模式 C++/CLI dll 从本机 C dll 调用的函数,以增强这些应用程序事件。使用此架构的应用程序可以在 Windows 2000 上完美运行。
该应用程序也可以在 Windows XP 上运行,但不幸的是,一旦在应用程序启动时加载 Native C dll,应用程序就会崩溃(未处理的异常)。当它尝试加载混合模式 dll 时,它似乎崩溃了。我已从 Native C dll 中删除了对混合模式 dll 的所有依赖项,并且应用程序可以正常启动。但是一旦添加了依赖,就会发生崩溃。 Windows 2000 上的代码与 Windows XP 上使用的代码相同。我无权访问应用程序代码,但可以访问 Native C dll 代码,但无法让调试器停止,因为崩溃发生在初始化完成之前。我怀疑这与 CLR 初始化和操作系统加载程序差异有关,但不确定。我正在寻找有关如何解决此问题的建议。我使用的是 VS2005,使用 2.0 框架。我很感激你能提供的任何帮助。
异常和堆栈跟踪并没有真正的帮助:
MyApplication.exe 中 0x775125f6 处未处理的异常:0xC0000005:访问冲突读取位置 0x775125f6。
775125f6()
user32.dll!7e418734()
[Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]
user32.dll!7e418816()
user32.dll!7e428ea0()
user32.dll!7e42ce7c()
ntdll.dll!7c90e473()
user32.dll!7e42e389()
...
I have a Native C Dll that is dynamically loaded by a legacy application. The intent of this dll is to allow overriding of application behavior based on certain application events. I have a C# dll that contains functions that I call from the Native C dll through the mixed mode C++/CLI dll to enhance these application events. The application using this architecture works flawlessly on Windows 2000.
The application also runs on Windows XP but unfortunately the application crashes(Unhandled Exception) once the Native C dll is loaded at application startup. It appears that it crashes when it is attempting to load the mixed mode dll. I have removed all dependencies to the mixed mode dll from the Native C dll and the application boots up fine. But once the dependency is added, the crash occurs. The code on Windows 2000 is the same as what is used on Windows XP. I don't have access to the application code but do have access to the Native C dll code but can't get the debugger to stop as the crash occurs before intialization has completed. I suspect it is something to do with CLR initialization and OS loader differences but am not certain. I'm looking for suggestions on how to go about resolving this. I am using VS2005 using the 2.0 Framework. I appreciate any help you could give.
The exception and stack trace is not real helpful:
Unhandled exception at 0x775125f6 in MyApplication.exe: 0xC0000005: Access violation reading location 0x775125f6.
775125f6()
user32.dll!7e418734()
[Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]
user32.dll!7e418816()
user32.dll!7e428ea0()
user32.dll!7e42ce7c()
ntdll.dll!7c90e473()
user32.dll!7e42e389()
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我曾经遇到过这个问题。我针对这个问题提供了一些附加信息和解决方法。但我也很高兴在这里总结并提供答案。
您遇到的基本问题是,在将本机应用程序链接到混合模式 dll 和非托管 dll 时,链接器的工作方式似乎存在错误或存在一些变化。看来只有当混合模式 dll 和非托管 dll 都由非托管可执行文件链接时才会发生这种情况。
我最终使用选项 3 来解决这个问题,因为它看起来是最安全的。只需转到“项目属性”对话框,选择“链接器”->“输入”选项卡。在“附加依赖项”字段中指定混合模式 dll 的 lib 文件。确保混合模式库是该字段中列出的第一个库。
设置附加依赖项字段,如下所示:
混合模式.lib;非托管.lib;非托管2.lib。
这似乎解决了非托管应用程序崩溃的问题。如果您重新排列库的顺序,使 unmanaged.lib 成为第一个,您最终会再次崩溃。
我希望这有帮助!
I've had this exact problem. I provided some additional information and workarounds on this question. But I'm happy to summarize and provide an answer here as well.
The basic problem you're encountering is there seems to be a bug or some variability in how the linker does it's job when linking native applications to mixed mode dlls and unmanaged dlls. It appears that this only happens when you have both mixed mode dlls and unmanaged dlls being linked by an unmanaged executable.
I've ended up using Option 3 to solve this problem, because it seemed the safest. Just go to the Project Properties dialog, select Linker->Input tab. Specify the lib file for the mixed mode dll in the Additional Dependencies field. Ensure that the mixed mode lib is the first lib listed in that field.
Set the Additional Dependencies field Like this:
mixedmode.lib;unmanaged.lib;unmanaged2.lib.
This seems to resolve the issue with the unmanaged application crashing. If you rearrange the order of the lib's so an unmanaged.lib is first you'll end up back at the crash.
I hope this helps!
在我看来,符号设置不正确。您可能需要首先执行以下操作:
http://www.microsoft.com/whdc/devtools/debugging/default.mspx
步骤 7 和8 应该会给你一个更有意义的堆栈来调试
Seems to me the symbol is not setup correctly. You might want to do the following first:
http://www.microsoft.com/whdc/devtools/debugging/default.mspx
Step 7 and 8 should get you a much more meaningful stack to debug with