I have an unmanaged application (MSACCESS.EXE), which dynamically loads various .NET assemblies (via COM interop).
Currently, the unmanaged application automatically initializes the .NET 2.0 CLR when the first assembly is loaded. Since I want to migrate (some of) the .NET assemblies to .NET Framework 4.x, I want the unmanaged application to load the .NET 4.0 CLR instead.
It is well-known that this can be done by providing the following *.exe.config file to the unmanaged application:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
</startup>
</configuration>
This works. However, it requires creating modifying the user's MS Office installation (by creating a file in the same folder as msaccess.exe), which I'd like to avoid, because (a) it requires administrative permissions and (b) it potentially affects other, unrelated Access-based applications as well.
Thus, I'd like to set this supportedRuntime
configuration at run-time, using VBA and/or (more likely) Windows API calls.
How do I modify an unmanaged application's "CLR preference" at runtime?
Alternative solutions that I have tried and ruled out:
- Dynamically load the correct CLR via
CorBindToRuntimeEx
: This would solve my problem, but I can't get it to work with late binding. That is why I currently investigate alternative approaches.