Search code examples
c++shellbatch-filereturn-code

How to get the return code from a batch/shell script that launched from C++ code


We have a C++ program, sometimes this program need to execute a user defined batch/shell/ant script. We are not able to control how this script runs. Is there a way to get the return code from C++ program?

Something like: exec a script.sh > status.tmp ?

We need to support both Windows and Linux.

Any ideas?


Solution

  • in linux just use

    int ret=system("myshellscrtipt.sh");
    

    since the return value of the script is the return value of the system function. In Windows I'dont't know wether there is a similar function. If you used the Qt toolkit you could do something like this

    QProcess process;

    process.start( "yourShellCommand", QStringList( args );

    and this would be really cross-platform..