Search code examples
c++windowsconsole-applicationtermination

Call function right before termination c++


Is there a way to call a function at termination(clicking on the red X of the console) of an non-Object-oriented console application(simple main program)?

Maybe to catch the message of the red X and handle it then...

Thanks!


Solution

  • When you are on Windows and have a console application running inside the normal console window, and then click on the Close Button ([X]) this will generate a CTRL_CLOSE_EVENT.

    You can handle these by installing a Console Event Handler with the SetConsoleCtrlHandler Win32 API function.