2013-02-07 61 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()) 
        { 

        } 
       } 
相關問題