Welcome to MSDN Blogs Sign in | Join | Help

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

            }

        }

    }

Published Monday, June 11, 2007 11:20 AM by JackG

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Enumerating AppDomains

Tuesday, July 24, 2007 6:35 PM by Garr Lystad

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.

# re: Enumerating AppDomains

Tuesday, July 24, 2007 8:16 PM by JackG

Garr, 3.0 is an addition to 2.0 so you should have something like C:\Windows\Microsoft.NET\Framework\v2.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

# re: Enumerating AppDomains

Friday, July 27, 2007 2:18 AM by Cucosel Bogdan

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

Thanks

# re: Enumerating AppDomains

Monday, July 30, 2007 3:01 PM by JackG

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

# re: Enumerating AppDomains

Saturday, November 15, 2008 9:22 AM by Roy

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

Thanks!

# re: Enumerating AppDomains

Thursday, November 20, 2008 1:27 PM by JackG

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

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker