Home > Backend Development > C++ > How Can I Detect Win32 Process Creation/Termination Without a Kernel-Mode Driver in C ?

How Can I Detect Win32 Process Creation/Termination Without a Kernel-Mode Driver in C ?

Susan Sarandon
Release: 2024-11-21 02:47:10
Original
231 people have browsed it

How Can I Detect Win32 Process Creation/Termination Without a Kernel-Mode Driver in C  ?

Detect Win32 Process Creation/Termination without a Kernel-Mode Driver in C

While NT kernel-mode drivers offer a comprehensive API for process creation and termination notifications, it is possible to achieve similar functionality in C using only Win32 API functions.

Using WMI

WMI (Windows Management Instrumentation) provides a rich set of event notifications, including those related to process creation and termination. However, WMI may not be suitable for all scenarios, especially if you need to track process termination only.

Using a Callback

A more efficient approach is to use a callback function to wait for a specific process to terminate. This can be achieved with the following steps:

  1. Call OpenProcess to obtain a handle to the process you want to monitor.
  2. Call RegisterWaitForSingleObject with the process handle as a parameter. Specify WaitOrTimerCallback as the callback function to be executed when the process terminates.
  3. The WaitOrTimerCallback function will be called once the process terminates.

Example Code

VOID CALLBACK WaitOrTimerCallback(
    _In_  PVOID lpParameter,
    _In_  BOOLEAN TimerOrWaitFired
)
{
    MessageBox(0, L"The process has exited.", L"INFO", MB_OK);
    return;
}

DWORD dwProcessID = 1234;
HANDLE hProcHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);

HANDLE hNewHandle;
RegisterWaitForSingleObject(&hNewHandle, hProcHandle , WaitOrTimerCallback, NULL, INFINITE, WT_EXECUTEONLYONCE);
Copy after login

This code will display a message box once the process with ID dwProcessID terminates.

The above is the detailed content of How Can I Detect Win32 Process Creation/Termination Without a Kernel-Mode Driver in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template