Home > Net >  Passing arguments to child boost::process
Passing arguments to child boost::process

Time:05-18

void mainParent()
{
    string str = ".\\childProcess.exe";
    boost::process::child c(str,bp::args({stringArg}) );
    c.wait();
}
int mainChild(int argc, const char* argv[]) 
{
    cout << "test == " << argv[0] << endl;
}

string stringArg ="text";

I tried: boost::process::child c(str,bp::args({stringArg}) );

but cout << "test == " << argv[0] << endl; outputs its own path to the exe instead of the text I want.

CodePudding user response:

but cout << "test == " << argv[0] << endl; outputs its own path to the exe instead of the text I want

As it should be, because argv[0] is supposed to hold the path to the exe file. The 1st command-line parameter will be in argv[1] instead, and the 2nd parameter will be in argv[2], and so on. Use argc to know how many strings are actually in argv[], eg:

int mainChild(int argc, const char* argv[]) 
{
    for(int i = 0; i < argc;   i) {
        cout << "argv[" << i << "] = " << argv[i] << endl;
    }
}
  • Related