views:

135

answers:

2

I want to convert WinMain's cmdLine argument to argc and argv so I can use the argument parsing function I wrote for console applications.

This would be trivial except that I want to support "quotes" too. For example:

test.exe test1 test2 "testing testing"

should be

argv[0] = "test.exe"; argv[1] = "test1"; argv[2] = "test2"; argv[3] = "testing testing";

I realize that cmdLine doesn't have the program name (the argv[0]); this doesn't matter I can use a dummy value.

I was thinking of doing it with a regex, (("[^"]+")\s+)|(([^\s]+)\s*) I'm not sure how well it would work though.. Probably not very well? Is there any function to do that in the windows api? Thanks

+3  A: 

CommandLineToArgvW looks like it would be helpful here.

sean e
Just watch out that `CommandLineToArgvW` doesn't add a sentinel NULL value to the end of the array, so it doesn't quite have the same semantics as `argv` in standard C.
jamesdlin
+1  A: 

If you are using Microsoft compiler, there are public symbols __argc, __argv and __wargv defined in stdlib.h. This also applies to MinGW that uses Microsoft runtime libraries.

Denis Krjuchkov
+1. When these are available, I personally prefer these over `CommandLineToArgvW` since you don't have to about calling `LocalFree` and since `__argv[__argc]` and `__wargv[__argc]` are set to `NULL`.
jamesdlin