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

时间:2015-08-05 16:52:21

标签: c++ c windows winapi createprocess

让我想编写一个启动另一个应用程序的应用程序。像这样:

# 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

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

有一个GetCommandLine函数,但我无法使用它,因为我从Linux移植代码并绑定到argc/argv(否则,它对我来说是一个非常难看的黑客)

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

基本上,我希望与CommandLineToArgvW相反。是否有标准方法来执行此操作?

4 个答案:

答案 0 :(得分:6)

关于如何引用争论的最终答案在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  \t\n\v\", 
        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,请参阅文章(它有所不同)。

我认为Microsoft C运行时库没有这样做的功能,这很疯狂。

答案 1 :(得分:3)

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

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

Parsing C++ Command-Line Arguments

  

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

     
      
  • 参数由空格分隔,空格或空格   标签

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

  •   
  • 用双引号括起来的字符串("字符串")是   无论是否包含空格,都将其解释为单个参数   内。带引号的字符串可以嵌入参数中。

  •   
  • 解释以反斜杠(\")开头的双引号   作为文字双引号字符(")。

  •   
  • 反斜杠按字面解释,除非它们立即解释   在双引号之前。

  •   
  • 如果偶数个反斜杠后跟双引号   mark,每个对的argv数组都放置一个反斜杠   反斜杠,双引号被解释为字符串   分隔符。

  •   
  • 如果奇数个反斜杠后跟双引号   mark,每个对的argv数组都放置一个反斜杠   反斜杠,双引号是"转义"通过   保留反斜杠,导致文字双引号(")   放在argv。

  •   

编写一个带有字符串数组并将它们连接在一起的函数应该不难,将上述规则的反向应用于数组中的每个字符串。

答案 2 :(得分:2)

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

假设要创建的程序名称是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 nd 参数
CreateProcess(NULL, command, ...);

答案 3 :(得分:0)

如果符合您的需要,您可以查看以下代码,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;
            }