简体   繁体   English

在C / C ++ Windows中终止进程

[英]termination of process in C/C++ windows

I am trying to catch the time when the process is ended (by killing it in taskmanager for instance). 我试图赶上进程结束的时间(例如通过在taskmanager中杀死它)。 inorder that when it detects (inside the process that was ended), before exiting to do some work. 以便在退出前做一些工作时(在已结束的进程内部)检测到。 The application of mine doesn't end byitslef (endless loop) 我的应用程序并没有结束(无限循环)

I tried : 1) atexit 2) SetUnhandledExceptionFilter 我试过了:1)atexit 2)SetUnhandledExceptionFilter

I tried in debug run to simple close the application that runs, but it did not get to the point to perform the extra task in the end. 我尝试在调试运行中简单地关闭正在运行的应用程序,但最终并没有执行额外的任务。

Help, 救命,

This should answer your question: 这应该可以回答您的问题:

There are two ways to kill application in Task Manager. 在任务管理器中有两种杀死应用程序的方法。

Killing through Applications tab would roughly be equivalent of SIGTERM(in Linux). 通过“应用程序”选项卡杀死将大致等同于SIGTERM(在Linux中)。 Application may intercept it and do more processing, since it's basically sending a "close window" message. 应用程序可能会拦截它并进行更多处理,因为它基本上是在发送“关闭窗口”消息。 Message to catch is WM_CLOSE. 捕获的消息是WM_CLOSE。

Killing through Processes tab would roughly be equivalent of SIGKILL(in Linux). 通过“进程”选项卡杀死将大致等同于SIGKILL(在Linux中)。 There is nothing you can do to intercept that, short of monitoring user's actions in Task Manager's listbox and End Process button, or having a watchdog process that will see when the first one is killed 除了监视任务管理器的列表框和“结束进程”按钮中用户的操作,或者没有监视程序可以看到第一个进程被杀死时,您无法采取任何措施来拦截它

Enumerate all running processes. 枚举所有正在运行的进程。 Once you do that, you will get process IDs. 完成后,您将获得进程ID。 Obtain handle to each process through previously obtained ID and find the process you are looking for. 通过先前获得的ID获取每个进程的句柄,然后找到您要查找的进程。 Use that same process handle in separate thread like this: 在单独的线程中使用相同的进程句柄,如下所示:

WaitForSingleObject( hProcess, INFINITE ); WaitForSingleObject(hProcess,INFINITE);

Once the process is terminated, this thread will end. 一旦进程终止,该线程将结束。

In console apps you should consider installing a HandlerRoutine callback function to catch CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT. 在控制台应用程序中,您应该考虑安装HandlerRoutine回调函数以捕获CTRL_C_EVENT,CTRL_BREAK_EVENT,CTRL_CLOSE_EVENT,CTRL_LOGOFF_EVENT,CTRL_SHUTDOWN_EVENT。

In GUI apps you should consider handling WM_CLOSE , WM_QUERYENDSESSION , WM_ENDSESSION . 在GUI应用程序中,您应该考虑处理WM_CLOSEWM_QUERYENDSESSIONWM_ENDSESSION

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

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