Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top