c++ - 如何获取原始命令行参数

标签 c++ c windows

我需要将命令行参数从 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/

相关文章:

c++ - Eigen3 Tensor 类中所有维度的缩减评估

c++ - minizip append 到文件

c++ - 快速排除成员是否在集合中

java - 如何在非英语系统中初始化 Gradle 项目?

windows - 用于在 Windows 中创建和运行 Shell 脚本的 Cygwin 包

c++ - uint64 标志或 uint32 标志[2] 用于 32 位编译器的函数参数?

c++ - 生成随机掷骰子并测量结果的频率

c 按值传递数组?

c - 我不知道在 C 中使用冒号

windows - 从命令提示符更改快捷方式的目标