Here is section of code that can list all AppDomain of the current process
using System.Runtime.InteropServices; // for domain enum using mscoree; // for domain enum. Add the following as a COM reference - C:\WINDOWS\Microsoft.NET\Framework\vXXXXXX\mscoree.tlb namespace MyNS { public class ListProcessAppDomains { public static IListGetAppDomains() { IList _IList = new List (); IntPtr enumHandle = IntPtr.Zero; CorRuntimeHostClass host = new mscoree.CorRuntimeHostClass(); try { host.EnumDomains(out enumHandle); object domain = null; while (true) { host.NextDomain(enumHandle, out domain); if (domain == null) break; AppDomain appDomain = (AppDomain)domain; _IList.Add(appDomain); } return _IList; } catch (Exception e) { Console.WriteLine(e.ToString()); return null; } finally { host.CloseEnum(enumHandle); Marshal.ReleaseComObject(host); } } } }
I slightly modified your code to support generics. I discovered it does not compile in NET 4.0 because CorRuntimeHostClass does not have a public constructor in .NET 4.0. In .NET 3.5, it still does and compiles and runs just fine.
ReplyDeleteThe rest of the gist linked to above shows how to initiate the class in powershell via reflection.