Detecting shut-down in a console...

closed account (L3ApX9L8)
So yea, am aware this question probably get asked a ton, but im also aware each time its answered in a pretty vague way (at least the answer i've been able to find)

So... is there a way?
There has to... right?
..
...

right? =s
Check out the atexit() function to tell you that your program is terminating.
http://www.cplusplus.com/reference/clibrary/cstdlib/atexit/


If you are trying to catch attempts to terminate your program on Windows, take a look at SetConsoleCtrlHandler().
http://www.google.com/search?btnI=1&q=msdn+SetConsoleCtrlHandler

Keep in mind that you cannot stop the console from closing -- only the user can do that by responding negatively to the Windows "End Program" dialogue that will pop-up.

I find this function only useful with a GUI program with an attached console (see AllocConsole()), since the user can be warned that attempts to close the console will summarily terminate the program. My control handler will typically look something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
BOOL WINAPI HandlerRoutine( DWORD dwCtrlType )
  {
  #ifdef __cplusplus
  cout <<
  #else
  printf( "%s",
  #endif

    "\n"
    "+-----------------------------------------------+\n"
    "|  HEY YOU THERE! CLOSING ME ENDS THE PROGRAM!  |\n"
    "+-----------------------------------------------+\n\n"

    "To make this window disappear, select\n"
    "File -> Hide Console from the main window''s menu.\n"

  #ifndef __cplusplus
    )
  #endif
  ;

  return dwCtrlType == CTRL_CLOSE_EVENT
  }

Note that there is an officially confirmed bug under Windows 9x where the handler is not called for CTRL_CLOSE_EVENT signals, so you may be out of luck if the user presses the little X on that obnoxious black window that pops-up with your GUI program while holding down the Ctrl key...

I don't know about this stuff on Linux. It really isn't necessary in that environment...

Hope this answered your question.
closed account (L3ApX9L8)
Indeed it did,
thanks again Duoas! ^^
Topic archived. No new replies allowed.