How do I convert argv to lpCommandLine parameter of CreateProcess?

后端 未结 4 625
深忆病人
深忆病人 2021-01-29 14:37

Let I want to write an application, that launches another application. Like this:

# This will launch another_app.exe
my_app.exe another_app.exe 
# This will laun         


        
4条回答
  •  北海茫月
    2021-01-29 15:03

    The definitive answer on how to quote arguments is on Daniel Colascione's blog:

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

    I am reluctant to quote the code here because I don't know the license. The basic idea is:

    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
    

    If you need to pass the command line to cmd.exe, see the article (it's different).

    I think it is crazy that the Microsoft C runtime library doesn't have a function to do this.

提交回复
热议问题