我需要将命令行参数从 A.exe 传递到 B.exe。如果 A.exe 带有多参数,例如
A.exe -a="a"-b="b"'
我可以用
BeginProcess("B.exe", **args!**)
启动 B.exe。 我怎样才能得到像
这样的原始命令行参数'-a="a"-b="b"'
最佳答案
如果您使用的是 Windows,则使用 GetCommandLine获取原始命令行。
请注意,GetCommandLine 还包括 argv[0]。因此,在将 GetCommandLine 的输出传递给 B 之前,您必须超越 argv[0]。
这是一些无错误检查的代码来做到这一点
#include <string.h>
#include <windows.h>
#include <iostream>
#include <ctype.h>
int main(int argc, char **argv)
{
LPTSTR cmd = GetCommandLine();
int l = strlen(argv[0]);
if(cmd == strstr(cmd, argv[0]))
{
cmd = cmd + l;
while(*cmd && isspace(*cmd))
++cmd;
}
std::cout<<"Command Line is : "<<cmd;
}
当我以 A.exe -a="a"-b="b"
运行上述程序时,我得到以下输出
A.exe -a="a" -b="b"
Command Line is : -a="a" -b="b"
关于c++ - 如何获取原始命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14150374/