   [Solution] Getting the Console Window to stay behind (crash-proof)
  Hi,I was gonna post this as a question, but then I kinda figured out a decent enough solution on my own.The task is to have a Windows Console application to leave its console window after it quits, saying "Press any key to continue . . ." with the following requirement: it should be crash-proof.The most common way AFAIK for doing this is simply putting a system("pause"); or something equivalent in functionality at the end of your program. The main problem with that is if your program crashes, that console window disappears.I basically copied the way VC++ Express does it when you press Ctrl+F5 (run without debugging), which I found out by using Process Explorer. They seem to run a cmd.exe batch script that executes your program, and _then_ executes pause.Originally I had a run_my_game.bat file along with my_game.exe file, which felt kinda confusing (Do I run the .exe or the .bat). I wanted to get rid of the .bat file (which is just my_game.exe and pause), while keeping it's crash-proofness.Here's the solution I came up with...int main(int argc, char * argv[]){// This should be at the very top of main()... before everything else.// Re-run the exe in a cmd environment, so that the console window won't get closed right away// NOTE: At this time, all command lines parameters get strippedchar * runcode = "-superdupercodethatallowsyourprogramtorunforreal";if (2 == argc && 0 == strcmp(argv[1], runcode))argc = 1;// Ignore the runcode parameterelse {std::string cmd; cmd = cmd + "\"" + argv[0] + "\" " + runcode + " & echo. & echo Feel free to say anything here if you want. & pause";system(cmd.c_str());return 0;}// Your program// ...}This way you only have one .exe file on disk.Hope that helps if you want to make your console window stay.The only downsides of this approach I can think of is that there will be two instances of your .exe as running processes (one dummy launcher, one real) instead of just one. I don't think you can do this without the additional cmd.exe process (since even VC++ uses it). The code can be extended not to strip the original command line params.[Edited by - shurcool on May 19, 2010 9:52:06 AM]
  Just a heads up.I also think that your code creates two running processes of your exe.If your exe is big, doing a lot of static initiations (locking files maybe?) this could be a potential problem.A "fix" for this is to use CreateProcess instead of system to run the second instance of your exe since that could run be made to just start your process instead of start and wait for it to finish (like system does).Maybe it's possible to still use system but add "start" with the correct command line switches?Cheers,Eq;Quote:Original post by eqI also think that your code creates two running processes of your exe.If your exe is big, doing a lot of static initiations (locking files maybe?) this could be a potential problem.A "fix" for this is to use CreateProcess instead of system to run the second instance of your exe since that could run be made to just start your process instead of start and wait for it to finish (like system does).Yep, you're absolutely right. I've already considered that, but the reason I didn't use CreateProcess is because the code for it is quite a bit more verbose.If this extra memory usage is a problem for someone, they can use CreateProcess instead. But I don't think any really big project is going to use a console window anyway... Although, another negative point of two .exe processes is that both will show up in Task Manager, so user won't know which to kill if they wanna do that (plus they'll be confused to see two .exes).Quote:Maybe it's possible to still use system but add "start" with the correct command line switches?Thanks for the suggestion! I got excited at first, because I wasn't aware of this command and it's almost exactly what I was looking for. But I don't think I can use it because it only allows one command to be run in background. But I need two (your .exe & pause).   ;  You could use the set_terminate() to call a function if your program throws an exception. Then just point it to a function that calls system("pause").   ;Quote:Original post by krezYou could use the set_terminate() to call a function if your program throws an exception. Then just point it to a function that calls system("pause").Thanks for the tip! :)Unfortunately, that approach seems to share a similar downfall of the atexit() way: it is not completely crash-proof.   