Hi:
I heard someone saying that under Visual C++ argv[0] does not
contain a fully qualified name when running under Windows XP.
Is that indeed the case?
Now I am at it, who does this expansion of the module name in
argv[0]. Is that done by the OS or by the VC run time?
Thanks, Erwin
argv[0] is not required to have a fully qualified name. The C++
standard only requires a pointer to a string that represents the
name used to invoke the program or "".
GetModuleFileName(NULL, ...) will get the full path and file name.
Erwin Kalvelagen wrote:
> I heard someone saying that under Visual C++ argv[0] does not
> contain a fully qualified name when running under Windows XP.
> Is that indeed the case?
I've not played with XP, but it's not true that argv[0] contains
a fully quallified name on any WIN32 (or UNIX either for that matter)
platform.
>
> Now I am at it, who does this expansion of the module name in
> argv[0]. Is that done by the OS or by the VC run time?
The OS passes the command line as a single string used to invoke
the command. The compiler runtime parses this to create via _setargv
that takes that string and breaks it up into the argv[] arguments and
then performs the wildcard expansion on that.
If you really want to know the file it was loaded from, then
you need to call GetModuleFileName.
You have not done much work with WIN32 I assume..... (You are
right about Unix, but absolutely wrong on Windows).
E:\argv>type argv.cpp
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("%s\n",argv[0]);
return 0;
}
E:\argv>debug\argv
E:\ARGV\DEBUG\ARGV.EXE
E:\argv>
You have not done much work with WIN32 I assume..... (You are
> I heard someone saying that under Visual C++ argv[0] does not
> contain a fully qualified name when running under Windows XP.
> Is that indeed the case?
It's not only true for XP, it's true for all WIN32 I've used
(it's also true for UNIX and lots of other platforms as well).
> Now I am at it, who does this expansion of the module name in
> argv[0]. Is that done by the OS or by the VC run time?
There is no expansion done at all. The command line used to invoke
the program is passed in by the operating system. The function
(which you can override if you like) called _setargv parses this
single string into the argv array. What gets dumped in argv[0]
is pretty much what the user typed. If he invoked the program
by typing "app" that's what goes there, if he typed a long path
(or it's started from the explorer typically), the it gets the
full path.
If you want to be assured of getting the file name of the executable
you should use GetModuleFileName().
"Richard Norman" <rsno...@mediaone.net> wrote in message news:71kc1uk1ovae7fg0t...@4ax.com...
"Richard Norman" <rsno...@mediaone.net> wrote in message news:71kc1uk1ovae7fg0t...@4ax.com...
Well, if by "command line" you mean the command line entered by the user at
the shell's command prompt that's certainly not true on 9x. I just tried
running a console application at the command line, using an unqualified
executable name, with two different command shells, and in both cases the
program returned a fully qualified executable name in argv[0].
On the other hand if one chooses to use spawnl() to launch an executable
argv[0] might just as easily contain "Fred".
Regards,
Will
I have to disagree.
Here is my program:
#include "stdio.h"
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
int main(int argc, char* argv[])
{
char fn[1024];
GetModuleFileName(NULL,fn,1024);
printf("argv[0] returns %s\n",argv[0]);
printf("GetModuleFilename returns %s\n",fn);
return 0;
}
compiled to argv.exe
the output is
argv[0] returns fluffy bunny
GetModuleFilename returns G:\ARGV.EXE
I think we can safely say that it is far safer to use GetModuleFilename than
argv[0]
Aside:
argv[0] actually comes from a call to GetCommandLine (In the VC CRT under
Win32 at any rate) which is descibed as follows:
"The GetCommandLine function returns a pointer to the command-line string
for the current process."
The interpretation of the meaning of "command line string" is left as an
excercise for the reader....
Paul
-cd
PS: Try it on your OS
#include <stdio.h>
int main(int arg, char* argv[])
{
printf("%s",argv[0]);
return 0;
}
"Erwin Kalvelagen" <er...@gams.com> wrote in message
news:egBp10mgBHA.1816@tkmsftngp02...
-cd
"Erwin Kalvelagen" <er...@gams.com> wrote in message
news:egBp10mgBHA.1816@tkmsftngp02...
argv[0] contains exactly the string used to launch the program under both
Windows 2000 and Windows XP: if a full path was specified to launch the
program it'll be a full path, otherwise not. This is apparently not the
behavior
under the Win9x family of fine program loaders.
The "expansion" of argc/argv is done by _setargv() {STDARGV.C} called
by __getmainargs() {CRTLIB.C}, called by mainCRTStartup() {CRTEXE.C}, which
are all part of the VC++ runtime library code.
-cd
"Erwin Kalvelagen" <er...@gams.com> wrote in message
news:uyLULwlgBHA.1644@tkmsftngp07...
>Hi:
>
>I heard someone saying that under Visual C++ argv[0] does not
>contain a fully qualified name when running under Windows XP.
>Is that indeed the case?
I don't know if the behavior under WinXP is different than under other
Windows versions, but in general, argv[0] isn't reliable. It will
typically contain whatever was the first argument on the command line
passed to CreateProcess, and if CreateProcess was passed an executable
name separately, this first argument needn't have any relationship to
the executable name. The program which calls CreateProcess determines
all this, so when you use argv[0], you're kinda at that program's
mercy.
>Now I am at it, who does this expansion of the module name in
>argv[0]. Is that done by the OS or by the VC run time?
Like Carl said, see stdargv.c to understand exactly how argv gets
filled in VC programs; it's done during program startup by the CRT,
but as mentioned earlier, the invoking program typically determines
the outcome. Like Richard said, use GetModuleFileName to reliably get
the fully qualified path. Use argv[0] only if you're reasonably sure
the programs which typically launch you set you up properly, and don't
rely on argv[0] to be correct unless you're absolutely sure all
programs which launch you set you up properly. In general, I would use
argv[0] only for things like printing messages to the console.
--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows
Erwin Kalvelagen wrote:
>
> > I've not played with XP, but it's not true that argv[0] contains
> > a fully quallified name on any WIN32 (or UNIX either for that matter)
> > platform.
> >
>
> You have not done much work with WIN32 I assume..... (You are
> right about Unix, but absolutely wrong on Windows).
I've done tons of work on WIN32. You're wrong.
>
> E:\argv>type argv.cpp
> #include <stdio.h>
>
> int main(int argc, char* argv[])
> {
> printf("%s\n",argv[0]);
> return 0;
> }
>
> E:\argv>debug\argv
> E:\ARGV\DEBUG\ARGV.EXE
>
> E:\argv>
On WIN2K, I get exactly what I typed to invoke the command "debug\argv" in this case.
"William DePalo [MVP]" wrote:
>
> "Ron Natalie" <r...@sensor.com> wrote in message
> news:3C16732F...@sensor.com...
> > There is no expansion done at all. The command line used to invoke
> > the program is passed in by the operating system.
>
> Well, if by "command line" you mean the command line entered by the user at
> the shell's command prompt that's certainly not true on 9x. I just tried
> running a console application at the command line, using an unqualified
> executable name, with two different command shells, and in both cases the
> program returned a fully qualified executable name in argv[0].
Evidentally, on the NT based stuff (NT, 2000, XP) you get what you type.
On the DOS variants (95/98/ME) you get the full path.
Thanks, Erwin
"Ron Natalie" <r...@sensor.com> wrote in message news:3C177A39...@sensor.com...
All wildcard expansion under DOS and Windows systems
is done by the runtime. You need to specify in your project
settings that you want wildcard expansion, however.
Check the help on this.