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