By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,376 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,376 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i], "-pw", strlen("-pw")) == 0)
{
cmdLineParameter[k]=(char *)malloc(sizeof(char)*
strlen(&szCmdLine[i])));
strncpy(cmdLineParameter[k],&szCmdLine[i],strlen(&szCmdLine[i]));
k++;
continue;
}
if (strncmp(&szCmdLine[i], "-vpw", strlen("-vpw")) ==
0)
{
cmdLineParameter[k]=(char *)malloc(sizeof(char)*
strlen(&szCmdLine[i])));
strncpy(cmdLineParameter[k],&szCmdLine[i],strlen(&szCmdLine[i]));
k++;
continue;
}
etc....
}
}
thanking u,
Nagesh

Dec 28 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Dec 28 '05 #2

P: n/a
hi,
I followed ur tips. I could run without any problem.

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

Dec 30 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.