2

I have unmanaged dll written in C, that will be injected into managed application (dotNet 4). I am going to enumerate _AppDomains, running in this app to load some module into domain. It is available to do this using ICorRuntimeHost interface. However, ICorRuntimeHost is deprecated, and (as described there http://msdn.microsoft.com/en-en/library/ms164320%28v=vs.100%29.aspx) replaced with ICLRRuntimeHost.

How can I perform _AppDomain enumeration using ICLRRuntimeHost? Is it possible?

Viacheslav Kovalev
  • 1,745
  • 12
  • 17

2 Answers2

3

Amongst the debugging interfaces there is ICorPublish.

You can use the ICorPublish::GetProcess() method to retrieve a ICorPublishProcess instance for a .NET process (identified by passing the respective process ID, which can be the ID of the current process, of course).

That interface provides the method ICorPublishProcess::EnumAppDomains(), which you can use the get a list of the current application domains in the target process, via an enumerator to ICorPublishAppDomain instances. Each of which has the ICorPublishAppDomain.GetName() method that gets you the name of the application domain.

Update: I have not tried this, but how about:

  • Using the approach above to enumerate the app domains (including getting their unique ID via ICorPublishAppDomain.GetID()).

  • The use ICLRRuntimeHost::ExecuteInAppDomain to actually execute code in that domain (including loading the module you need to). The (first) parameter AppDomainId would be the value you get from ICorPublishAppDomain.GetID().

There seems to be an, at least related, example (CppHostCLR) about that in the Microsoft All-In-One Code Framework over on codeplex.com

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • Thanks, Christian. I'll keep on mind this interface, but it is not exactly what I need. I want to protect (encrypt) some netmodules and load it from unmanaged dll. So I need not only appdomains enumeration, but ability to load some code into specified domains. – Viacheslav Kovalev Mar 18 '13 at 16:49
2

I would use the recommended approach: you register your own AppDomainManager with IClrControl::SetAppDomainManagerType, make it register with the host (with AppDomainManagerInitializationOptions.RegisterWithHost), receive notifications of AppDomain creation and "listen" for domain unload through IActionOnCLREvent::OnEvent(Event_DomainUnload).

That way, you already have "some code" (the domain manager) loaded into each of the domains (including the default appdomain).

Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
  • 1
    Thanks a lot, Lorenzo. I even forgot what I've tried to accomplish by this, but maybe this will be usefull for someone else. – Viacheslav Kovalev Dec 27 '14 at 16:54
  • That was the goal :) I am working a lot with AppDomains lately, and when I come across an AppDomain question.. I just answer it. It may be useful for someone, in the future – Lorenzo Dematté Dec 27 '14 at 19:31