How to dump game data before closing C++ Windows - c++

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.

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

Related

How to hide console window while opening hidden COM object using C++?

I am new to C++ so please be gentle. So i created a small C++ script which will be a part of a larger program. It creates an invisible window and navigates to home. While that part is done, it always creates a console window when it finishes execution and then in less than a second it vanishes. What do i need to change in order to make the program work in a way that the console window won't open ?
Instead of compiling as a console application, compile as a windows desktop project. Then convert main to be WinMain

click close console window to end a c++ console program is proper way?

I have just got this problem for a few days. Before, I've always thought that letting the program exit by returning from main and clicking close the console window is the same way to end the program.
However, I've found that they are different. Since my program opens a camera which is an object. And closing the console windows does not destroy or clean up the object. So the next time I have error to open the camera again
I just need a confirm if this is true?
Then why only until now I can see the problem?
Closing a console window in Windows, kills the running program (or stack of running programs). Unless it has registered a handler for this event, it gets no chance to clean up. If you want solution, register a handler.
Hm, consulting the documentation, wait a few secs…
OK, look up SetConsoleCtrlHandler.
Closing a running console application will kill the process, not giving you the chance for any clean up code. I guess you could hook a windows message loop to trap the WM_CLOSE message and do proper cleanup, but at the end of the day, you just shouldn't kill the process.

Keep C++ Program to be always running

I have C++ program written for Windows, which uses SendInput() to emulate keyboard keys. The program works and sends space key, which works fine in Icy Tower (Game) and the character keeps on jumping. However when I tried it with game like Counter Strike it didn't jump and when I tried pressing alt+tab to switch quickly between my program and the game I found the game to jump for once and the SendInput() makes it's way. So my guess is that when the program is in the background it doesn't send the keys to the game, is there a way to get around this?

Application not starting until form is displayed to user for the first time

This is a weird question. I have written a .NET application that starts a process. This process is a MFC application written in c++. For some reason the process does not start doing anything until the form is displayed to the user for the first time. For example, if the process starts minimized I have to un-minimize it (click on it) before it will start doing whatever it's supposed to do. Also, if my application is running and starting this process while the screen is locked the process behaves the same as if it's minimized. It doesn't start doing anything until I unlock the screen and it is displayed to the user for the first time. Like I said, this is a weird question so I hope I'm conveying the problem properly.
Sounds like your functionality is embedded in MFC Windows' load event. If you want the application to be more reactive, move that code to your application class.

Add console to existing MFC application

I'm working with 2 friends in a class project to make a D&D game. so far for the assignments I've been doing character creation stuff and strutting on the command line.
Now we're bringing or part together and I need to output ny dice rolls on a console and a few things on another one that will have to become the main view or tab or whatever it's called when it requires input/attention.
Problem is I never learned MFC yet because I didn't need it. How hard would it be to make a sample MFC console all that I can give to the teammate in charge of the GUI?
Could anyone point me to some instruction on making a console for an MFC app and how to give it output and receive output?
First, you can't. for both Unix/Linux and Windows, there is a one console/process limit. If you want another console, you have to create another process, that writes and reads the other console, while you send and receive the data.
You can use a NamedPipe http://msdn.microsoft.com/en-us/library/windows/desktop/aa365590%28v=vs.85%29.aspx to send data between processes, and the CreateProcess() function lets you create a process with a separate Console window.
Alternatively you can just write a Console-Look-a-Like window in some GUI.