I am trying to catch the event closing console window of a console application written in C++. From the other post I made last night and some google, I got down to using
BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
char mesg[128];
switch(CEvent)
{
case CTRL_C_EVENT:
Beep( 750, 300 );
escape = 1;
MessageBox(NULL,
"CTRL+C received!","CEvent",MB_OK);
break;
case CTRL_BREAK_EVENT:
//MessageBox(NULL,
// "CTRL+BREAK received!","CEvent",MB_OK);
break;
case CTRL_CLOSE_EVENT:
escape = 1;
//MessageBox(NULL,
// "Program being closed!","CEvent",MB_OK);
break;
case CTRL_LOGOFF_EVENT:
MessageBox(NULL,
"User is logging off!","CEvent",MB_OK);
break;
case CTRL_SHUTDOWN_EVENT:
MessageBox(NULL,
"User is logging off!","CEvent",MB_OK);
break;
}
return FALSE; // TRUE: process terminated; FALSE: process passed to next handler or if no else process is terminated
}
The original purpose of this event catching is to force the program to return to main and finish there after user click [x] to close console window.
It's interesting though the handler is always called but the program does not return to main sometimes, it's sometimes yes and others no. Especially, when I don't use "MessageBox" in event handler. It most likely not to return to main
Am I doing the right way to force the so-called program cursor to return to main?
Edit 01:
It's can be tested by putting a breakpoint at "return" keyword of main thread and the event handler which shows clear that event's "return" is reached everytime but never does for the main thread. And the output also shows the order of program exited:
The thread 'Main Thread' (0x1524) has exited with code -1073741510 (0xc000013a).
The program '[5300] eventhandler.exe: Native' has exited with code -1073741510 (0xc000013a).
This raise another question I might put in another post I also edit to show I put return in the handler
User contributions licensed under CC BY-SA 3.0