C++未安装 .net 4.0 时 CLR 加载 .net 4.0 dll

发布于 2024-10-20 05:00:55 字数 1209 浏览 4 评论 0原文

我有一个应用程序,用 C++ 编写并使用 VS 2010 编译。 我需要从 C++ 代码加载用 C# 编写并为 .net 4.0 平台编译的 .net dll 并调用一些函数。我已经通过 C++ CLR 完成了此操作,并且它的调用 .net 功能很好,直到系统中未安装 .net 4.0 为止。

当未安装 .net 4.0 时,任何函数调用都会在我的 C++ 代码中引发 SEH 异常,但库加载成功。 每次异常代码都是相同的0xE0434F4D。

KERNELBASE.dll!RaiseException()  + 0x3d bytes   
mscorwks.dll!RaiseTheExceptionInternalOnly()  + 0x295 bytes 
mscorwks.dll!UnwindAndContinueRethrowHelperAfterCatch()  + 0x63 bytes   
mscorwks.dll!CorDllMainForThunk()  + 0xcd bytes 
mscoree.dll!CorDllMainWorkerForThunk()  + 0x62 bytes    
mscoree.dll!VTableBootstrapThunkInitHelper()  + 0x12 bytes  
mscoree.dll!VTableBootstrapThunkInitHelperStub()  + 0x3e bytes  
hs.exe!SrvAuth::PluginProxy::ProxyBody()  Line 363 + 0x13 bytes C++
hs.exe!hs::hsThread::InvokableEntry(void * thread_obj=0x000000000024de90)  Line 458 C++
msvcr100.dll!_callthreadstartex()  Line 314 + 0xd bytes C
msvcr100.dll!_threadstartex(void * ptd=0x0000000000000000)  Line 292 + 0x5 bytes    C
kernel32.dll!BaseThreadInitThunk()  + 0xd bytes 
ntdll.dll!RtlUserThreadStart()  + 0x21 bytes    

问题:

  1. 是否可以从C++代码中确定加载的dll的目标.net平台并确定系统中是否安装了该平台?
  2. 也许还有其他方法可以防止 SEH 异常并显示人类可读的错误解释?

提前致谢

I have an application, written on c++ and compiled with VS 2010.
I need to load .net dll written on C# and compiled for .net 4.0 platform from c++ code and call some functions. I have already done this through C++ CLR and its calling .net functions fine until .net 4.0 is not installed in the system.

When .net 4.0 is not installed any function call raises SEH exception in my c++ code, but library loading is successful.
Every time exception code is the same 0xE0434F4D.

KERNELBASE.dll!RaiseException()  + 0x3d bytes   
mscorwks.dll!RaiseTheExceptionInternalOnly()  + 0x295 bytes 
mscorwks.dll!UnwindAndContinueRethrowHelperAfterCatch()  + 0x63 bytes   
mscorwks.dll!CorDllMainForThunk()  + 0xcd bytes 
mscoree.dll!CorDllMainWorkerForThunk()  + 0x62 bytes    
mscoree.dll!VTableBootstrapThunkInitHelper()  + 0x12 bytes  
mscoree.dll!VTableBootstrapThunkInitHelperStub()  + 0x3e bytes  
hs.exe!SrvAuth::PluginProxy::ProxyBody()  Line 363 + 0x13 bytes C++
hs.exe!hs::hsThread::InvokableEntry(void * thread_obj=0x000000000024de90)  Line 458 C++
msvcr100.dll!_callthreadstartex()  Line 314 + 0xd bytes C
msvcr100.dll!_threadstartex(void * ptd=0x0000000000000000)  Line 292 + 0x5 bytes    C
kernel32.dll!BaseThreadInitThunk()  + 0xd bytes 
ntdll.dll!RtlUserThreadStart()  + 0x21 bytes    

Questions:

  1. Is it possible to determine target .net platform of the loaded dll from C++ code and to determine if such platform installed in the system?
  2. Maybe there is any other way prevent SEH exception and show human readable error explanation?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

寒尘 2024-10-27 05:00:55

确定目标程序集的 CLR 运行时版本的一种方法是在通过 .NET 加载器(反射)加载 DLL 文件之前直接读取该文件的标头。您需要检查 IMAGE_COR20_HEADER 标头中的 MajorRuntimeVersion 字段。

请参阅 http://ntcore.com/files/dotnetformat.htmhttp://www.codeproject.com/Articles/65181/From-Binary -to-Data-Structures.aspx 了解更多详细信息。

要确定是否安装了.NET 4.0,您可以检查注册表项是否存在
HKLM\Software\Microsoft\NET Framework Setup\NDP\v4

请参阅如何检测 .NET Framework版本和服务包是否已安装?

One way to determine the CLR runtime version of the target assembly is to read the DLL file's headers directly before loading it via the .NET loader (reflection). You would want to check the MajorRuntimeVersion field from the IMAGE_COR20_HEADER header.

See http://ntcore.com/files/dotnetformat.htm and http://www.codeproject.com/Articles/65181/From-Binary-to-Data-Structures.aspx for more details.

To determine if .NET 4.0 is installed, you can check for the presence of the registry key
HKLM\Software\Microsoft\NET Framework Setup\NDP\v4.

See How to detect what .NET Framework versions and service packs are installed?.

奶气 2024-10-27 05:00:55

0xE0434F4D 是托管异常的异常代码。当您获得该代码时,已经为时已晚。异常未处理,没有剩余的实时代码来解释异常。在 CLR 启动的早期阶段改进错误处理需要您自己托管 CLR。 Google CorBindToRuntimeEx 以查找所需的样板代码。

编写一个更好的安装程序来确保 .NET 4.0 在计算机上正确部署可能是一个更有效的攻击计划。使用安装项目非常简单。

0xE0434F4D is the exception code for a managed exception. By the time you get that code it is already too late. The exception was unhandled, there's no live code left to interpret the exception. Improving error handling at such an early stage of the CLR booting up requires hosting the CLR yourself. Google CorBindToRuntimeEx to find the boilerplate code that's required.

Writing a better installer that ensures that .NET 4.0 is properly deployed on the machine might be a more fruitful plan of attack. It is really simple with a Setup project.

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