private static AppDomain GetAppDomain(string friendlyName)
        {
            IntPtr enumHandle = IntPtr.Zero;
            var    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;
                    if (appDomain.FriendlyName.Equals(friendlyName))
                    {
                        return(appDomain);
                    }
                }
            }
            finally
            {
                host.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(host);
                host = null;
            }
            return(null);
        }
Beispiel #2
0
        public static IList <AppDomain> GetAppDomains()
        {
            IList <AppDomain> _IList     = new List <AppDomain>();
            IntPtr            enumHandle = IntPtr.Zero;

            //COM Reference to the mscoree.tlb is required for this
            //mscoree.tlb - Common Language Runtime Execution Engine 2.0 Library
            mscoree.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);
            }
        }