My C Win32应用程序应允许传递完整的命令行以便其他程序启动,例如,将argv []传递给CreateProcess()的方法
myapp.exe /foo /bar "C:\Program Files\Some\App.exe" arg1 "arg 2"
myapp.exe
可能看起来像
int main(int argc, char**argv)
{
int i;
for (i=1; i<argc; ++i) {
if (!strcmp(argv[i], "/foo") {
// handle /foo
} else if (!strcmp(argv[i], "/bar") {
// handle /bar
} else {
// not an option => start of a child command line
break;
}
}
// run the command
STARTUPINFO si;
PROCESS_INFORMATION pi;
// customize the above...
// I want this, but there is no such API! :(
CreateProcessFromArgv(argv+i, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
// use startup info si for some operations on a process
// ...
}
我能想到的一些解决方法:
- 使用
GetCommandLine()
,并找到对应的argv [I] - 写的东西串类似于
ArgvToCommandLine()
也在another SO question中提及
它们都很长并且重新实现繁琐的Windows命令行解析逻辑,这已经是CommandLineToArgvW()
的一部分。
有没有针对我的问题的“标准”解决方案?标准(Win32,CRT等)解决方案的实现算作解决方案。
'的strstr()'样部分是有问题的。这个用例怎么样:'myapp.exe/SetTitle C:\ app.exe/RunMinimized C:\ app.exe/ArgToApp.exe'。实际的命令从第二场比赛开始。 – 2010-10-20 22:13:53
@Ilia我看到了,在这种情况下,你应该自己解析它。无论如何,不要在速度上放肆。 – ruslik 2010-10-20 22:25:58