1

I am working on a project in which I put my data (I.E. Game world, mobs) into text files, which are read in when I run the game. This works perfectly fine. The game saves the data when I hit a key that exits the game loop, basically saving and then closing the game. However, I habitually hit the X on the top right of the console, and this obviously causes the game to close without saving. My question is, is there any way to run a function when somebody hits the close button on the console, and then close the program? I am working on Windows XP, C++, Console Program.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Evanias
  • 27
  • 11

1 Answers1

4

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.

What happens when you close a c++ console application

Community
  • 1
  • 1
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501