Changing .NET 4 Runtime Activation Policy For Dll Within C++ Code
We've developed a C# 4.0 module that needs to be accessed from an unmanaged C++ program (as well as a Fortran program). The DLL gets loaded correctly and the method is available to the C++ program, but an exception gets thrown when calling the method that points to the C# code. For a visualization:
[Unmanaged C++ / Fortran Projects]
-> controller.dll (unmanaged C++ within the C++ project)
-> managed_wrapper.dll (managed C++ within the C# project)
-> C# 4.0 Project
The exception gets thrown when the controller attempts to call the managed C++ (using .NET 4.0 framework).
My suspicions are that the开发者_如何学JAVA following configuration needs to be applied, as we encountered an error previously when creating a proof of concept:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
I applied it correctly for the aforementioned proof of concept, but that was on a stand-alone console application. Since the unmanaged C++ program is huge and the settings are not universal for every module, I'm unable to create a MyApplication.exe.config
file with the above configuration and call it day.
My question is this: Is it possible to apply that configuration to a specific DLL?
From my understanding, this is not possible.
The issue is that, the configuration setting you need to apply (useLegacyV2RuntimeActivationPolicy
) is basically saying "for this entire application, no matter what the assembly requests, use CLR 4 instead.
If you could apply it to a single DLL, you'd be violating the goals here - since the goal is to force all assemblies to use CLR 4.
Edit:
After further study, there is actually a way to do this in some limited scenarios. I posted about this on my blog, but basically, you can use the CLR Hosting API to get some access to setting this from within a library.
精彩评论