本机 C Dll 调用 C++/CLI 混合模式 Dll - 未处理的异常

发布于 2024-08-09 17:37:31 字数 912 浏览 7 评论 0原文

我有一个由旧应用程序动态加载的本机 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荆棘i 2024-08-16 17:37:31

我曾经遇到过这个问题。我针对这个问题提供了一些附加信息和解决方法。但我也很高兴在这里总结并提供答案。

您遇到的基本问题是,在将本机应用程序链接到混合模式 dll 和非托管 dll 时,链接器的工作方式似乎存在错误或存在一些变化。看来只有当混合模式 dll 和非托管 dll 都由非托管可执行文件链接时才会发生这种情况。

  • 选项1:将混合模式dll设置为延迟加载。
  • 选项 2:通过让非托管 exe 中至少有一个 CLR 感知的文件,使非托管 exe 成为混合模式可执行文件。该文件可以为空,只需存在它即可允许在可执行文件中正确链接。
  • 选项 3:更新非托管可执行文件,以便它在链接任何非托管库之前链接混合模式库。

我最终使用选项 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.

  • Option 1: Set the mixed mode dll to be Delay Loaded.
  • Option 2: Make the unmanaged exe a mixed mode executable, by having at least one file in it CLR aware. The file can be empty, it just has to be present to allow things to link correctly in the executable.
  • Option 3: Update the unmanaged executable so that it links the mixed mode library before linking any unmanaged libraries.

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!

哭泣的笑容 2024-08-16 17:37:31

在我看来,符号设置不正确。您可能需要首先执行以下操作:

  1. 安装 Windows 调试工具
    http://www.microsoft.com/whdc/devtools/debugging/default.mspx
  2. 打开命令提示符,切换到该文件夹​​,运行 WinDBG -I
  3. 运行你的程序,让它崩溃,WinDBG 将被触发
  4. type .symfix
  5. type .sympath+
  6. type .loadby sos clr
  7. type !CLRStack -a
  8. type kb

步骤 7 和8 应该会给你一个更有意义的堆栈来调试

Seems to me the symbol is not setup correctly. You might want to do the following first:

  1. install Debugging Tools for Windows
    http://www.microsoft.com/whdc/devtools/debugging/default.mspx
  2. open command prompt, change to the folder, run WinDBG -I
  3. run your program, let it crash, WinDBG will be triggered
  4. type .symfix
  5. type .sympath+
  6. type .loadby sos clr
  7. type !CLRStack -a
  8. type kb

Step 7 and 8 should get you a much more meaningful stack to debug with

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文