Exception occured While loading dynamically EXE assembly in C++/CLI (Could not load file or assembly ', Version=1.0.3836.39802 …)

别等时光非礼了梦想. 提交于 2019-12-06 10:43:34

"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).

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!