进程和模块查看器






2.85/5 (10投票s)
显示进程详情和附加的模块。

图1 ProcMonitor 1.0.0.1

图2 ProcMonitor 1.0.0.0
引言
使用 ProcMonitor 可以查看当前用户下运行的进程的详细信息,例如:
- 附加了哪些模块(DLL),
- 正在运行多少个句柄和线程,
- 进程版本,
- 等等。
新版本中的更改
这是 ProcMonitor 1.0.0.0 的升级。在这个新版本中,我添加了一个notifyIcon,为列表视图添加了一个图标,添加了表格布局和面板来控制定位和布局。
使用代码
该应用程序使用 Visual Studio 2008 Team System 编译。因此,它无法在早期版本的 Visual Studio 下编译/运行,但是您可以使用早期版本的 Visual Studio 复制代码或使用现有文件。以下是源代码的列表
应用程序中使用的命名空间。
using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Text; using System.Windows.Forms;
在下面的代码中,我在formMain类中声明了一个ListView,并将所有正在运行的进程保存在一个Process数组Process[]中。
namespace ProcMonitor
{
    public partial class formMain : Form
    {
        public ListViewItem item1; /* ListView to display the process */
        public Process[] proc = Process.GetProcesses(); // retrieving all running process
下面是btnExit_Click、btnAboutUs_Click(非常简单)和formMain_Load事件。在formMain_Load事件中,调用了函数EnumerateProcess()。在这个函数中,使用了位于System.Diagnostics命名空间中的Process类。EnumerateProcess()函数枚举当前用户会话中所有正在运行的进程,并将其存储在Process数组中,即proc[]。
        public formMain()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void btnAboutUs_Click(object sender, EventArgs e)
        {
            AboutBox frmAbout = new AboutBox();
            frmAbout.Show();
        }
        private void formMain_Load(object sender, EventArgs e)
        {
            try
            {
                EnumerateProcess();
            }
            catch(Exception ex)
            {
                Throw ex;
            }
        }
以下代码用于按升序和降序对ListView进行排序。
        private void listViewProcess_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (listViewProcess.Sorting == SortOrder.Ascending)
            {
                listViewProcess.Sorting = SortOrder.Descending;
            }
            else
                listViewProcess.Sorting = SortOrder.Ascending;
         } 
最后,以下代码是应用程序的核心,用于显示进程的详细信息,例如模块、版本、内存使用情况、句柄计数、线程计数等。要检索模块和相关信息,使用了ProcessModuleCollection类。
    private void listViewProcess_Click(object sender, EventArgs e)
    {
      if (listViewProcess.SelectedItems.Count != 0)
      {
        try
        {
          string Selected_Process = listViewProcess.SelectedItems[0].Text;
          Process[] ObjModulesList = Process.GetProcessesByName(Selected_Process);
          ProcessModuleCollection ObjModules = ObjModulesList[0].Modules;
          labelProcessName.Text = ObjModulesList[0].MainModule.ModuleName.ToString();
          labelProcessID.Text = ObjModulesList[0].Id.ToString();
          labelThreads.Text = ObjModulesList[0].Threads.Count.ToString();
          labelPriority.Text = ObjModulesList[0].PriorityClass.ToString();
          labelProcessDescription.Text = 
            ObjModulesList[0].MainModule.FileVersionInfo.FileDescription.ToString();
          labelMemoryUsage.Text = 
            ((ObjModulesList[0].WorkingSet64) / 1024).ToString();
          labelHandles.Text = ObjModulesList[0].HandleCount.ToString();
          labelPath.Text = ObjModulesList[0].MainModule.FileName.ToString();
          labelProcessVersion.Text = 
                  ObjModulesList[0].MainModule.FileVersionInfo.FileVersion.ToString();
          listViewModules.Items.Clear();
          foreach (ProcessModule objModule in ObjModules)
          {
            item1 = new ListViewItem(objModule.ModuleName);
            item1.SubItems.Add(objModule.FileName);
            item1.SubItems.Add(objModule.FileVersionInfo.CompanyName);
            item1.SubItems.Add(objModule.FileVersionInfo.FileVersion.ToString());
            item1.SubItems.Add(objModule.FileVersionInfo.FileDescription.ToString());
            listViewModules.Items.AddRange(new ListViewItem[] { item1 });
          }
          labelTotalModulesAttached.Text = ObjModules.Count.ToString();
        }
        catch (SystemException SysEX)
        {
          MessageBox.Show("Error: While reading the process, the following error"
              "occured.\n"+SysEX.Message,
              "Exception",MessageBoxButtons.OK,MessageBoxIcon.Error);
          labelProcessName.Text = string.Empty;
          labelProcessID.Text = string.Empty;
          labelThreads.Text = string.Empty;
          labelPriority.Text = string.Empty;
          labelProcessDescription.Text = string.Empty;
          labelMemoryUsage.Text = string.Empty;
          labelHandles.Text = string.Empty;
          labelPath.Text = string.Empty;
          labelProcessVersion.Text = string.Empty;
          labelTotalModulesAttached.Text = string.Empty;
          listViewModules.Items.Clear();
        }
      }
    }
    private void listViewModules_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        if (listViewModules.Sorting == SortOrder.Ascending)
        {
            listViewModules.Sorting = SortOrder.Descending;
        }
        else
            listViewModules.Sorting = SortOrder.Ascending;
    }
    private void btnRefresh_Click(object sender, EventArgs e)
    {
        try
        {
            EnumerateProcess();
        }
        catch(Exception ex)
        {
           Throw ex;
        }
    }
    public void EnumerateProcess()
    {
      listViewProcess.Items.Clear();
      foreach (Process pr in proc)
      {
        pr.Refresh();
        if (pr.BasePriority <13)
        {
          if (pr.SessionId == 1)
          {
            item1 = new ListViewItem(pr.ProcessName);
            item1.SubItems.Add(pr.Id.ToString());
            item1.SubItems.Add(((pr.WorkingSet64) / 1024).ToString());
            item1.SubItems.Add(pr.MainModule.FileVersionInfo.FileDescription.ToString());
            listViewProcess.Items.AddRange(new ListViewItem[] { item1 });
          }
        }
      }
    }
      
  }
      
}
关注点
该程序是在 Windows Vista 上使用 Visual Studio 2008 开发的。Vista 不允许访问其他用户的会话,也不允许低优先级进程访问高优先级进程,但在开发过程中,有时应用程序显示它正在以高优先级运行,但仍然允许进程监视器访问其详细信息,而无需以管理员权限运行。这似乎很奇怪?????


