예제 #1
1
        /// <摘要>
        /// 获得当前进程中的所有应用程序域
        /// 此方法用ICorRuntimeHost接口获得当前进程中的应用程序域的枚举
        /// </摘要>
        public static List<AppDomain> GetAppDomainsInCurrentProcess()
        {
            var appDomains = new List<AppDomain>();
            var hEnum = IntPtr.Zero;

            // CorRuntimeHostClass提供了ICorRuntimeHost接口
            var host = new CorRuntimeHost();

            try
            {

                // 获得当前进程中的所有应用程序域
                host.EnumDomains(out hEnum);
                while (true)
                {
                    object domain;
                    host.NextDomain(hEnum, out domain);
                    if (domain == null)
                        break;
                    appDomains.Add(domain as AppDomain);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                host.CloseEnum(hEnum);
                Marshal.ReleaseComObject(host);
            }
            return appDomains;
        }
예제 #2
0
파일: Program.cs 프로젝트: tjuckett/OmniEve
        //
        //https://stackoverflow.com/questions/388554/list-appdomains-in-process
        //
        //Remember to reference COM object \WINDOWS\Microsoft.NET\Framework\vXXX\mscoree.tlb, set reference mscoree "Embed Interop Types" as "False".
        public static IEnumerable<AppDomain> EnumAppDomains()
        {
            IList<AppDomain> appDomains = new List<AppDomain>();
            IntPtr enumHandle = IntPtr.Zero;
            ICorRuntimeHost host = null;

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

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

                    Marshal.ReleaseComObject(host);
                }
            }
        }
예제 #3
0
        public static IEnumerable <AppDomain> GetAppDomains()
        {
            List <AppDomain> result = new List <AppDomain>();

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

            try
            {
                host.EnumDomains(out enumHandle);

                object domain = null;
                while (true)
                {
                    host.NextDomain(enumHandle, out domain);
                    if (domain == null)
                    {
                        break;
                    }
                    AppDomain appDomain = (AppDomain)domain;
                    result.Add(appDomain);
                }
            }
            finally
            {
                host.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(host);
                host = null;
            }
            return(result);
        }
        private static AppDomain GetAppDomain(string friendlyName)
        {
            IntPtr          enumHandle = IntPtr.Zero;
            ICorRuntimeHost host       = new CorRuntimeHost();

            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);
        }
예제 #5
0
        /// <summary>
        /// Gets all AppDomains in current process.
        /// This method uses ICorRuntimeHost interface to get an enumerator for the domains
        /// in the current process.
        /// </summary>
        public static List <AppDomain> GetAppDomainsInCurrentProcess()
        {
            var appDomains = new List <AppDomain>();
            var hEnum      = IntPtr.Zero;

            // The class CorRuntimeHostClass is a coclass, which implements the
            // ICorRuntimeHost interface.
            var host = new CorRuntimeHost();

            try
            {
                // Gets an enumerator for the domains in the current process.
                host.EnumDomains(out hEnum);
                while (true)
                {
                    object domain;
                    host.NextDomain(hEnum, out domain);
                    if (domain == null)
                    {
                        break;
                    }
                    appDomains.Add(domain as AppDomain);
                }
            }
            finally
            {
                host.CloseEnum(hEnum);
                Marshal.ReleaseComObject(host);
            }
            return(appDomains);
        }
예제 #6
0
        public static AppDomain[] GetAllAppDomains()
        {
            List<AppDomain> appDomains = new List<AppDomain>();
            IntPtr handle = IntPtr.Zero;
            CorRuntimeHost host = 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.ToArray();
        }
예제 #7
0
        static IEnumerable <AppDomain> GetLoadedAppDomains()
        {
            var    appDomains = new List <AppDomain>();
            IntPtr handle;
            var    runtimeHost = new CorRuntimeHost();

            runtimeHost.EnumDomains(out handle);
            var doContinue = true;

            while (doContinue)
            {
                object domain;
                runtimeHost.NextDomain(handle, out domain);
                if (domain == null)
                {
                    doContinue = false;
                }
                else
                {
                    appDomains.Add((AppDomain)domain);
                }
            }
            runtimeHost.CloseEnum(handle);
            return(appDomains);
        }
예제 #8
0
        public static IList <System.AppDomain> GetAppDomains()
        {
            IList <System.AppDomain> _IList = new List <System.AppDomain>();
            IntPtr          enumHandle      = IntPtr.Zero;
            ICorRuntimeHost host            = new CorRuntimeHost();

            try
            {
                host.EnumDomains(out enumHandle);
                object domain = null;
                while (true)
                {
                    host.NextDomain(enumHandle, out domain);
                    if (domain == null)
                    {
                        break;
                    }
                    System.AppDomain appDomain = (System.AppDomain)domain;
                    _IList.Add(appDomain);
                }
                return(_IList);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }
            finally
            {
                host.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(host);
            }
        }
예제 #9
0
        // To do: provide an option to exlude namespaces starting with System.
        private void GetAllDomains()
        {
            _domains = new List<AppDomain>();

            IntPtr enumHwnd = IntPtr.Zero;
            ICorRuntimeHost host = new CorRuntimeHost();
            try
            {
                host.EnumDomains(out enumHwnd);
                object domain = null;
                while (true)
                {
                    host.NextDomain(enumHwnd, out domain);
                    if (domain == null)
                        break;

                    _domains.Add((AppDomain) domain);
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                host.CloseEnum(enumHwnd);
                Marshal.ReleaseComObject(host);
            }
        }
예제 #10
0
        public static AppDomain GetAppDomain(string AppDomainName)
        {
            AppDomain      AppDom = null;
            IntPtr         Handle = IntPtr.Zero;
            CorRuntimeHost Host   = new CorRuntimeHost();

            try
            {
                Host.EnumDomains(out Handle);
                while (true)
                {
                    object domain;
                    Host.NextDomain(Handle, out domain);
                    if (domain == null)
                    {
                        break;
                    }

                    if (((AppDomain)domain).FriendlyName == AppDomainName)
                    {
                        AppDom = (AppDomain)domain;
                        break;
                    }
                }
            }
            finally
            {
                if (Handle != IntPtr.Zero)
                {
                    Host.CloseEnum(Handle);
                }
            }

            return(AppDom);
        }
예제 #11
0
        /// <摘要>
        /// 获得当前进程中的所有应用程序域
        /// 此方法用ICorRuntimeHost接口获得当前进程中的应用程序域的枚举
        /// </摘要>
        public static List <AppDomain> GetAppDomainsInCurrentProcess()
        {
            var appDomains = new List <AppDomain>();
            var hEnum      = IntPtr.Zero;

            // CorRuntimeHostClass提供了ICorRuntimeHost接口
            var host = new CorRuntimeHost();

            try
            {
                // 获得当前进程中的所有应用程序域
                host.EnumDomains(out hEnum);
                while (true)
                {
                    object domain;
                    host.NextDomain(hEnum, out domain);
                    if (domain == null)
                    {
                        break;
                    }
                    appDomains.Add(domain as AppDomain);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                host.CloseEnum(hEnum);
                Marshal.ReleaseComObject(host);
            }
            return(appDomains);
        }
예제 #12
0
        //
        //https://stackoverflow.com/questions/388554/list-appdomains-in-process
        //
        //Remember to reference COM object \WINDOWS\Microsoft.NET\Framework\vXXX\mscoree.tlb, set reference mscoree "Embed Interop Types" as "False".

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

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

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

                    Marshal.ReleaseComObject(host);
                }
            }
        }
예제 #13
0
        public static string GetAllAppDomainNames()
        {
            string         AppDom = "";
            IntPtr         Handle = IntPtr.Zero;
            CorRuntimeHost Host   = new CorRuntimeHost();

            try
            {
                Host.EnumDomains(out Handle);
                while (true)
                {
                    object domain;
                    Host.NextDomain(Handle, out domain);
                    if (domain == null)
                    {
                        break;
                    }

                    if (AppDom == "")
                    {
                        if (((AppDomain)domain).IsDefaultAppDomain())
                        {
                            AppDom = "DefaultDomain";
                        }
                        else
                        {
                            AppDom = ((AppDomain)domain).FriendlyName;
                        }
                    }
                    else
                    {
                        if (((AppDomain)domain).IsDefaultAppDomain())
                        {
                            AppDom += "\tDefaultDomain";
                        }
                        else
                        {
                            AppDom += "\t" + ((AppDomain)domain).FriendlyName;
                        }
                    }
                }
            }
            finally
            {
                if (Handle != IntPtr.Zero)
                {
                    Host.CloseEnum(Handle);
                }
            }

            return(AppDom);
        }
예제 #14
0
        public static void ListAllAppDomains()
        {
            var currentDomain = AppDomain.CurrentDomain;

            IList <AppDomain> list       = new List <AppDomain>();
            IntPtr            enumHandle = IntPtr.Zero;
            ICorRuntimeHost   host       = new CorRuntimeHost();

            try
            {
                host.EnumDomains(out enumHandle);
                object domain = null;
                while (true)
                {
                    host.NextDomain(enumHandle, out domain);
                    if (domain == null)
                    {
                        break;
                    }
                    AppDomain appDomain = (AppDomain)domain;
                    list.Add(appDomain);
                }

                foreach (var item in list)
                {
                    if (currentDomain.Id == item.Id)
                    {
                        Console.BackgroundColor = ConsoleColor.DarkBlue;
                    }

                    Console.WriteLine($"-> Domain friendly name: {item.FriendlyName}");
                    Console.ResetColor();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                host.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(host);
            }
        }
        private static IList <AppDomain> GetAppDomains()
        {
            IList <AppDomain> result = new List <AppDomain>();
            var enumHandle           = IntPtr.Zero;
            var runtimeHost          = new CorRuntimeHost();

            try
            {
                runtimeHost.EnumDomains(out enumHandle);

                while (true)
                {
                    runtimeHost.NextDomain(enumHandle, out var domain);

                    if (domain == null)
                    {
                        break;
                    }

                    var appDomain = (AppDomain)domain;
                    result.Add(appDomain);
                }

                return(result);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
                return(null);
            }
            finally
            {
                runtimeHost.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(runtimeHost);
            }
        }