problem in placing command line aruments(LPSTR) into **char

Discussion in 'C++' started by Nagesh, Dec 28, 2005.

  1. Nagesh

    Nagesh Guest

    hi,

    I am want copy the command line arguments from szCmdLine of
    WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,
    int iCmdShow) into **char cmdLineParameter. So that I can use
    cmdLineParameter in other functions. If any body knows pls help me

    I am trying the following code sample:

    char **cmdLineParameter;
    int k=0;
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR
    szCmdLine, int iCmdShow)
    {
    for (i = 0; i < strlen(szCmdLine); i++)
    {
    // Now check for command-line arguments
    if (strncmp(&szCmdLine, "-pw", strlen("-pw")) == 0)
    {
    cmdLineParameter[k]=(char *)malloc(sizeof(char)*
    strlen(&szCmdLine)));
    strncpy(cmdLineParameter[k],&szCmdLine,strlen(&szCmdLine));
    k++;
    continue;
    }
    if (strncmp(&szCmdLine, "-vpw", strlen("-vpw")) ==
    0)
    {
    cmdLineParameter[k]=(char *)malloc(sizeof(char)*
    strlen(&szCmdLine)));
    strncpy(cmdLineParameter[k],&szCmdLine,strlen(&szCmdLine));
    k++;
    continue;
    }
    etc....
    }
    }


    thanking u,
    Nagesh
     
    Nagesh, Dec 28, 2005
    #1
    1. Advertising

  2. Nagesh

    pH Guest

    The code you're using there will cause memory faults, as you never
    allocate memory for the array cmdLineParameters itself, only for the
    strings its elements point to; so, you need something like
    cmdLineParameter = (char **)malloc(sizeof(char *) * something), where
    something is the number of arguments you are expecting.

    Also, using strncmp() at every offset into szCmdLine is rather
    inefficient; it might be better to use strtok or similar to split the
    string at each space (or whatever) character, and then put the results
    in your cmdLineParameter array. You'd still need to allocate memory for
    this array; to avoid over/under allocating the amount, you might want
    to count how many spaces appear in the szCmdLine and allocate space for
    that many parameters, and then copy tokens out of the string into the
    array.
     
    pH, Dec 28, 2005
    #2
    1. Advertising

  3. Nagesh

    Nagesh Guest

    hi,
    I followed ur tips. I could run without any problem.

    thank u very much,
    Wish u a happy new year,
    Nagesh
     
    Nagesh, Dec 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gent
    Replies:
    4
    Views:
    5,135
    John Harrison
    Nov 4, 2004
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,159
    Ian Collins
    May 9, 2006
  3. Replies:
    6
    Views:
    599
    CBFalconer
    Feb 17, 2007
  4. Replies:
    3
    Views:
    763
  5. Larry

    LPSTR to PBYTE

    Larry, Jan 12, 2010, in forum: C++
    Replies:
    22
    Views:
    4,449
    Evil's Toy
    Jan 15, 2010
Loading...

Share This Page