Results 1 to 19 of 19

Thread: Command Line Parameters

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341

    Command Line Parameters

    in a windows 32bit application (non-console) the part in the WinMain function: LPSTR lpCmdLine is the command line. how do i use this if i want to test to see if it is equal to /full

    i tried

    if (lpCmdLine == "/full") {

    }

    but dat dont seem to work.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A string is not a char* pointer. The mistake which nearly EVERYBODY makes (not just in C/C++, they do it in VB too) is to think that the command line will only ever have one thing on it. In its simplest form, you'd need something like:
    Code:
    #include <windows.h>
    #include <string.h>
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
        if(strcmp(lpCmdLine, "/full") == 0) {
            /* They asked for full */
        }
    
        return 0;
    }
    However, what you actually need to do is to split up lpCmdLine into a tokenised set similar to what the runtime library does for you.

    http://www.microsoft.com/msj/default...hive/s569a.htm

    You want argcargv.c and argcargv.h which give you the code to split it up properly...that might help you along the way but these stupid college PCs don't have MSVC on them
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Cheers for the reply, it only takes the one parameter such as /full so i dont think splitting it will be an issue yet, though for my server it may well be.

    Thanks.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No, what I meant was, what if someone does:
    Code:
    prog.exe /full /blahblah
    That will break it because lpCmdLine will be equal to "/full /blahblah" and the comparison will return non-equal.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    lol, i know but like it only uses one thing, i dont have several parameters that can be used together or individually its just a toggle.

    Its done what i needed it too, so thanks !

  6. #6
    Zaei
    Guest
    Its a safety thing, Psy. If its just a crap project to test something simple out, thats fine, but you have to make sure that you do what parksie says when working on something that you might have to have command parameters with.

    Z.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    argcargv.c and argcargv.h - can someone send me these coz i cant find them on my pc

    [email protected]

    i need them now anyway coz i need something like the Split() thing in vb to seperate something such as

    ;/text;richardcaunt;hello people

    into

    /text
    richardcaunt
    hello people

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    They're in the link I posted
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    i getcha, only saw the test maps when i first looked, there is only argcargv.c so i take it a .h isnt required.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The .h is probably just for the prototypes so it'll be easy to recreate
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    well i copied that code fomr the site and created a header for it. it works ok except for in compiling i get an error:

    --------------------Configuration: temp - Win32 Debug--------------------
    Compiling...
    winmain.cpp
    argcargv.cpp
    C:\Windows\Profiles\Richard\Desktop\temp\argcargv.cpp(27) : error C2440: '=' : cannot convert from 'void *' to 'char *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Error executing cl.exe.

    temp.exe - 1 error(s), 0 warning(s)

    the code is:

    PSTR pszSysCmdLine, pszCmdLine;
    ..
    ..
    ..
    (line 27): pszCmdLine = HeapAlloc( GetProcessHeap(), 0, cbCmdLine+1 );

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's C code, so you should compile it in a .c file. The compiler error is because HeapAlloc returns void*, but you can't arbitrarily assign pointers in C++ (you can in C) so you need a (char*) cast which shuts it up
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    ok, i got the function working properly. now all i need to do is to get the data that it has split up from the argcargv.cpp file.

    the data is stored like so:

    Code:
    #define _MAX_CMD_LINE_ARGS  128
    char *_ppszArgv[_MAX_CMD_LINE_ARGS+1];
    i added a function like so to do this:

    Code:
    char *GetArgument (int argument) {
        return _ppszArgv[argument];
    }
    i then added this to my WinMain function:

    Code:
    int num = _ConvertCommandLineToArgcArgv ();
    
    for (int i = 0; i < num; i++) {
        char *temp;
        temp = GetArgument(i);
        MessageBox(NULL, temp, "Argument", MB_OK);
    }
    which makes it do nothing.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    lol, i just ran it again and it works

    do u have any clues as to how to modify this to work similar to the split function in vb ?

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    YAY - i got it going v. good.

    Cheers for all the help mike !

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No problems

    No idea how to get it working similarly to Split, haven't paid much attention to it. I have a definitely non-optimised bit of code for parsing them in the form:
    Code:
    myprog.exe --param=value --other=that
    ...but it relies on them already being split up. It's really messy and I haven't looked at it for ages so I won't burden you with it

    PS: Note that the memory allocated is never freed...it's unnecessary since it will be reclaimed when the process ends. As a byproduct, don't call _Convert... too many times.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    i got it converted to split it how i want, i added another parameter thats like char split so i can the call

    splitter.split("/text;richard;how are you", ';');

    to get

    /text
    richard
    how are you



    it needs to be called alot tho, so do i want it free up memory ?

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yes. The only reason that code doesn't bother is because they need to exist until the very end of main(), by which point they'll be freed by the system anyway.

    In a case like this, it's best to use a vector of strings.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    hmmm darn, that sounds well too complicated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width