如何在windows中监控进程/程序的执行?

发布于 2024-07-12 09:32:52 字数 168 浏览 6 评论 0原文

我们正在尝试开发一个小型应用程序,可以监视 Windows 计算机中正在执行的程序/进程。

如果程序/进程不应该运行,则应将其阻止。 它的工作原理类似于防病毒软件。

这是基本思想。

我想知道如何连接操作系统以获取有关尝试在计算机中运行的每个程序/进程的通知。

We are trying to develop a small application that can monitor the programs/processes that are executing in a windows machine.

If the program/process is not supposed to run, it should be blocked. It works similar to an antivirus.

This is the basic idea.

I want to know the ways to hook into the OS to get notified about every single program/process trying to run in the machine.

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

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

发布评论

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

评论(3

谷夏 2024-07-19 09:32:52

最简单的方法是使用 WMI。 特别监视Win32_ProcessStartTrace。 这比 Win32_Process 更好,因为它被设置为使用事件,而 Win32_Process 需要轮询,这对 CPU 更密集。 下面是如何在 C# 中执行此操作。 首先确保 System.Management 已设置为您的项目的参考。

    public System.Management.ManagementEventWatcher mgmtWtch;

    public Form1()
    {
        InitializeComponent();
        mgmtWtch = new System.Management.ManagementEventWatcher("Select * From Win32_ProcessStartTrace");
        mgmtWtch.EventArrived += new System.Management.EventArrivedEventHandler(mgmtWtch_EventArrived);
        mgmtWtch.Start();
    }

    void mgmtWtch_EventArrived(object sender, System.Management.EventArrivedEventArgs e)
    {
        MessageBox.Show((string)e.NewEvent["ProcessName"]);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        mgmtWtch.Stop();
    }

每次启动新进程时,代码都会生成一个消息框。 从那里您可以检查白名单/黑名单并采取适当的行动。

The easiest way is to use WMI. Specifically monitor the Win32_ProcessStartTrace. This is better than Win32_Process, because it is setup to use events whereas Win32_Process requires polling which is more CPU intensive. Below is how to do it in C#. First make sure that System.Management is setup as a reference for your project.

    public System.Management.ManagementEventWatcher mgmtWtch;

    public Form1()
    {
        InitializeComponent();
        mgmtWtch = new System.Management.ManagementEventWatcher("Select * From Win32_ProcessStartTrace");
        mgmtWtch.EventArrived += new System.Management.EventArrivedEventHandler(mgmtWtch_EventArrived);
        mgmtWtch.Start();
    }

    void mgmtWtch_EventArrived(object sender, System.Management.EventArrivedEventArgs e)
    {
        MessageBox.Show((string)e.NewEvent["ProcessName"]);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        mgmtWtch.Stop();
    }

The code will generate a messagebox everytime you launch a new process. From there you can check a whitelist/blacklist and act appropriately.

樱花细雨 2024-07-19 09:32:52

我还没有尝试过获取实时通知。 然而,以下是如何在 C# 中获取正在运行的进程

using System.Diagnostics;

 //Somewhere in your method

Process[] runningList = Process.GetProcesses();

foreach(Process p in runningList){
Console.WriteLine("Process: {0} ID: {1}", p.ProcessName, p.Id);
}

您还可以使用进程的以下属性

  • StartTime - 显示进程启动的时间
  • TotalProcessorTime - 显示进程占用的 CPU 时间
  • Threads - 提供对集合的访问进程中的线程

I havn't tried geting real-time notification. How ever, Here is how to get running processes in C#

using System.Diagnostics;

 //Somewhere in your method

Process[] runningList = Process.GetProcesses();

foreach(Process p in runningList){
Console.WriteLine("Process: {0} ID: {1}", p.ProcessName, p.Id);
}

You can also use the following props of a process

  • StartTime - Shows the time the process started
  • TotalProcessorTime - Shows the amount of CPU time the process has taken
  • Threads - gives access to the collection of threads in the process
荒路情人 2024-07-19 09:32:52

我会使用常量 WH_GETMESSAGE 检查 Win32-api SetWindowsHookEx ,以便在创建新窗口时向程序添加回调。

http://pinvoke.net/default.aspx/user32.SetWindowsHookEx

Google 该 API和 WH_GETMESSAGE 了解更多信息。

另请查看以下文章/代码库:
http://www.vbaccelerator.com/home/ Vb/Code/Libraries/Hooks/vbAccelerator_Hook_Library/article.asp

http://www.codeproject.com/KB/DLL/hooks.aspx?fid=2061& ;df=90&mpp=25&噪音=3&sort=位置&view=Quick&fr=76&select=726975

I would check up the Win32-api SetWindowsHookEx with the constant WH_GETMESSAGE to add a callback to your program when a new window is being created.

http://pinvoke.net/default.aspx/user32.SetWindowsHookEx

Google that API and WH_GETMESSAGE to find out more.

Also check out the following articles/code librarys:
http://www.vbaccelerator.com/home/Vb/Code/Libraries/Hooks/vbAccelerator_Hook_Library/article.asp

http://www.codeproject.com/KB/DLL/hooks.aspx?fid=2061&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=76&select=726975

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文