Getting filename of my program

This is a discussion on Getting filename of my program within the C Programming forums, part of the General Programming Boards category; I'm wiriting program for windows, which would copy itself and do stuff, and I need to know exact filename of ...

Getting filename of my program

I'm wiriting program for windows, which would copy itself and do stuff, and I need to know exact filename of my program (path wold be nice too). I've tried to modify crap from command line, but there is zillion conditions where I can make an error generator of my prog. Like "c:\program.exe param\prog2.exe", "program.exe" can be file or "program.exe param" cold be directory, you'll never know...
So... back to the question, how can I determine filename of my program?

Not really, we just competing with guys who can make most annoying program (you know, popups and stuff) and if other can find the "cure". It's actually very stupid when you think about it...
BUT if you want some practical application for self multiplying program I can give you one: self installing programs.

Anyway, thanks anonytmouse, completely forgot about that.
isme86, the problem is that if I type "c:\program.exe" in command line. argv[0] will be "c:\\program.exe" or even better, in windows GetCommandLine() can return "\"c:\\program.exe\" ".

1.14 How can I find a process' executable file?
===============================================

This would be a good candidate for a list of `Frequently Unanswered
Questions', because the fact of asking the question usually means that the
design of the program is flawed. :-)

You can make a `best guess' by looking at the value of `argv[0]'. If this
contains a `/', then it is probably the absolute or relative (to the
current directory at program start) path of the executable. If it does
not, then you can mimic the shell's search of the `PATH' variable, looking
for the program. However, success is not guaranteed, since it is possible
to invoke programs with arbitrary values of `argv[0]', and in any case the
executable may have been renamed or deleted since it was started.

If all you want is to be able to print an appropriate invocation name with
error messages, then the best approach is to have `main()' save the value
of `argv[0]' in a global variable for use by the entire program. While
there is no guarantee whatsoever that the value in `argv[0]' will be
meaningful, it is the best option available in most circumstances.

The most common reason people ask this question is in order to locate
configuration files with their program. This is considered to be bad form;
directories containing executables should contain *nothing* except
executables, and administrative requirements often make it desirable for
configuration files to be located on different filesystems to executables.

A less common, but more legitimate, reason to do this is to allow the
program to call `exec()' *on itself*; this is a method used (e.g. by some
versions of `sendmail') to completely reinitialise the process (e.g. if a
daemon receives a `SIGHUP').