In a Windows program, what is the canonical way to parse the command line obtained from GetCommandLine into multiple arguments, similar to the argv array in Unix? It seems that CommandLineToArgvW does this for a Unicode command line, but I can't find a non-Unicode equivalent. Should I be using Unicode or not? If not, how do I parse the command line?
相关问题
- Correctly parse PDF paragraphs with Python
- the application was unable to start correctly 0xc0
- R: eval(parse()) error message: cannot ope
- Handle button click in another application
- win32 Python - pythoncom error - ImportError: No m
相关文章
- How do I get from a type to the TryParse method?
- Why windows 64 still makes use of user32.dll etc?
- Can WM_NEXTDLGCTL be used with non-dialog windows?
- Windows EventLog: How fast are operations with it?
- Slow ANTLR4 generated Parser in Python, but fast i
- Are resource files compiled as UNICODE or ANSI cod
- Parsing JSON in QML [duplicate]
- user32 and kernel method list for C# [closed]
Apparently you can use __argv outside main() to access the pre-parsed argument vector...
Here is an implementation of CommandLineToArgvA that delegate the work to CommandLineToArgvW, MultiByteToWideChar and WideCharToMultiByte.
CommandLineToArgvW()
is in shell32.dll. I'd guessthat the Shell developers created the function for their own use, and it was made public either because someone decided that 3rd party devs would find it useful or because some court action made them do it.Since the Shell developers only ever needed a Unicode version that's all they ever wrote. It would be fairly simple to write an ANSI wrapper for the function that converts the ANSI to Unicode, calls the function and converts the Unicode results to ANSI (and if Shell32.dll ever provided an ANSI variant of this API, that's probably exactly what would do).
This article claims to provide an ANSI version of CommandLineToArgvW.
I followed the source for parse_cmd (see "argv_parsing.cpp" in the latest SDK) and modified it to match the paradigm and operation for CommandLineToArgW and developed the following. Note: instead of using LocalAlloc, per Microsoft recommendations (see https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723(v=vs.85).aspx) I've substituted HeapAlloc. Additionally one change in the SAL notation. I deviate slightly be stating
_In_opt_
for lpCmdLine - as CommandLineToArgvW does allow this to beNULL
, in which case it returns an argument list containing just the program name.A final caveat, parse_cmd will parse the command line slightly different from CommandLineToArgvW in one aspect only: two double quote characters in a row while the state is 'in quote' mode are interpreted as an escaped double quote character. Both functions consume the first one and output the second one. The difference is that for CommandLineToArgvW, there is a transition out of 'in quote' mode, while parse_cmdline remains in 'in quote' mode. This is properly reflected in the function below.
You would use the below function as follows:
int argc = 0; LPSTR *argv = CommandLineToArgvA(GetCommandLineA(), &argc); HeapFree(GetProcessHeap(), NULL, argv);
None of these solved the problem perfectly when don't want parsing UNICODE, so my solution is modified from WINE projects, they contains source code of
CommandLineToArgvW
ofshell32.dll
, modified it to below and it's work perfectly for me:Be careful when parsing empty string
""
, it's returnNULL
instead of executable path, that's the different behavior with the standardCommandLineToArgvW
, the recommanded usage is below: