简体   繁体   English

C:其他系统进程的捕获进程终止

[英]C : Catch Process Termination for other system processes

I need to catch process termination signals for child processes. 我需要捕获子进程的进程终止信号。

So for example, if my Win32 console application spawns a notepad process and the user closes notepad, I would like to detect that. 因此,例如,如果我的Win32控制台应用程序产生了一个记事本进程,而用户关闭了记事本,我想检测到这一点。

I don't want to block (asynchronous model) 我不想阻止(异步模型)

I'm creating a process using the win api CreateProcess 我正在使用Win API CreateProcess创建一个进程

Have you tried WaitForSingleObject() with it's dwMilliseconds parameter as 0? 您是否尝试过将其dwMilliseconds参数设置为0的WaitForSingleObject() WaitForSingleObject() will return immediately if dwMilliseconds is 0 and will return WAIT_TIMEOUT if the process isn't dead or WAIT_OBJECT_0 if it is. 如果dwMilliseconds为0, WaitForSingleObject()将立即返回;如果进程未WAIT_OBJECT_0WaitForSingleObject()将返回WAIT_TIMEOUT否则,将返回WAIT_OBJECT_0
Example, assuming the child process handle is hProcess : 例如,假设子进程句柄是hProcess

DWORD result = WaitForSingleObject(hProcess, 0);
if (result == WAIT_TIMEOUT)
    /* Process not dead */;
else if (result == WAIT_OBJECT_0)
    /* Process dead */;
else
    /* Error occured */;

And alternative is GetExitCodeProcess() . 另一种方法是GetExitCodeProcess() The "exit code" returned by it will be STILL_ACTIVE if it is still running, otherwise it will return the actual exit code. 如果它仍在运行,则返回的“退出代码”将为STILL_ACTIVE ,否则它将返回实际的退出代码。 Example, again assuming the child process handle is hProcess : 示例,再次假设子进程句柄是hProcess

DWORD exitCode;
if (!GetExitCodeProcess(hProcess, &exitCode))
    /* Error occured */;
else if (exitCode == STILL_ACTIVE)
    /* Process is still running */
else
    /* exitCode now contains the process exit code, and the process is not running anymore */;

Both these examples are non-blocking 这两个例子都是非阻塞的

Use WaitForSingleObject from a new thread. 从新线程中使用WaitForSingleObject

See Waiting for a Process to Terminate 请参阅等待进程终止

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

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