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.
Asked
Active
Viewed 202 times
1
-
The game is a console application? – Robert Harvey Jan 16 '14 at 20:51
-
2@RobertHarvey, totally possible for beginners! Also: http://stackoverflow.com/questions/696117/what-happens-when-you-close-a-c-console-application – Moo-Juice Jan 16 '14 at 20:52
-
Why not use auto save features periodically? – camdixon Jan 16 '14 at 20:52
-
@RobertHarvey Yes this is console – Evanias Jan 16 '14 at 20:53
-
@camdixon I could, but I am trying to make this program as efficient as possible, and auto saves aren't necessary if I can figure this out. However, I may have to go with auto saves if this fails. – Evanias Jan 16 '14 at 20:55
1 Answers
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.

Community
- 1
- 1

Robert Harvey
- 178,213
- 47
- 333
- 501
-
I will try to research this... don't know how to use it. Time to go googling! Thanks though. – Evanias Jan 16 '14 at 20:58