在 C++/CLI 中动态加载 EXE 程序集时发生异常(无法加载文件或程序集,版本=1.0.3836.39802 ...)

发布于 2024-09-08 06:30:42 字数 506 浏览 9 评论 0原文

我在动态加载程序集时遇到 C++/CLI 异常,该程序集本身使用 Assembly.Load 在 C++/CLI 托管模式下创建 EXE。它成功加载 DLL 程序集,但无法加载 EXE 程序集并生成以下异常:

TestManager.dll 中发生类型为“System.IO.FileLoadException”的未处理异常

无法加载文件或程序集“testAssembly,Version=1.0.3836.39802,Culture=neutral,PublicKeyToken=null”或其依赖项之一。尝试使用修复程序加载无法验证的可执行文件(具有超过 2 个部分或 TLS 部分的 IAT。)

HRESULT 异常:0x80131019

TestManager.dll 本身是一个托管 dll,并加载到 CLI 中的另一个 CLR 进程中,并尝试将 EXE 程序集加载为一个单独的进程,但失败并生成异常。

这可能是由于使用混合模式造成的。

I am facing an exception in C++/CLI while dynamically loading assembly which itself creates an EXE in C++/CLI managed mode using Assembly.Load. It successfully loads a DLL assembly, but fails to load EXE assembly and generates the following exception:

An unhandled exception of type 'System.IO.FileLoadException' occurred in TestManager.dll

Could not load file or assembly 'testAssembly, Version=1.0.3836.39802, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Attempt to load an unverifiable executable with fixups` (IAT with more than 2 sections or a TLS section.)

Exception from HRESULT: 0x80131019

TestManager.dll itself is a managed dll and loaded into another CLR process in CLI and tries to load EXE assembly as a seperate process, but fails and generates an exception.

This could probably be due to playing with mixed modes.

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

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

发布评论

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

评论(3

命比纸薄 2024-09-15 06:30:42

“当作为引用的程序集加载时,混合模式 C++ EXE 无法在内存中正确重定位。这就是运行时失败的原因。”

引用来自微软对此错误的回应 在 Connect 上,他们解释说他们不会修复它(对于罕见的情况来说太麻烦了)。

"A mixed mode C++ EXE cannot be relocated in memory properly when loaded as a referenced assembly. This is why there is a runtime failure."

The quote is from Microsoft's response to this bug on Connect, where they explain that they're not going to fix it (too much trouble for a rare situation).

浅忆流年 2024-09-15 06:30:42

TL;DR:通过将 CLR 支持从 /clr 更改为 /clr:pure,将程序集类型从混合更改为仅托管。

详细信息:
我今天遇到了非常相似的情况:
我有各种托管 DLL,全部用 /clr 编译,因为其中一些导入本机 DLL。
我有一个 EXE,也是用 /clr 编译的。
它们全部都是用 C++/CLI 编写的。

到目前为止,所有用户控件都位于 DLL 中。今天,我在 EXE 的程序集中创建了一个 UC,并希望将此 UC 插入到 EXE 的主窗体中。它失败了,只是说

无法加载工具箱项目。它将从工具箱中删除。

没有别的了。

所以我创建了一个新的winforms项目,添加了对EXE的引用(有效),并尝试在Visual Studio设计器工具箱中添加EXE的控件。上次操作失败,错误消息为

尝试加载带有修复程序的无法验证的可执行文件(具有超过 2 个部分或 TLS 部分的 IAT。)

随着第二条失败消息,我发现了这个 Stackoverflow 帖子,其中@Stephen 明显在引号上方

“混合模式 C++ EXE 在以下情况下无法在内存中正确重定位:
作为引用的程序集加载。这就是为什么有一个运行时
失败。”

。这意味着如果消息正确,我正在编译为混合模式程序集 EXE。因此我查找了可以更改我创建的程序集类型的位置,并找到了 MSDN 上的混合(本机和托管)程序集
它链接到一些带有详细描述的页面,其中之一是
纯净且可验证的代码 (C++/CLI)在那里我发现我必须使用/clr:pure

在更改我的 EXE 程序集(不是 DLL,它们保持混合状态)的此设置后,我能够将其添加到测试项目的 VS 设计器工具箱中,并将 UC 插入到 EXE 的主窗体中。

TL;DR: change your assembly type from mixed to managed-only, by changing the CLR support from /clr to /clr:pure.

Details:
I had a very similar situation today:
I have various managed DLLs, all compiled with /clr because some of them import native DLLs.
I have an EXE, also compiled with /clr.
All of them are written in C++/CLI.

Up to now, all user-controls were in the DLLs. Today I have created an UC in the assembly of the EXE and wanted to insert this UC in the EXE's main form. It failed and just said

Failed to load toolbox item. It will be removed from the toolbox.

Nothing else.

So I created a new winforms project, added the reference to the EXE (worked), and tried to add the controls of the EXE in the Visual Studio Designer Toolbox. The last action failed, error message was

Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.)

With the 2nd failure message I found this Stackoverflow post, where @Stephen Clearly above quotes

"A mixed mode C++ EXE cannot be relocated in memory properly when
loaded as a referenced assembly. This is why there is a runtime
failure."

from MSDN. This means that I was compiling to a mixed-mode assembly EXE if the message was right. So I looked up where I can change the type of assembly I create and found Mixed (Native and Managed) Assemblies at MSDN,
which links to some pages with detailed descriptions, one of which is
Pure and Verifiable Code (C++/CLI). There I saw that I had to use /clr:pure.

After changing this setting for my EXE assembly (not the DLLs, they remain mixed), I was able to add it to the VS Designer Toolbox of the test project and also insert the UC into the main form of the EXE.

很糊涂小朋友 2024-09-15 06:30:42

我认为您需要在.NET 中使用命名管道进行进程间通信。 Assembly.Load 不适用于 EXE 程序集。

I think you need to use named pipes for inter process communication in .NET. Assembly.Load will not work for EXE assemblies.

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