将参数传递给 child boost::process

Passing arguments to child boost::process

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";

我试过了:boost::process::child c(str,bp::args({stringArg}) );

但是 cout << "test == " << argv[0] << endl; 输出它自己的 exe 路径而不是我想要的文本。

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

因为它应该是,因为 argv[0] 应该保存 exe 文件的路径。第一个 command-line 参数将在 argv[1] 中,第二个参数将在 argv[2] 中,依此类推。使用argc可以知道argv[]中实际有多少个字符串,eg:

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