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

N

Nagesh

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
 
P

pH

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.
 
N

Nagesh

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

thank u very much,
Wish u a happy new year,
Nagesh
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top