How to load an assembly only into a specific domain?

  .net-assembly, appdomain, c++, clr

Why CSharp.dll gets loaded into DefaultDomain and how to avoid this?

Console::WriteLine(AppDomain::CurrentDomain->FriendlyName);
auto assemblies = AppDomain::CurrentDomain->GetAssemblies();
for (int i = 0; i < assemblies->Length; ++i) Console::WriteLine(assemblies[i]->GetName());

AppDomain^ domain = AppDomain::CreateDomain(ToMngd(L"MyDomain"));
ObjectHandle^ handle = domain->CreateInstance(ToMngd(L"CSharpDll"), ToMngd(L"CSharpDll.Class1"));

Console::WriteLine(domain->FriendlyName);
assemblies = domain->GetAssemblies();
for (int i = 0; i < assemblies->Length; ++i) Console::WriteLine(assemblies[i]->GetName());

AppDomain::Unload(domain);

Console::WriteLine(AppDomain::CurrentDomain->FriendlyName);
assemblies = AppDomain::CurrentDomain->GetAssemblies();
for (int i = 0; i < assemblies->Length; ++i) Console::WriteLine(assemblies[i]->GetName());

Produces this:

DefaultDomain
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
CppDll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

MyDomain
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
CSharpDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

DefaultDomain
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
CppDll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
CSharpDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Source: Windows Questions C++

LEAVE A COMMENT