如何枚举CLR管理的正在运行的进程?

发布于 2024-11-02 18:50:24 字数 309 浏览 4 评论 0原文

如何枚举正在运行的进程?应用程序域怎么样?

会有任何与安全相关的问题吗?

我对仅 .NET 4.0、仅 Windows 7/Windows Server 2008 R2 的解决方案感到满意。

PS:这就是我正在努力做的事情...... ProcessExplorer.NET 问题

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爺獨霸怡葒院 2024-11-09 18:50:24

System.Diagnostics.Process.GetProcesses() 将列出所有正在运行的进程作为 Process 对象。

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

好菇凉咱不稀罕他 2024-11-09 18:50:24

使用上面的 Process.GetProcesses 后,您可以使用 ICLRMetaHost::EnumerateLoadedRuntimes 来查看进程是否正在运行 CLR 实例,并获取其中运行的 CLR 版本。 http://msdn.microsoft.com/en-us/library/dd233119.aspx 。该 API 不直接向 .NET 公开,但它是 COM,因此您可以 tlbimport mscoree.dll 并使用 .NET 中自动生成的包装器。

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.

丢了幸福的猪 2024-11-09 18:50:24
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;
      }
   }
}
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;
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文