Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

argv and windows XP

34 views
Skip to first unread message

Erwin Kalvelagen

unread,
Dec 11, 2001, 10:54:24 AM12/11/01
to

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


Erwin Kalvelagen

unread,
Dec 11, 2001, 12:57:13 PM12/11/01
to

Richard Norman

unread,
Dec 11, 2001, 1:34:49 PM12/11/01
to

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.

Ron Natalie

unread,
Dec 11, 2001, 1:45:38 PM12/11/01
to

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.

Erwin Kalvelagen

unread,
Dec 11, 2001, 3:09:18 PM12/11/01
to

> 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).

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>


Erwin Kalvelagen

unread,
Dec 11, 2001, 3:11:22 PM12/11/01
to

> 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

Ron Natalie

unread,
Dec 11, 2001, 3:57:19 PM12/11/01
to
Let me try this again. The first one got eaten by the message
gnomes.

> 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().

Erwin Kalvelagen

unread,
Dec 11, 2001, 4:09:49 PM12/11/01
to
Thanks. I am aware of that, but that was not my question.

"Richard Norman" <rsno...@mediaone.net> wrote in message news:71kc1uk1ovae7fg0t...@4ax.com...

Erwin Kalvelagen

unread,
Dec 11, 2001, 4:22:34 PM12/11/01
to
Thanks. I was aware of that. It does not really answer
my question however.

"Richard Norman" <rsno...@mediaone.net> wrote in message news:71kc1uk1ovae7fg0t...@4ax.com...

William DePalo [MVP]

unread,
Dec 11, 2001, 4:34:30 PM12/11/01
to
"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].

On the other hand if one chooses to use spawnl() to launch an executable
argv[0] might just as easily contain "Fred".

Regards,
Will


Paul

unread,
Dec 11, 2001, 5:48:55 PM12/11/01
to
"Erwin Kalvelagen" <er...@gams.com> wrote in message
news:#Ewsr#ngBHA.2428@tkmsftngp03...

> You have not done much work with WIN32 I assume..... (You are
> right about Unix, but absolutely wrong on Windows).

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


Carl Daniel

unread,
Dec 12, 2001, 12:28:35 AM12/12/01
to
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.

-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...

Carl Daniel

unread,
Dec 12, 2001, 12:35:10 AM12/12/01
to
Oh, and 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:egBp10mgBHA.1816@tkmsftngp02...

Carl Daniel

unread,
Dec 12, 2001, 12:41:07 AM12/12/01
to
Try posting your question once and waiting a while for responses before
posting it again....

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...

Doug Harrison [MVP]

unread,
Dec 12, 2001, 1:26:23 AM12/12/01
to
Erwin Kalvelagen wrote:

>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

Ron Natalie

unread,
Dec 12, 2001, 10:39:37 AM12/12/01
to

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.

Ron Natalie

unread,
Dec 12, 2001, 10:40:45 AM12/12/01
to

"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.

Erwin Kalvelagen

unread,
Dec 12, 2001, 11:01:52 AM12/12/01
to

Well, I owe you an apology.

Thanks, Erwin

"Ron Natalie" <r...@sensor.com> wrote in message news:3C177A39...@sensor.com...

Ron Ruble

unread,
Dec 11, 2001, 6:41:18 PM12/11/01
to

Erwin Kalvelagen wrote in message ...

>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?


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.


0 new messages