Is there an event like "Console Close" in C ++?

Is there any event, for example, in C # "FormClosing", but in C ++, when the Console closes, where can I execute some code before closing the console? (In my case, I would like to create a directory with user login before the console is completely closed).

+7
c ++ windows console
source share
3 answers

I assume that you want to receive the event when the [X] button is pressed

BOOL WINAPI HandlerRoutine( DWORD eventCode ) { switch( eventCode ) { case CTRL_CLOSE_EVENT: // do your thing return FALSE; break; } return TRUE; } 

What are you looking for?

You also need to enable the handler:

 int main() { SetConsoleCtrlHandler( HandlerRoutine , TRUE ); getch(); } 

More details

+11
source share

If you want to close the console, you can use FreeConsole();

If you want to open the console again, you can use AllocConsole();

0
source share

The closing console effectively kills your application. On Windows, I'm not sure about the ability to capture this, but on Linux can you capture the KILL signal?

0
source share

All Articles