拆分可执行路径和参数 [英] Split executable path and arguments
本文介绍了拆分可执行路径和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要能够在命令中拆分可执行路径和参数.
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.exe" "C:\test 文件夹\versioninfo.txt"
"C:\Windows\notepad.exe" "C:\test folder\versioninfo.txt"
rundll "C\Windows\somelibrary.dll"
rundll "C\Windows\somelibrary.dll"
有人有一段代码来解析这样的字符串吗?
Anyone has a piece of code to parse such strings?
谢谢.
推荐答案
我以前用过这样的:
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
这篇关于拆分可执行路径和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文