Boris Raznikov
Boris Raznikov

Reputation: 2453

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,

Upvotes: 2

Views: 2234

Answers (3)

Alexey Frunze
Alexey Frunze

Reputation: 62106

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.

Upvotes: 1

user472155
user472155

Reputation:

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.

Upvotes: 1

6D65
6D65

Reputation: 795

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

Upvotes: 4

Related Questions