列出正在处理的AppDomain

时间:2008-12-23 10:09:50

标签: .net process appdomain

是否有可能在进程内枚举AppDomains?

3 个答案:

答案 0 :(得分:71)

您可能需要查看this post

using System.Runtime.InteropServices;
// Add the following as a COM reference - C:\WINDOWS\Microsoft.NET\Framework\vXXXXXX\mscoree.tlb
using mscoree;                              

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

答案 1 :(得分:8)

我将其改进为:

using System.Runtime.InteropServices;
using mscoree;

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

    try
    {
        host = new CorRuntimeHostClass();
        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);
        }
    }
}

将其称为:

foreach (AppDomain appDomain in EnumAppDomains())
{
    // use appDomain
}

请记住引用COM对象\ WINDOWS \ Microsoft.NET \ Framework \ vXXX \ mscoree.tlb,将引用mscoree“Embed Interop Types”设置为“False”。

答案 2 :(得分:0)

VB.NET:

<System.Runtime.CompilerServices.CompilerGenerated>
<System.Runtime.InteropServices.Guid("CB2F6722-AB3A-11D2-9C40-00C04FA30A3E")>
<System.Runtime.InteropServices.InterfaceType(Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown)>
<System.Runtime.InteropServices.ComImport>
<System.CLSCompliant(False)>
Interface ICorRuntimeHost
    Sub _VtblGap1_11()
    Sub EnumDomains(<System.Runtime.InteropServices.Out> ByRef enumHandle As IntPtr)
    Sub NextDomain(<System.Runtime.InteropServices.[In]> ByVal enumHandle As IntPtr, <System.Runtime.InteropServices.Out> <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IUnknown)> ByRef appDomain As Object)
    Sub CloseEnum(<System.Runtime.InteropServices.[In]> ByVal enumHandle As IntPtr)
End Interface

Private Function GetCorRuntimeHost() As ICorRuntimeHost
    Return CType(Activator.CreateInstance(System.Type.GetTypeFromCLSID(New Guid("CB2F6723-AB3A-11D2-9C40-00C04FA30A3E"))), ICorRuntimeHost)
End Function

Public Function GetAppDomains() As List(Of AppDomain)
    Dim ls As List(Of AppDomain) = New List(Of AppDomain)
    Dim enumHandle As IntPtr = IntPtr.Zero

    Dim host As ICorRuntimeHost = GetCorRuntimeHost()

    Try
        host.EnumDomains(enumHandle)
        Dim domain As Object = Nothing

        While True
            host.NextDomain(enumHandle, domain)
            If domain Is Nothing Then Exit While
            Dim appDomain As AppDomain = CType(domain, AppDomain)
            ls.Add(appDomain)
        End While

        Return ls
    Catch e As Exception
        Console.WriteLine(e.ToString())
        Return Nothing
    Finally
        host.CloseEnum(enumHandle)
        Runtime.InteropServices.Marshal.ReleaseComObject(host)
    End Try
End Function

用法:

Dim ls As List(Of AppDomain) = GetAppDomains()
System.Console.WriteLine(ls)