Sign in to follow this  

How can I restart my c++ application?

This topic is 2662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all.

I've got a game with an inbuilt auto updater and I'd like it to restart upon completion. Currently when they close the game they have to manually reopen it again. Is it possible to perhaps call a batch file from within my application that closes and reopens it again?

This is just for Windows atm, but eventually it'd be nice to have on linux and mac too.

Cheers,
Ben.

Share this post


Link to post
Share on other sites
Well, for a Windows-only solution, assuming you knew the applications window handle, you could use SendMessage() to pass it the WM_CLOSE message. Of course, if your application has unsaved data, you could lose it or have it wait on a "Do you want to save your changes?" dialog. You could also register your own WM_USER message, which would automatically save the changes and then post the WM_CLOSE message. At that point, you can use several ways to launch the application. Two of which are, CreateProcess() and ShellExecuteEx().

These are supported back to Windows 2000/XP, unlike the Restart Manager, which seems like it locks you into Vista or newer.

Share this post


Link to post
Share on other sites
Another way not yet mentioned would be to have the file that people run NOT be your game, but have it be a thin shell that is in charge of running your game (ie it launches your game).

The benefit in this is that your game can tell the program running it "hey, i need to restart, so start me up again after i shutdown".

Then your game closes as it would normally, and your other program that ran it would launch it up again.

Under normal circumstances, when your game ended, this shell program would just shut down as well.

for the record by shell program i dont mean a console application, i mean a program that has no visual display which launches the actual game after it is launched.

HTH!

Share this post


Link to post
Share on other sites
Cheers guys. Yeah I can already close down the application from within the application. And I know I can open up the the application simply with a batch file that says something like:

start MyGame
exit

So all I really need to be able to do is call a batch file from a C++ application and it's all done. I'd just call this batch file as the very last line of main like:


int main() {
//...blah blah program loop

//then when we're about to close..
if(needToRestart) CallBatchFile("./restart.cmd"); //BAM!
}



I THINK that should work? Right? Only problem is I don't know HOW to call a batch file from within a C++ application.

Share this post


Link to post
Share on other sites
your plan to call a batch file won't work because the program won't terminate until the batch file does.

the batch file won't terminate until the program it launches terminates.

If you go with your batch file approach it will just spawn another copy of your game but not kill your old one.

re-read my previous post... :P

Share this post


Link to post
Share on other sites
Maybe I'm wrong, but I just tested it and it seems that if you add 'start' before the application name then the batch file can move onto the next line (being exit).

So if I put 'start' into my restart.cmd file as such:


start MyGame
exit



Then the batch file will start up another instance of my game and then immediately close, allowing the first one to close immediately afterwards.

Your suggestion is probably cleaner though. Could that shell be a simple batch file as well?

Share this post


Link to post
Share on other sites
Hey all. I've got it working. Here's how for all interested. First I created a batch file which very simply says:


start MyGame
exit




I then named this file "Restart MyGame.cmd" and saved it to the same directory as "MyGame.exe". Then at the very end of my main function I simply added a single line that says:


int main() {
//...blah blah program loop. Somewhere in here determined if needToRestart = true

if(needToRestart) system("start Restart MyGame.cmd");
}



And that's it! I've checked the task manager to make sure there aren't any invisible processes starting up without closing, and it's all good.

Cheers all :)

Share this post


Link to post
Share on other sites
Since you're auto-updating your executable, take care that maybe your app's folder is not writeable to (if you did a proper install how you should it isn't).

It might be a good idea to write the batch file to the temp folder. Doesn't solve the problem if you want to write the new executable in the app folder without admin rights though. Is the updater elevating?

Share this post


Link to post
Share on other sites
One suggestion is to start a separate application handling the update.
That is, start the an update application from your application, and close down your app.
Let the update application wait for your app to close down before proceeding with the update.
Once the update is done, start your application and close down the update application.

Share this post


Link to post
Share on other sites
Running through a launcher that has elevated privileges would probably be the most robust solution, since it can perfectly manage the lifetime of the game, handle the patching and updating, and respond to and report crashes. In a live environment you can even have it attach itself as a debugger to the game (and vice versa) to make sure users don't have that option.

Share this post


Link to post
Share on other sites

This topic is 2662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this