我使用 CreateProcess 为 Win32 编写了子命令执行代码:
CreateProcessW(NULL, // app
&commandW[0], // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&startup_info, // STARTUPINFOW pointer
&process_info); // receives PROCESS_INFORMATION
这使用了一个“技巧”并将整个命令放在第二个参数中,该参数通常只包含传递给程序的参数。我想为 POSIX 做同样的事情,但同样的“技巧”不适用于该execlp
功能(我也打赌其他所有功能)。有没有办法仍然使用一个单一的std::string command
并开始一个过程?将命令拆分为可执行文件名和参数是可能的,但如果可能的话,我想避免很多工作。