繁体   English   中英

如何监控应用程序是否打开?

[英]How to monitor that an application is opened?

我正在实施一个桌面分析应用程序,它需要记录用户在 PC 上打开的程序的名称和时间。 它是一个 C# (WPF) 应用程序,在用户登录时启动并在没有 UI 的情况下运行。 对于 Word 或 IE 等程序,它还会捕获他们正在查看的文档或 URL。

目前我有一个工作解决方案如下:

安装 Windows Hook for Mouse Down。 当该事件触发时,我使用 p-Invoke 到“GetForegroundWindow”,然后使用窗口句柄到“GetWindowThreadProcessId”,通过 ProcessId 我可以获得包含名称、开始时间和参数开始列表的 System.Diagnostics.Process 对象。 我维护一个历史列表,所以我只写一个跟踪条目,如果这个 processId/窗口句柄组合以前没有被记录过。

此解决方案确实可以正常工作,但需要鼠标挂钩,该挂钩可能会被 Windows 丢弃,而不会发出任何通知或无法有问题地检查它是否仍然被挂钩。 更不用说这个实现似乎是一个黑客。

如果有更好更直接的方法请指教。

谢谢。

您可以使用__InstanceCreationEvent事件和Win32_Process WMI 类来监视创建的进程。

试试这个示例 C# 应用程序

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;


namespace GetWMI_Info
{
    public class EventWatcherAsync 
    {
        private void WmiEventHandler(object sender, EventArrivedEventArgs e)
        {
            //in this point the new events arrives
            //you can access to any property of the Win32_Process class
            Console.WriteLine("TargetInstance.Handle :    " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Handle"]);
            Console.WriteLine("TargetInstance.Name :      " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Name"]);

        }

        public EventWatcherAsync()
        {
            try
            {
                string ComputerName = "localhost";
                string WmiQuery;
                ManagementEventWatcher Watcher;
                ManagementScope Scope;                

                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
                Scope.Connect();

                WmiQuery ="Select * From __InstanceCreationEvent Within 1 "+
                "Where TargetInstance ISA 'Win32_Process' ";

                Watcher = new ManagementEventWatcher(Scope, new EventQuery(WmiQuery));
                Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler);
                Watcher.Start();
                Console.Read();
                Watcher.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception {0} Trace {1}", e.Message, e.StackTrace);
            }

        }

        public static void Main(string[] args)
        {
           Console.WriteLine("Listening process creation, Press Enter to exit");
           EventWatcherAsync eventWatcher = new EventWatcherAsync();
           Console.Read();
        }
    }
}

如果您想监视在 Windows 上运行的所有内容的性能,则可以使用PerformanceCounter 类 每次应用程序启动时,windows 都会创建数十个性能计数器来跟踪应用程序的 ProcessID、CPU 使用情况、内存使用情况、每秒 I/O 操作等。

例如,以下代码将为您提供 Chrome 的进程 ID:

PerformanceCounter perf = new PerformanceCounter("Process", "ID Process", "chrome");
int procId = (int)perf.NextValue();

您还可以使用PerformanceCounterCategory 类轻松枚举类别、实例和计数器。

您可以使用Windows 的 PerfMon 工具来了解您将能够检索哪些信息。 我建议您查看进程类别(使用 PerfMon),您将在其中找到所有活动进程的列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM