Closing a c++ console app with the “x” in the top corner throws an CTRL_CLOSE_EVENT
which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function. In there you could override the close functionality and perform whatever you wished to do, and then optionally still perform the default behavior.