Parsing the command line
-
Hi, im writting a small W32 application (non MFC) and i need to parse the command line for passed arguments, and since the app MUST be compiled for unicode, the MSDN docs say i should use the GetCommandLine() function to get a unicode version of it, and it also suggests using CommandLineToArgvW() to get a main() style array of arguments and a count. inside my winmain(), heres what i do:
// get the arguments int argcount=0; LPWSTR* arguments=CommandLineToArgvW(GetCommandLine(), &argcount); // do stuff with arguments . . . // release the memory use for arguments(¿?) if(arguments) GlobalFree((HGLOBAL)arguments);
the problem is that in the GlobalFree() line, the app takes an exception and crashes. but MSDN sais i should do the GlobalFree! i quote: "It is the caller's responsibility to free the memory used by the argument list when it is no longer needed. To free the memory, use a single call to the GlobalFree function." any idea of what im doing wrong? thanks! -
Hi, im writting a small W32 application (non MFC) and i need to parse the command line for passed arguments, and since the app MUST be compiled for unicode, the MSDN docs say i should use the GetCommandLine() function to get a unicode version of it, and it also suggests using CommandLineToArgvW() to get a main() style array of arguments and a count. inside my winmain(), heres what i do:
// get the arguments int argcount=0; LPWSTR* arguments=CommandLineToArgvW(GetCommandLine(), &argcount); // do stuff with arguments . . . // release the memory use for arguments(¿?) if(arguments) GlobalFree((HGLOBAL)arguments);
the problem is that in the GlobalFree() line, the app takes an exception and crashes. but MSDN sais i should do the GlobalFree! i quote: "It is the caller's responsibility to free the memory used by the argument list when it is no longer needed. To free the memory, use a single call to the GlobalFree function." any idea of what im doing wrong? thanks!I tried that exact code (just putting the arguments into a list ctrl) and it worked with no problems. Post the rest of your code where you "do stuff" :) --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber Laugh it up, fuzzball.
-
I tried that exact code (just putting the arguments into a list ctrl) and it worked with no problems. Post the rest of your code where you "do stuff" :) --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber Laugh it up, fuzzball.
hi, thanks for your reply. when i ran that code in debug mode on my XP machine, it just caused a "first chance exception" shown in the output window of vc, (i dint even noticed it the first time) later i compiled the release version and it worked OK on windows XP, but when i tried it on NT4 server, then the exception became a "memory cant be read" error message, and the program crashed. heres the code in "do stuff": // checkout the command line for passed arguments int nargs=0; LPWSTR* cmdargs=CommandLineToArgvW(GetCommandLine(), &nargs); LPWSTR argument=NULL; for(int i=1; i<nargs; i++) { argument=cmdargs[i]; switch(i) { case 1: // 1st arg is times to do the test { int tests=_wtoi(argument); tests=tests>=0? tests : 0; if(!tests) { if(wcscmp(argument, L"0")!=0) // atoi failed or really zero? days=DEFAULT_TEST_TIMES; // invalid argument! } mttimes=tests; break; } case 2: // 2nd is "F" or "f" for File, (writte a log file) if(wcsicmp(argument, L"F")==0) dolog=TRUE; break; default: break; } } // release the memory if(cmdargs) GlobalFree((HGLOBAL)cmdargs); the first chance exception occurs in the GlobalFree() line, if i remove it, then it runs fine on both XP and NT4 and both release and debug versions.