|
-
Jan 17th, 2002, 11:43 AM
#1
Thread Starter
Frenzied Member
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.
-
Jan 17th, 2002, 02:00 PM
#2
Monday Morning Lunatic
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
-
Jan 17th, 2002, 02:03 PM
#3
Thread Starter
Frenzied Member
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.
-
Jan 17th, 2002, 02:08 PM
#4
Monday Morning Lunatic
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
-
Jan 17th, 2002, 02:14 PM
#5
Thread Starter
Frenzied Member
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 !
-
Jan 17th, 2002, 05:51 PM
#6
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.
-
Jan 18th, 2002, 02:05 PM
#7
Thread Starter
Frenzied Member
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
-
Jan 18th, 2002, 02:39 PM
#8
Monday Morning Lunatic
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
-
Jan 18th, 2002, 02:51 PM
#9
Thread Starter
Frenzied Member
i getcha, only saw the test maps when i first looked, there is only argcargv.c so i take it a .h isnt required.
-
Jan 18th, 2002, 02:55 PM
#10
Monday Morning Lunatic
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
-
Jan 19th, 2002, 04:38 PM
#11
Thread Starter
Frenzied Member
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 );
-
Jan 20th, 2002, 06:02 AM
#12
Monday Morning Lunatic
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
-
Jan 20th, 2002, 07:43 AM
#13
Thread Starter
Frenzied Member
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.
-
Jan 20th, 2002, 07:45 AM
#14
Thread Starter
Frenzied Member
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 ?
-
Jan 20th, 2002, 07:59 AM
#15
Thread Starter
Frenzied Member
YAY - i got it going v. good.
Cheers for all the help mike !
-
Jan 20th, 2002, 08:11 AM
#16
Monday Morning Lunatic
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
-
Jan 20th, 2002, 08:14 AM
#17
Thread Starter
Frenzied Member
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 ?
-
Jan 20th, 2002, 09:45 AM
#18
Monday Morning Lunatic
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
-
Jan 20th, 2002, 09:47 AM
#19
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|