views:

591

answers:

2

How do you run an executable with parameters passed on it from a C++ program and how do you get the return value from it?

Something like this: c:\myprogram.exe -v

+9  A: 

Portable way:

 int retCode = system("prog.exe arg1 arg2 arg3");

With embedded quotes/spaces:

 int retCode = system("prog.exe \"arg 1\" arg2 arg3");
Chris Kaminski
how do you handle parameters with spaces in them? (e.g. "arg 1", "arg 2")
Bill
+3  A: 

On Windows, if you want a bit more control over the process, you can use CreateProcess to spawn the process, WaitForSingleObject to wait for it to exit, and GetExitCodeProcess to get the return code.

This technique allows you to control the child process's input and output, its environment, and a few other bits and pieces about how it runs.

Martin