2

I am using Visual Studio 2012. I am using system function to run an exe. If this exe crashes, it pops up the dialog box saying the program needed to close. But it freezes the calling program until I close the box. Is there anyway for the calling program to exit the exe and continue? It only needs to work for Windows.

I am calling like this

int ret = system((prog + "> log.txt 2>&1").c_str());

The extra is to output std out and err to a log file.

Strangly, if I remove that part it returns control just fine.

int ret = system((prog).c_str());

Any ideas?? Thanks

Edit: OK if I do this it returns control

int ret = system((prog + "> log.txt").c_str());

I got the extra characters from this website:

http://www.robvanderwoude.com/redirection.php

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • What do you want `ret` to be when the process hasn't exited yet? –  May 20 '13 at 11:35
  • If it crashes, ret can be anything other than 0. I just check for non-0, to write in an error log. – Neil Kirk May 20 '13 at 11:36
  • But that's the point I'm trying to make: it hasn't crashed yet, not until the user clicks "Close". It is at the point where you get that dialog box still possible to attach a debugger to the process, fix whatever is wrong, and let it continue and finish successfully. –  May 20 '13 at 11:38

1 Answers1

1

You need to modify the error mode of the other process. If you control the code of the other process, call SetErrorMode and adding the SEM_FAILCRITICALERRORS option to the error mode. You should make this call as soon as the process begins execution.

The documentation on MSDN even discusses the very issue that you have encountered:

Best practice is that all applications call the process-wide SetErrorMode function with a parameter of SEM_FAILCRITICALERRORS at startup. This is to prevent error mode dialogs from hanging the application.

If you cannot control the code in the other process, then you can set the error mode in your process. That will have the desired effect because a child process inherits the error mode of its parent process.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490