Reputation: 1476
Is there any event like in C# "FormClosing" but in C++ as Console closing where I can execute some code before the Console close? (In my case, I'd like to create a directory with the input of the user before the console is closed completely).
Upvotes: 8
Views: 1707
Reputation: 5660
My guess is that you want to get the Event when clicking the [X]
BOOL WINAPI HandlerRoutine( DWORD eventCode )
{
switch( eventCode )
{
case CTRL_CLOSE_EVENT:
// do your thing
return FALSE;
break;
}
return TRUE;
}
Is that what you're looking for?
You also need to enable the Handler:
int main()
{
SetConsoleCtrlHandler( HandlerRoutine , TRUE );
getch();
}
Upvotes: 14
Reputation: 2304
A closing console effectively kills your application. In windows I'm unsure of the ability to trap this, but in linux you could trap the KILL signal?
Upvotes: 0
Reputation: 26197
If you want to close the console, then you can use FreeConsole();
If you then further want to open the console again, you can use AllocConsole();
Upvotes: 0