public static IEnumerable <AppDomain> EnumAppDomains()
        {
            IntPtr          enumHandle = IntPtr.Zero;
            ICorRuntimeHost host       = null;

            try
            {
                host = GetCorRuntimeHost();
                host.EnumDomains(out enumHandle);
                object domain = null;

                host.NextDomain(enumHandle, out domain);
                while (domain != null)
                {
                    yield return((AppDomain)domain);

                    host.NextDomain(enumHandle, out domain);
                }
            }
            finally
            {
                if (host != null)
                {
                    if (enumHandle != IntPtr.Zero)
                    {
                        host.CloseEnum(enumHandle);
                    }

                    Marshal.ReleaseComObject(host);
                }
            }
        }
예제 #2
0
    public static List <AppDomain> GetAllAppDomains()
    {
        List <AppDomain> appDomains = new List <AppDomain>();

        IntPtr          handle = IntPtr.Zero;
        ICorRuntimeHost host   = (ICorRuntimeHost)(new CorRuntimeHost());

        try
        {
            host.EnumDomains(out handle);
            while (true)
            {
                object domain;
                host.NextDomain(handle, out domain);
                if (domain == null)
                {
                    break;
                }
                appDomains.Add((AppDomain)domain);
            }
        }
        finally
        {
            host.CloseEnum(handle);
        }

        return(appDomains);
    }
예제 #3
0
        public static System.AppDomain GetDefaultAppDomain()
        {
            ICorRuntimeHost corHost = (ICorRuntimeHost) new CorRuntimeHost();

            object appDomain;

            corHost.GetDefaultDomain(out appDomain);

            return((System.AppDomain)appDomain);
        }
예제 #4
0
        public static System.AppDomain GetAppDomain(string friendlyName)
        {
            const string method = "GetAppDomain";

            if (friendlyName == null)
            {
                throw new NullParameterException(typeof(ClrUtil), method, "friendlyName");
            }

            System.AppDomain appDomain = null;

            // Enumerate all AppDomains inside the process using the COM hosting interface.

            ICorRuntimeHost corHost = (ICorRuntimeHost) new CorRuntimeHost();

            System.IntPtr hEnum;
            corHost.EnumDomains(out hEnum);

            while (true)
            {
                object nextDomain;
                corHost.NextDomain(hEnum, out nextDomain);
                if (nextDomain == null)
                {
                    break;
                }

                System.AppDomain domain = (System.AppDomain)nextDomain;
                if (domain.FriendlyName == friendlyName)
                {
                    appDomain = domain;
                    break;
                }
            }

            corHost.CloseEnum(hEnum);
            Marshal.ReleaseComObject(corHost);

            return(appDomain);
        }
예제 #5
0
        public IEnumerable <string> Get()
        {
            string[] domainNames = new string[10];
            int      ii          = 0;

            ICorRuntimeHost host       = null;
            IntPtr          enumHandle = IntPtr.Zero;

            try
            {
                host = new mscoree.CorRuntimeHost();
                host.EnumDomains(out enumHandle);
                object domain = null;

                host.NextDomain(enumHandle, out domain);
                while (domain != null && ii < 10)
                {
                    AppDomain ad = (AppDomain)domain;
                    domainNames[ii++] = ad.FriendlyName;
                    host.NextDomain(enumHandle, out domain);
                }
            }
            finally
            {
                if (host != null)
                {
                    if (enumHandle != IntPtr.Zero)
                    {
                        host.CloseEnum(enumHandle);
                    }

                    Marshal.ReleaseComObject(host);
                }
            }

            return(domainNames);
        }