GregC
GregC

Reputation: 8007

How to enumerate running processes managed by CLR?

How to enumerate running processes? What about app domains?

Would there be any security-related gotchas?

I would be comfortable with .NET 4.0 only, Windows 7/Windows Server 2008 R2-only solution.

P.S.: This is what I am trying to do... ProcessExplorer.NET question

Upvotes: 4

Views: 1344

Answers (4)

GregC
GregC

Reputation: 8007

using System;
using System.Diagnostics;

namespace ConsoleApplication1
{
   class Program
   {
      static void Main(string[] args)
      {
         Process[] procs = Process.GetProcesses();
         foreach (Process proc in procs)
         {
            string processName = proc.ProcessName;
            int processId = proc.Id;

            try
            {
               if (proc.HasDesktop40CLR())
                  Console.WriteLine("#{0}, {1} (has Desktop CLR 4.0+)", processId, processName);

               if (proc.HasOlderDesktopCLR())
                  Console.WriteLine("#{0}, {1} (has Older Desktop CLR)", processId, processName);

               if (proc.HasMscorlib())
                  Console.WriteLine("#{0}, {1} (has MSCORLib)", processId, processName);
            }
            catch (Exception)
            {
               Console.WriteLine("#{0}, {1} (skipped/unknown)", processId, processName);
            }
         }
      }
   }

   public static class ProcessExtensions
   {
      public static bool HasDesktop40CLR(this Process proc)
      {
         for (int ix = 0; ix < proc.Modules.Count; ++ix)
         {
            var moduleName = proc.Modules[ix].ModuleName;

            if (string.Compare(moduleName, "mscoree.dll", ignoreCase: true) == 0)
               return true;

            if (string.Compare(moduleName, "mscoreei.dll", ignoreCase: true) == 0)
               return true;
         }

         return false;
      }

      public static bool HasOlderDesktopCLR(this Process proc)
      {
         for (int ix = 0; ix < proc.Modules.Count; ++ix)
         {
            var moduleName = proc.Modules[ix].ModuleName;

            if (string.Compare(moduleName, "mscorwks.dll", ignoreCase: true) == 0)
               return true;
         }

         return false;
      }

      public static bool HasMscorlib(this Process proc)
      {
         for (int ix = 0; ix < proc.Modules.Count; ++ix)
         {
            var moduleName = proc.Modules[ix].ModuleName;

            if (string.Compare(moduleName, "mscorlib.dll", ignoreCase: true) == 0)
               return true;

            if (string.Compare(moduleName, "mscorlib.ni.dll", ignoreCase: true) == 0)
               return true;
         }

         return false;
      }
   }
}

Upvotes: 1

Nate
Nate

Reputation: 30646

System.Diagnostics.Process.GetProcesses() will list all the running processes as Process objects.

Upvotes: 1

Steve
Steve

Reputation: 438

Once you use Process.GetProcesses above, you can use ICLRMetaHost::EnumerateLoadedRuntimes to see if the process is running an instance of the CLR, and get the versions of the CLR running in it. http://msdn.microsoft.com/en-us/library/dd233119.aspx. The API isn't exposed to .NET directly, but it's COM so you can tlbimport mscoree.dll and use the autogenerated wrappers in .NET.

Upvotes: 1

Related Questions