运行程序时是否可以获取正在运行的进程及其相应的应用程序域的列表?我知道 mscoree.dll 允许我使用 ICorRuntimeHost.EnumDomains 方法检索当前进程的所有应用程序域。有没有一种方法可以不使用外部 API而仅使用纯 C# 代码来获取此信息?我知道 mdbg 有一些可能有帮助的功能,但我不确定如何使用这个调试器。我真的正在寻找仅使用 C# 的解决方案。
谢谢
编辑: 目标是在 html 页面上显示正在运行的每个进程及其相应的应用程序域。理想情况下,应该有一个函数可以迭代所有正在运行的进程并检索此信息。
检索当前进程的所有应用程序域的代码:
private static List<AppDomainInf> GetAppDomains()
{
IList<AppDomain> mAppDomainsList = new List<AppDomain>();
List<AppDomainInf> mAppDomainInfos = new List<AppDomainInf>();
IntPtr menumHandle = IntPtr.Zero;
ICorRuntimeHost host = new CorRuntimeHost();
try
{
host.EnumDomains(out menumHandle);
object mTempDomain = null;
//add all the current app domains running
while (true)
{
host.NextDomain(menumHandle, out mTempDomain);
if (mTempDomain == null) break;
AppDomain tempDomain = mTempDomain as AppDomain;
mAppDomainsList.Add((tempDomain));
}
//retrieve every app domains detailed information
foreach (var appDomain in mAppDomainsList)
{
AppDomainInf domainInf = new AppDomainInf();
domainInf.Assemblies = GetAppDomainAssemblies(appDomain);
domainInf.AppDomainName = appDomain.FriendlyName;
mAppDomainInfos.Add(domainInf);
}
return mAppDomainInfos;
}
catch (Exception)
{
throw; //rethrow
}
finally
{
host.CloseEnum(menumHandle);
Marshal.ReleaseComObject(host);
}
}
最佳答案
使用位于 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\MdbgCore.dll 内的 MdbgCore.dll。:
CorPublish cp = new CorPublish();
foreach (CorPublishProcess process in cp.EnumProcesses())
{
foreach (CorPublishAppDomain appDomain in process.EnumAppDomains())
{
}
}
关于c# - 获取所有进程及其相应的应用程序域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14758915/