Failed to load C module in new appdomain

3

I have some .NET code that need to run in a separate AppDomain, and everything goes well except when there is call of COM component.
To make it clear, I wrote a very simple repro as below:

AppDomain ap = AppDomain.CreateDomain("newap");
ap.DoCallBack(
    () =>
    {
        //GroupPolicyObject is from assembly microsoft.grouppolicy.management.interop.dll
        GroupPolicyObject newGPO = new GroupPolicyObject();
    });

The exception:

System.TypeInitializationException: The type initializer for '' threw an exception. ---> .ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain. ---> System.Runtime.InteropServices.COMException: Invalid operation. (Exception from HRESULT: 0x80131022) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at .GetDefaultDomain() at .DoCallBackInDefaultDomain(IntPtr function, Void* cookie) at .LanguageSupport._Initialize(LanguageSupport* ) at .LanguageSupport.Initialize(LanguageSupport* ) --- End of inner exception stack trace --- at .ThrowModuleLoadException(String errorMessage, Exception innerException) at .LanguageSupport.Initialize(LanguageSupport* ) at .cctor()

The code can pass in default domain, and should not be the issue related with the version of .NET framework.

I did a lot of searching in the internet, but there’s no useful solution. Does anyone have ideas what’s the issue and how to achieve this?

c#
com
appdomain
asked on Stack Overflow Nov 21, 2013 by yesilver • edited Nov 21, 2013 by Pranav 웃

1 Answer

4

At last I fixed it by adding

  <startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>

in the cofig file.

The link is quite useful.

answered on Stack Overflow Nov 22, 2013 by yesilver • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0