SetConsoleCtrlHandler routine issue SetConsoleCtrlHandler routine issue windows windows

SetConsoleCtrlHandler routine issue


It looks like you can no longer ignore close requests on Windows 7.

You do get the CTRL_CLOSE_EVENT event though, and from that moment on, you get 10 seconds to do whatever you need to do before it auto-closes. So you can either do whatever work you need to do in the handler or set a global flag.

case CTRL_CLOSE_EVENT: // CTRL-CLOSE: confirm that the user wants to exit.                       close_flag = 1;                       while(close_flag != 2)                         Sleep(100);                       return TRUE;

Fun fact: While the code in your CTRL_CLOSE_EVENT event runs, the main program keeps on running. So you'll be able to check for the flag and do a 'close_flag = 2;' somewhere. But remember, you only have 10 seconds. (So keep in mind you don't want to hang up your main program flow waiting on keyboard input for example.)


I suspect that this is by-design on Windows 7 - if the user wants to quit your application, you're not allowed to tell him "No".


There is no need to wait for any flag from the main thread, the handler terminates as soon as the main thread exits (or after 10s).

BOOL WINAPI ConsoleHandler(DWORD dwType){    switch(dwType) {    case CTRL_CLOSE_EVENT:    case CTRL_LOGOFF_EVENT:    case CTRL_SHUTDOWN_EVENT:      set_done();//signal the main thread to terminate      //Returning would make the process exit!      //We just make the handler sleep until the main thread exits,      //or until the maximum execution time for this handler is reached.      Sleep(10000);      return TRUE;    default:      break;    }    return FALSE;}