分割可执行文件路径和参数可执行文件、路径、参数

2023-09-04 05:09:21 作者:ミ﹏蕞蒗漫の副歌

我需要能够分裂的命令的可执行文件路径和参数。

I need to be able to to split the executable path and arguments in a command.

Windows处理易如下:

Windows handles the following easily:

的notepad.exe C:\ TESTFILE.TXT

"notepad.exe C:\testfile.txt"

记事本C:\ testfolder \ versioninfo.txt

"notepad c:\testfolder\versioninfo.txt"

C:\ WINDOWS \ NOTEPAD.EXEC:\ test文件夹\ versioninfo.txt

"C:\Windows\notepad.exe" "C:\test folder\versioninfo.txt"

RUNDLLC:\的Windows \ somelibrary.dll

rundll "C\Windows\somelibrary.dll"

任何人有一张code解析这些字符串?

Anyone has a piece of code to parse such strings?

感谢。

推荐答案

我用这样的事情在过去的:

I've used something like this in the past:

char* lpCmdLine = ...;
char* lpArgs = lpCmdLine;
// skip leading spaces
while(isspace(*lpArgs))
    lpArgs++;
if(*lpArgs == '\"')
{
    // executable is quoted; skip to first space after matching quote
    lpArgs++;
    int quotes = 1;
    while(*lpArgs)
    {
        if(isspace(*lpArgs) && !quotes)
            break;
        if(*lpArgs == '\"')
            quotes = !quotes;
    }
}
else
{
    // executable is not quoted; skip to first space
    while(*lpArgs && !isspace(*lpArgs))
        lpArgs++;
}
// TODO: skip any spaces before the first arg
 
精彩推荐
图片推荐