我如何将argv转换为CreateProcess的lpCommandLine参数

How do I convert argv to lpCommandLine parameter of CreateProcess?

本文关键字:CreateProcess lpCommandLine 参数 转换 argv      更新时间:2023-10-16

让我写一个应用程序,它启动另一个应用程序。这样的:

# This will launch another_app.exe
my_app.exe another_app.exe 
# This will launch another_app.exe with arg1, arg and arg3 arguments
my_app.exe another_app.exe arg1 arg2 arg3

这里的问题是我在main函数中得到char* argv[],但我需要将其合并到LPTSTR以便将其传递给CreateProcess

有一个GetCommandLine函数,但我不能使用它,因为我是从Linux移植代码并绑定到argc/argv(否则,它对我来说是一个非常丑陋的hack)。

我不能轻易地手工合并参数,因为argv[i]可能包含空格。

基本上,我想要CommandLineToArgvW的反转。是否有标准的方法来做到这一点?

关于如何引用参数的确切答案在Daniel Colascione的博客上:

https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/

我不愿意在这里引用代码,因为我不知道许可证。基本思想是:

for each single argument:
    if it does not contain  tnv", 
        just use as is
    else
        output "
        for each character
            backslashes = 0
            if character is backslash
                count how many successive backslashes there are
            fi
            if eow
                output the backslashs doubled
                break
            else if char is "
                output the backslashs doubled
                output "
            else
                output the backslashes (*not* doubled)
                output character
            fi
        rof
        output "
    fi // needs quoting
rof // each argument

如果您需要将命令行传递给cmd.exe,请参阅文章(它是不同的)。

我认为这是疯狂的,微软C运行时库没有一个函数来做这个

没有与CommandLineToArgvW()相反的Win32 API。您必须自己格式化命令行字符串。这只不过是基本的字符串连接。

微软记录了命令行参数的格式(或者至少是vc++编写的应用程序所期望的格式):

解析c++命令行参数

Microsoft C/c++启动代码使用以下规则解释操作系统命令行上给出的参数:

  • 参数用空格分隔,空格可以是空格或选项卡。

  • 插入字符(^)不被识别为转义字符或分隔符。该字符完全由命令行处理在被传递给argv数组之前,在操作系统中调用解析器

  • 用双引号("string")括起来的字符串是解释为单个参数,而不管是否包含空白内部。带引号的字符串可以嵌入参数中。

  • 解释前面有反斜杠(")的双引号作为双引号字符(")

  • 反斜杠按字面意思解释,除非它们立即出现

  • 偶数反斜杠后跟双引号注意,argv数组中每一对反斜杠和双引号被解释为字符串分隔符。

  • 如果奇数个反斜杠后跟双引号注意,argv数组中每一对反斜杠,双引号被剩余的反斜杠,导致字面双引号(")为

对于您来说,编写一个函数应该不难,该函数接受字符串数组并将它们连接在一起,将上述规则的反转应用于数组中的每个字符串。

您需要重新创建命令行,注意将所有程序名称和参数包含在"中。这是通过将"连接到这些字符串来完成的,一个在开头,一个在结尾。

假设要创建的程序名称为argv[1],第一个参数为argv[2]等…

char command[1024]; // size to be adjusted
int i;
for (*command=0, i=1 ; i<argc ; i++) {
   if (i > 1) strcat(command, " ");
   strcat(command, """);
   strcat(command, argv[i]);
   strcat(command, """);
}
使用CreateProcess 的2参数
CreateProcess(NULL, command, ...);

您可以查看下面的代码,如果它适合您的需要,txt数组sz可以用作字符串指针。我已经添加了对Unicode和MBCS的代码支持,

            #include <string>
            #include <vector>
            #ifdef _UNICODE
                #define String std::wstring
            #else
                #define String std::string
            #endif 
            int _tmain(int argc, _TCHAR* argv[])
            {
                TCHAR sz[1024] = {0};
                std::vector<String> allArgs(argv, argv + argc);
                for(unsigned i=1; i < allArgs.size(); i++)
                {
                    TCHAR* ptr = (TCHAR*)allArgs[i].c_str();
                    _stprintf_s(sz, sizeof(sz), _T("%s %s"), sz, ptr);
                }
                return 0;
            }