.net 列出正在处理的AppDomains
是否有可能在进程中枚举AppDomains?您可能需要查看.net 列出正在处理的AppDomains,.net,process,appdomain,.net,Process,Appdomain,是否有可能在进程中枚举AppDomains?您可能需要查看 使用System.Runtime.InteropServices; //添加以下内容作为COM参考-C:\WINDOWS\Microsoft.NET\Framework\VXXXXX\mscoree.tlb 使用mscoree; 公共静态IList GetAppDomains() { IList_IList=新列表(); IntPtr enumHandle=IntPtr.Zero CorRuntimeHostClass host=new
使用System.Runtime.InteropServices;
//添加以下内容作为COM参考-C:\WINDOWS\Microsoft.NET\Framework\VXXXXX\mscoree.tlb
使用mscoree;
公共静态IList GetAppDomains()
{
IList_IList=新列表();
IntPtr enumHandle=IntPtr.Zero
CorRuntimeHostClass host=new mscoree.CorRuntimeHostClass();
尝试
{
host.EnumDomains(out-enumHandle);
对象域=空;
while(true)
{
host.NextDomain(enumHandle,out-domain);
如果(域==null)中断;
AppDomain AppDomain=(AppDomain)域;
_IList.Add(appDomain);
}
返回IList;
}
捕获(例外e)
{
Console.WriteLine(如ToString());
返回null;
}
最后
{
host.CloseEnum(enumHandle);
Marshal.ReleaseComObject(主机);
}
}
}
更新
您可以将接口添加为:
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace mscoree
{
[CompilerGenerated]
[Guid("CB2F6722-AB3A-11D2-9C40-00C04FA30A3E")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[TypeIdentifier]
[ComImport]
[CLSCompliant(false)]
public interface ICorRuntimeHost
{
void _VtblGap1_11();
void EnumDomains(out IntPtr enumHandle);
void NextDomain([In] IntPtr enumHandle, [MarshalAs(UnmanagedType.IUnknown)] out object appDomain);
void CloseEnum([In] IntPtr enumHandle);
}
}
并添加一个方法:
private static ICorRuntimeHost GetCorRuntimeHost()
{
return (ICorRuntimeHost)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("CB2F6723-AB3A-11D2-9C40-00C04FA30A3E")));
}
那么就不需要mscoree.tlb引用了。并做出如下更改:
using mscoree;
ICorRuntimeHost host = null;
host = GetCorRuntimeHost();
这是我目前解决这个问题的代码
原始答案 我将其细化为:
using System.Runtime.InteropServices;
using mscoree;
public static IEnumerable<AppDomain> EnumAppDomains()
{
IntPtr enumHandle = IntPtr.Zero;
CorRuntimeHostClass 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“嵌入互操作类型”设置为“False”。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
在.NET 4.0中,您必须替换
CorRuntimeHostClass host=new CorRuntimeHostClass()代码>带有ICorRuntimeHost主机=新CorRuntimeHost()代码>。我只想对这段代码表示感谢。非常有用。请查看是否要在.NET 4.0中使用CorRuntimeHostClass而不是ICorRuntimeHost。对于像我这样不熟悉添加COM引用的人,我可以通过右键单击项目,进入“添加->引用”,然后选中“COM->公共语言运行时执行环境”来完成此操作。这告诉我“ICorruptimeHost由于其保护级别而无法访问”。谢谢你的问题。原始帖子应该使用CorruptimeHostClass
而不是'ICorruptimeHost'。我提供了另一种更好的方法作为原始答案的更新。这对我很有效,我们希望在outlook addin中打开html页面。
<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)