Loading both CLR 2.0 and 4.0 in an ExcelDNA addin

Mar 25, 2016 at 10:46 AM
I am using an ExcelDNA addin with Runtimeversion specified to 2.0 in the .dna file.
<DnaLibrary RuntimeVersion="v2.0">
...
My objective is to call a .Net 4.0 assembly from a .Net 3.5 assembly using COM. The reason I am doing it this way is that I can't upgrade my .Net 3.5 assembly to 4.0 and I do not have a .Net 3.5 version of my 4.0 assembly.

So, from my Addin (.net framework 3.5), I tried to instantiate a COM object (actually an .Net 4.0 COM object) using Activator.CreateInstance and passing it a progId. Then I get the following error message:
System.TypeInitializationException: Retrieving the COM class factory for component with CLSID {G65C127E-7428-5666-B3BF-45F235750FB2} failed due to the following error: 80131534.

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)

   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)

   at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)

   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

   at System.Activator.CreateInstance(Type type)
I understand that ExcelDNA load only the CLR 2.0 when RuntimeVersion="v2.0".

Is it a way to load both CLR 2.0 and 4.0 from the same addin?
Coordinator
Mar 25, 2016 at 12:16 PM
No - your add-in cannot both load CLR 2.0 and CLR 4.0.

Can you explain what happens when you try to run your add-in under the 4.0 runtime? In general assemblies compiled for .NET 3.5 will work perfectly under .NET 4.0.

-Govert
Mar 25, 2016 at 1:04 PM
As my addin is compiled for .net 3.5, I wan't be able to use (and reference from Visual Studio) assemblies built for .net 4.0.
So, If set the RuntimeVersion to v4.0, my addin will work except that I won't be able to reference and use .net 4.0 assemblies.
Coordinator
Mar 25, 2016 at 1:26 PM
I don't understand.
  • What goes wrong if you set RuntimeVersion="v4.0" in your add-in? Even if your add-in is compiled for .NET 3.5, it should run fine under the .NET 4.0 runtime.
  • What are the constraints that prevent you from compiling your add-in to .NET 4.0 ?
Anyway - an Excel-DNA add-in will always load only one version of the runtime.

-Govert
Mar 25, 2016 at 2:45 PM
What goes wrong if you set RuntimeVersion="v4.0" in your add-in? Even if your add-in is compiled for .NET 3.5, it should run fine under the .NET 4.0 runtime.
If my add-in is compiled for .NET 3.5, I won't be able to use my .NET 4.0 assembly from my add-in in a Visual Studio solution. The only possibility I found (except using WCF services) is to use a COM object.
What are the constraints that prevent you from compiling your add-in to .NET 4.0 ?
It could be a possibility to compile my addin to .NET 4.0. I need to do many technical tests as my addin uses several dlls including C++/COM dlls, mixed mode assemblies.
Coordinator
Mar 25, 2016 at 6:26 PM
It's probably good to try to move to .NET 4.

You could also make two separate .xll add-ins. But getting them to talk to each other is likely to be as much work as just moving your stuff to .NET 4.

-Govert
Apr 5, 2016 at 1:38 PM
I have managed to move to .Net 4 and everything works well but I need to do the two followings changes
  • Update the .dna file to include
<DnaLibrary RuntimeVersion="v4.0">
  • As my addin have dependencies with others COM components, I had to update the executable (excel.exe) configuration file by adding the following changes
<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
The option useLegacyV2RuntimeActivationPolicy is for COM components to work properly under the 4.0 runtime.
Is it possible to have the startup options in the .dna file ?
Coordinator
Apr 5, 2016 at 2:02 PM

You can make a <AddIn>/xll.config file that is used as the config file for your add-in, but I don’t think that particular setting works from the AppDomain config file – See this discussion: https://groups.google.com/forum/#!searchin/exceldna/useLegacyV2RuntimeActivationPolicy/exceldna/8bEpoS53-YY/AOJnK0MiUUIJ

I’m not sure if this alternative approach can help you: http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/

-Govert