Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to load C module in new appdomain

Tags:

c#

com

appdomain

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?

like image 756
yesilver Avatar asked Nov 24 '25 13:11

yesilver


1 Answers

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.

like image 61
yesilver Avatar answered Nov 26 '25 02:11

yesilver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!