0

I apparently can detect my c++ program is closed by following code

static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType)
{
  switch (dwCtrlType)
  {
  case CTRL_C_EVENT: // Ctrl+C
   case CTRL_BREAK_EVENT: // Ctrl+Break
case CTRL_CLOSE_EVENT: //case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT: // User logs off.
 case CTRL_SHUTDOWN_EVENT: // System is shutting down. 
   } 
}

I want to detect and know whether my program is closed by unknown so that can run few last bits my code so smooth closing. .

KIm.K
  • 11
  • 1
  • 1
    What do you mean closed by unknown? Like if the user closes the command line? – NathanOliver Jun 13 '18 at 19:06
  • 4
    You asked this very question earlier today, except it specified Windows Task Manager. Your sample code even had the same odd indentation. Edit : If I recall correctly, it was closed as a duplicate of https://stackoverflow.com/questions/1527450/can-i-handle-the-killing-of-my-windows-process-through-the-task-manager – François Andrieux Jun 13 '18 at 19:10
  • There's a million ways a process can die. Which is "unknown" in your opinion? – tadman Jun 13 '18 at 19:10
  • You just want to intercept WM_CLOSE in your message loop and then check to see what is going on. – ss7 Jun 13 '18 at 19:15
  • 1
    @shenk A console program doesn't have a message loop. – Paul Sanders Jun 13 '18 at 19:29
  • If your program is running and I terminate the power (without pressing the power button), most likely this is an unknown. Windows has tried to be nice and and send closure messages to them; but people don't like waiting for processes to close and find other *unknown* methods to kill your process. – Thomas Matthews Jun 13 '18 at 19:33
  • unknown process killing like user closes using the command line, program self crash etc .. – KIm.K Jun 13 '18 at 19:37
  • @FrançoisAndrieux link that mentioned is not for console c++ program as A console program doesn't have a message loop so no wm_close – KIm.K Jun 13 '18 at 19:38
  • 1
    You can't detect EVERY possible way that a process can be terminated. You can only detect the more common "graceful" ways, like when the user presses Ctrl-C or Ctrl-Break, or closes the console window, or exits Windows, etc. – Remy Lebeau Jun 13 '18 at 20:34
  • @RemyLebeau ik, but how about command line kills and task manager kills – KIm.K Jun 14 '18 at 04:07
  • @KIm.K nope, can't detect those from inside the app that is being killed – Remy Lebeau Jun 14 '18 at 04:11
  • @RemyLebeau You can detect a kill from Task Manager in an app with a message loop (you get a `WM_QUIT` or something, I forget exactly what) but you don't get long to react to it. Shortly after that, the Task Manager terminates the process direct if it hasn't exited already. – Paul Sanders Jun 14 '18 at 10:59
  • OP, please to indent your code nicely, it only takes a moment. Thx. – Paul Sanders Jun 14 '18 at 11:01
  • 1
    @PaulSanders Only if Task Manager attempts a "graceful" close from the Applications tab, but not a brute force termination from the Processes tab. – Remy Lebeau Jun 14 '18 at 14:54
  • @RemyLebeau Yes, quite right. – Paul Sanders Jun 14 '18 at 15:39
  • 1
    We're programmers here, we can use the correct terminology. On Windows, `kill()` is not the correct terminology. The function is `TerminateProcess()`. Apart from being from the wrong OS, `kill` is unclear because the `kill()` function (and `kill` utility) can send any signal, not only `SIGKILL`, and most of the signals can be handled. There is no way for the target of a `TerminateProcess()` call to handle it. – Ben Voigt Jun 14 '18 at 16:05

1 Answers1

1

The whole idea of task-manger based kill and command-line kill is to kill the program as quickly and abruptly as possible! (It is to prevent virus and other dangerous program to stop immediately and give no time to react).

The only possible practical solution for your console program is to have another program running and monitoring your program, so when it closed abruptly, your second program shall take care of it and do the finishing work left.

(hardly, no user is gonna kill both programs at once)

mike
  • 88
  • 1
  • 9