首页 > 解决方案 > Get command line parameters as one large string

问题描述

I know that I can get the whole command line with

LPWSTR commandLine = GetCommandLineW();

and get all parts from it

int argv = 0;
LPWSTR* szArglist = CommandLineToArgvW(commandLine, &argv);

but when launching

$ myapp.exe arg1 arg2 arg3

this always contains the name of the executable itself (myapp.exe arg1 arg2 arg3).

How can I get just all parameters as one large string (arg1 arg2 arg3).

标签: cwinapi

解决方案


When you say "How can I get just all parameters as one large string", what do you actually mean?

If you want the exact parameters used to create the process you just call GetCommandLine and skip the first argument. If the string starts with a " you skip until you find the closing ", otherwise you skip until you find a space; while(*cmd > ' ') ++cmd; or something along those lines.

If you want the parameters after they have been processed you need to concatenate all the argv strings from 1 to argc -1. What processed means depends on your C run-time library if you get them from main(). CommandLineToArgvWs processing of quotes and backslashes is documented on MSDN. Because the parameters have been processed, you cannot use this concatenated string to launch other processes since required quotes may have been removed.


推荐阅读