简体   繁体   中英

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). 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

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). Application may intercept it and do more processing, since it's basically sending a "close window" message. Message to catch is WM_CLOSE.

Killing through Processes tab would roughly be equivalent of SIGKILL(in 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. Obtain handle to each process through previously obtained ID and find the process you are looking for. Use that same process handle in separate thread like this:

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.

In GUI apps you should consider handling WM_CLOSE , WM_QUERYENDSESSION , WM_ENDSESSION .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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