I want to convert WinMain\'s cmdLine argument to argc and argv so I can use the argument parsing function I wrote for con
If you want plain int argc, char** argv arguments you have to do it on your own.
void fetchCmdArgs(int* argc, char*** argv) {
// init results
*argc = 0;
// prepare extraction
char* winCmd = GetCommandLine();
int index = 0;
bool newOption = true;
// use static so converted command line can be
// accessed from outside this function
static vector<char*> argVector;
// walk over the command line and convert it to argv
while(winCmd[index] != 0){
if (winCmd[index] == ' ') {
// terminate option string
winCmd[index] = 0;
newOption = true;
} else {
if(newOption){
argVector.push_back(&winCmd[index]);
(*argc)++;
}
newOption = false;
}
index++;
}
// elements inside the vector are guaranteed to be continous
*argv = &argVector[0];
}
// usage
int APIENTRY WinMain(...) {
int argc = 0;
char** argv;
fetchCmdArgs(&argc, &argv);
}
CommandLineToArgvW looks like it would be helpful here.
Based on Denis K response
See: https://msdn.microsoft.com/library/dn727674.aspx
This adds Windows specific entrypoint to clasic startpoint of your app:
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
{
return main(__argc, __argv);
}
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.