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.
Printable View
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.
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: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.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;
}
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 :rolleyes:
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.
No, what I meant was, what if someone does:That will break it because lpCmdLine will be equal to "/full /blahblah" and the comparison will return non-equal.Code:prog.exe /full /blahblah
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 !
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.
argcargv.c and argcargv.h - can someone send me these coz i cant find them on my pc :D:D
[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
They're in the link I posted :confused:
i getcha, only saw the test maps when i first looked, there is only argcargv.c so i take it a .h isnt required.
The .h is probably just for the prototypes so it'll be easy to recreate :)
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 );
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 :)
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:
i added a function like so to do this:Code:
#define _MAX_CMD_LINE_ARGS 128
char *_ppszArgv[_MAX_CMD_LINE_ARGS+1];
i then added this to my WinMain function:Code:
char *GetArgument (int argument) {
return _ppszArgv[argument];
}
which makes it do nothing.Code:
int num = _ConvertCommandLineToArgcArgv ();
for (int i = 0; i < num; i++) {
char *temp;
temp = GetArgument(i);
MessageBox(NULL, temp, "Argument", MB_OK);
}
lol, i just ran it again and it works :D
do u have any clues as to how to modify this to work similar to the split function in vb ?
YAY - i got it going v. good.
Cheers for all the help mike !
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:...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 :)Code:myprog.exe --param=value --other=that
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 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
:D:D
it needs to be called alot tho, so do i want it free up memory ?
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.
hmmm darn, that sounds well too complicated :(