How to traverse the AppDomain in the current process

original
2014/06/11 12:00
Reading number 103

The. Net Framework does not provide a managed interface to obtain other AppDomains in the current process! Therefore, we have to use Hosting Interfaces to accomplish this.
At MSCOREE DLL Net 1.0 provides an ICorRuntimeHost interface, which includes EnumDomains and NextDomain methods. Fortunately, MSCOREE The DLL actually directly provides a public implementation: CorRuntimeHostClass. Therefore, we just need to create a new CorRuntimeHostClass and call the EnumDomains and NextDomain methods:

 


  
  
  1. public   static  IList GetAppDomains() 
  2.     IList list =  new  List();  
  3.     IntPtr enumHandle = IntPtr. Zero;  
  4.     CorRuntimeHostClass host =  new  CorRuntimeHostClass();  
  5.      try  
  6.     { 
  7.         host. EnumDomains( out  enumHandle);  
  8.          object  domain =  null ;  
  9.          while  ( true
  10.         { 
  11.             host. NextDomain(enumHandle,  out  domain);  
  12.              if  (domain ==  null break ;  
  13.             AppDomain appDomain = (AppDomain)domain;  
  14.             list. Add(appDomain);  
  15.         } 
  16.          return  list;  
  17.     } 
  18.      catch  (Exception e) 
  19.     { 
  20.          return   null ;  
  21.     } 
  22.      finally   
  23.     { 
  24.         host. CloseEnum(enumHandle);   
  25.         Marshal. ReleaseComObject(host);   
  26.     } 

For MSCOREE For DLL assembly reference, please use COM reference: C: WINDOWS Microsoft NET\Framework\vXXXXXX\mscoree.tlb
About MSCOREE For a more detailed description of DLL, see . NET: MSCOREE DLL

This article is from“ Grapevine Control Blog ”Blog, please keep this source http://powertoolsteam.blog.51cto.com/2369428/477795

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top