使用其appdomains及其程序集枚举所有CLR流程

时间:2014-01-24 08:10:56

标签: c# .net process .net-assembly appdomain

我正在尝试使用MdbgCore.dll - CLR调试接口的托管包装器来帮助枚举所有托管进程并列出他们拥有的应用程序域及其中的所有程序集。下面的代码似乎有用,但它没有看到大多数CLR程序 - EnumProcesses只返回单个正在运行的程序,而我确信还有更多(例如我的测试启动):

CorPublish corPublish = new CorPublish();

Func<int, int, List<CorAssembly>> EnumerateAssemblies = (pid, adid) => {
    MDbgEngine engine = new MDbgEngine();
    engine.Attach(pid);
    var result = engine.Processes.Active.Modules
            .Cast<MDbgModule>()
            .Select(m => m.CorModule.Assembly)
            .Where(a => a.AppDomain.Id == adid)
            .ToList();
    engine.Processes.Active.Detach().WaitOne();
    return result;
};
var result = corPublish.EnumProcesses()
    .Cast<CorPublishProcess>()
    .SelectMany(p => p.EnumAppDomains()
                      .Cast<CorPublishAppDomain>()
                      .Select(d => new
                        {
                            Process = p,
                            Domain = d
                        }))
    // I ommit my own program as debugger is already attached
    .Where(x => !x.Process.DisplayName.Contains("Console.vshost.exe"))
    .SelectMany(x => EnumerateAssemblies(x.Process.ProcessId, x.Domain.Id)
                        .Select(a => new
                        {
                            Process = x.Process,
                            Domain = x.Domain,
                            Assembly = a
                        }))
    .ToList();

有没有人遇到过这个API的问题?那里几乎没有文档和示例。

注意:对于好奇,我只想评估此API的可用性以及编写简单的示例工具。我不打算在生产代码中使用它。

0 个答案:

没有答案