Click here to Skip to main content
15,916,683 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to use command line arguments in a win32 application? Pin
Sauce!14-Jul-08 21:14
Sauce!14-Jul-08 21:14 
AnswerRe: How to use command line arguments in a win32 application? Pin
Cedric Moonen14-Jul-08 21:26
Cedric Moonen14-Jul-08 21:26 
GeneralRe: How to use command line arguments in a win32 application? Pin
Sauce!14-Jul-08 21:36
Sauce!14-Jul-08 21:36 
AnswerRe: How to use command line arguments in a win32 application? Pin
CPallini14-Jul-08 21:41
mveCPallini14-Jul-08 21:41 
GeneralRe: How to use command line arguments in a win32 application? Pin
Sauce!14-Jul-08 21:50
Sauce!14-Jul-08 21:50 
GeneralRe: How to use command line arguments in a win32 application? Pin
CPallini14-Jul-08 22:00
mveCPallini14-Jul-08 22:00 
GeneralRe: How to use command line arguments in a win32 application? Pin
Sauce!14-Jul-08 22:21
Sauce!14-Jul-08 22:21 
GeneralRe: How to use command line arguments in a win32 application? Pin
CPallini14-Jul-08 22:38
mveCPallini14-Jul-08 22:38 
Sauce! wrote:
Now, may we discuss the bad things I did in my original snippet so that I may learn from it in the future? That would be nice


Your original code:
// Get command-line parameters
	for (int n=0; n < (sizeof(lpCmdLine) / sizeof(char)); n++)
	{
		if(lpCmdLine[n] == "-" && lpCmdLine[n+1] == "w")
		{
			char w[4];
			w[0] = lpCmdLine[n+2];
			w[1] = lpCmdLine[n+3];
			w[2] = lpCmdLine[n+4];
			w[3] = lpCmdLine[n+5];
			WINDOW_WIDTH = w[];
		}

		if(lpCmdLine[n] == "-" && lpCmdLine[n+1] == "h")
		{
			char h[4];
			h[0] = lpCmdLine[n+2];
			h[1] = lpCmdLine[n+3];
			h[2] = lpCmdLine[n+4];
			h[3] = lpCmdLine[n+5];
			WINDOW_HEIGHT = h[];
		}
	}


1. sizeof(lpCmdLine) = sizeof(LPSTR) = 4 (on 32 bit systems). You should use strlen(lpCmdLine) instead.

2. Since the code inside the loop accesses lpCmdLine[n+5], the loop itself cannot run on the whole lpCmdLine's range available.
So the loop controlling condition should be (n < strlen(lpCmdLine) - 5) to
prevent buffer overrun.

3. You wrote your own version of atoi, but it is wrong: suppose pCmdLine[0] to pCmdLine[3] containing the right input for the number, the (brute force and simplified) code for a atoi-like function may be
int value = 0;
value = pCmdLine[0]-'0'; // since pCmdLine[0] is a character 
value *= 10;
value += pCmdLine[1]-'0';
value *= 10;
value += pCmdLine[2]-'0';
value *= 10;
value += pCmdLine[3]-'0';

Smile | :)

If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke


[My articles]

GeneralRe: How to use command line arguments in a win32 application? Pin
Sauce!15-Jul-08 2:39
Sauce!15-Jul-08 2:39 
GeneralRe: How to use command line arguments in a win32 application? Pin
CPallini15-Jul-08 3:15
mveCPallini15-Jul-08 3:15 
GeneralRe: How to use command line arguments in a win32 application? Pin
Sauce!15-Jul-08 5:51
Sauce!15-Jul-08 5:51 
GeneralRe: How to use command line arguments in a win32 application? Pin
CPallini16-Jul-08 7:04
mveCPallini16-Jul-08 7:04 
AnswerRe: How to use command line arguments in a win32 application? Pin
David Crow15-Jul-08 3:21
David Crow15-Jul-08 3:21 
QuestionHow to verify that calling process has read access to the specified range of memory in vista Pin
V K 214-Jul-08 20:33
V K 214-Jul-08 20:33 
AnswerRe: How to verify that calling process has read access to the specified range of memory in vista Pin
Stephen Hewitt14-Jul-08 20:42
Stephen Hewitt14-Jul-08 20:42 
AnswerRe: How to verify that calling process has read access to the specified range of memory in vista Pin
Stephen Hewitt14-Jul-08 20:53
Stephen Hewitt14-Jul-08 20:53 
Questionfatal error: Cannot open type library file: 'msxml.dll' in Vista Pin
NiceNaidu14-Jul-08 19:48
NiceNaidu14-Jul-08 19:48 
AnswerRe: fatal error: Cannot open type library file: 'msxml.dll' in Vista Pin
Stephen Hewitt14-Jul-08 20:22
Stephen Hewitt14-Jul-08 20:22 
Questionenable DHCP using win32 API Pin
an8914-Jul-08 19:45
an8914-Jul-08 19:45 
AnswerRe: enable DHCP using win32 API Pin
Stephen Hewitt14-Jul-08 20:36
Stephen Hewitt14-Jul-08 20:36 
Question[Window Mobile Owner Draw] Pin
jjobluewind9714-Jul-08 19:39
jjobluewind9714-Jul-08 19:39 
Questionerror C2143: syntax error : missing '{' before ':' Pin
rp_suman14-Jul-08 18:11
rp_suman14-Jul-08 18:11 
AnswerRe: error C2143: syntax error : missing '{' before ':' Pin
ThatsAlok15-Jul-08 3:36
ThatsAlok15-Jul-08 3:36 
GeneralRe: error C2143: syntax error : missing '{' before ':' Pin
rp_suman15-Jul-08 4:18
rp_suman15-Jul-08 4:18 
QuestionRe: error C2143: syntax error : missing '{' before ':' Pin
rp_suman15-Jul-08 18:22
rp_suman15-Jul-08 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.