Exception occured While loading dynamically EXE assembly in C++/CLI (Could not load file or assembly ', Version=1.0.3836.39802 ...)
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 exce开发者_JAVA技巧ption 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.
"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.
精彩评论