Enumerating AppDomains

Recently I was writing a sample app showing some new MAF (Managed Add-In Framework) features that will be released very soon. Stay tuned.

As I was showing isolation and unload-ability, I wanted to enumerate the AppDomain's in the current process. Surprisingly there is no managed API in the BCL to show AppDomains L Using Interop however, we can list the AppDomains.

using System.Runtime.InteropServices; // for domain enum

using mscoree; // for domain enum. Add the following as a COM reference - C:\WINDOWS\Microsoft.NET\Framework\vXXXXXX\mscoree.tlb

namespace MyNS

{

public class ListProcessAppDomains

{

public static IList<AppDomain> GetAppDomains()

{

IList<AppDomain> _IList = new List<AppDomain>();

IntPtr enumHandle = IntPtr.Zero;

CorRuntimeHostClass host = new mscoree.CorRuntimeHostClass();

try

{

host.EnumDomains(out enumHandle);

object domain = null;

while (true)

{

host.NextDomain(enumHandle, out domain);

if (domain == null) break;

AppDomain appDomain = (AppDomain)domain;

_IList.Add(appDomain);

}

return _IList;

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

return null;

}

finally

{

host.CloseEnum(enumHandle);

Marshal.ReleaseComObject(host);

}

}

}

Comments

  • Anonymous
    July 24, 2007
    I don't see mscoree in .NET 3.0.  How do I find all the AppDomains of a Process now?   I'd be happy just to be able to find the AppDomain of a given .NET window, but in the case of a thread that starts windows in different AppDomains, I can only find the last AppDomain where a window was created by the thread.  That's no help if the window I'm given was created somewhere else. Thanks.

  • Anonymous
    July 24, 2007
    Garr, 3.0 is an addition to 2.0 so you should have something like C:WindowsMicrosoft.NETFrameworkv2.0.50727 on your machine (Vista won't show 2.0 in ARP since it is part of the O/S).  Check out mscoree in this location. - JackG

  • Anonymous
    July 26, 2007
    Can you please tell me if there's any way of determining the AppDomains of another process ? Thanks

  • Anonymous
    July 30, 2007
    Cucosel, There don't seem to be any remote activation methods.  You could use the samples I posted on remoting or COM to load a bit of code in the target process in order to enumerate the AD's. - JackG

  • Anonymous
    November 15, 2008
    Jack, where are the samples you posted that could be used to load code in the target process? Thanks!

  • Anonymous
    November 20, 2008
    Roy, I don't understand which post you are refering too. Could you be more specific? All source should be available. - JackG

  • Anonymous
    September 08, 2009
    Is this threadsafe? What happens if another thread is doing CreatDomain in parallel?

  • Anonymous
    November 24, 2009
    Jack, In windows 7 I am getting this error "Interop type 'mscoree.CorRuntimeHostClass' cannot be embedded. Use the applicable interface instead." Can you suggest any workaround?

  • Anonymous
    January 11, 2010
    Hi, Is there any way to get a list of AppDomains in another manged Process running on the machine. I just get some information, that we can do this using Debugging API (Managed Wrapper). Can you please comment on this. It will be gr8, if you provide some example. Thanks, TusharG

  • Anonymous
    January 13, 2010
    Jack, good programming technic - using appropriate interface ICorRuntimeHost instead of using public class of uncertain dll... But, in the .NET Framework version 2.0, interface ICorRuntimeHost is superceded by ICLRRuntimeHost! You cannot enumerate application domains in any right way...

  • Anonymous
    March 25, 2010
    Brilliant! Works a charm! I was hesitant to use a COM object because I don't understand how they work, but your code worked flawlessly so I cannot complain. Thankyou!

  • Anonymous
    October 11, 2010
    The comment has been removed

  • Anonymous
    October 19, 2010
    The comment has been removed

  • Anonymous
    February 11, 2011
    The comment has been removed