2013-02-07 62 views
1

运行程序时,是否可以获取正在运行的进程及其对应的应用程序域的列表?我知道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); 
     } 
    } 
+0

这需要一个调试函数ICorDebugProcess :: EnumerateAppDomains()。当然有更好的方法来实现你想要的,但是这个代码的目标是完全不可见的。 –

+0

感谢您的建议,但我期望避免使用任何C++库或进口如果可能。 – Matthew

回答

2

使用MdbgCore.dll位于内C:检索所有应用程序域的当前进程

代码\程序文件(x86)\微软的SDK \的Windows \ v7.0A \ BIN \ MdbgCore.dll :

CorPublish cp = new CorPublish(); 
foreach (CorPublishProcess process in cp.EnumProcesses()) 
      { 
        foreach (CorPublishAppDomain appDomain in process.EnumAppDomains()) 
        { 

        } 
       } 
相关问题