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();



                host.EnumDomains(out enumHandle);

                object domain = null;

                while (true)



                    host.NextDomain(enumHandle, out domain);

                    if (domain == null) break;

                    AppDomain appDomain = (AppDomain)domain;



                return _IList;


            catch (Exception e)



                return null;









Comments (14)

  1. Garr Lystad says:

    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.


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

  3. Cucosel Bogdan says:

    Can you please tell me if there’s any way of determining the AppDomains of another process ?


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

  5. Roy says:

    Jack, where are the samples you posted that could be used to load code in the target process?


  6. Roy, I don’t understand which post you are refering too. Could you be more specific? All source should be available. – JackG

  7. Doug says:

    Is this threadsafe? What happens if another thread is doing CreatDomain in parallel?

  8. chandar says:


    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?

  9. TusharG says:


    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.



  10. MaratSh says:


    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…

  11. Boinst says:

    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!

  12. DSS2000 says:


    "Interop type 'mscoree.CorRuntimeHostClass' cannot be embedded. Use the applicable interface instead."

    the line:

               CorRuntimeHostClass host = new mscoree.CorRuntimeHostClass();

    should be:

               ICorRuntimeHost host = new mscoree.CorRuntimeHost();

  13. Jason says:

    In order to fix the 'mscoree.CorRuntimeHostClass' cannot be embedded. Use the applicable interface instead. error with VS2010/Net.4 you need to change the property on the mscoree reference to say

    Embed Interop Types = False

    link: mokosh.co.uk/…/net-4-0-interop-type-cannot-be-embedded-use-the-applicable-interface-instead

  14. Tatyana says:

    Answer to MaratSh question.

    You see the error because you use .net 4.0. Change the line with error to

    ICorRuntimeHost host = new CorRuntimeHost();