c - 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
).
解决方案
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()
. CommandLineToArgvW
s 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.
推荐阅读
- python - 熊猫:如何生成新列以获取另一列的最后日期时间值
- javascript - 有没有办法使用空手道 DSL 获得 sessionStorage?
- xml - Powershell - XML - 如何从每个节点系列的不同深度提取多个值
- linux - 在周日某个时间之后和周五某个时间之前运行 cron 的干净方式
- excel - VBA使用文件中的参数列表查找/替换并在搜索字符串中包含空格
- c++ - std::function 模板参数推导
- javascript - 通过比较共享 value2 的元素的 value1 从数组中删除对象
- javascript - 是否可以在它所在的类之外的异步函数中传递数据?
- kotlin - 根据字段值反序列化为密封子类
- javascript - Onclick 在循环内单击所有元素