Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Parsing the command line

Parsing the command line

Scheduled Pinned Locked Moved C / C++ / MFC
announcementc++data-structuresjsonperformance
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ernesto D
    wrote on last edited by
    #1

    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!

    M 1 Reply Last reply
    0
    • E Ernesto D

      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!

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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.

      E 1 Reply Last reply
      0
      • M Michael Dunn

        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.

        E Offline
        E Offline
        Ernesto D
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups