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

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    84

    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?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    TCHAR szProgramPath[MAX_PATH];
    GetModuleFileName(NULL, szProgramPath, MAX_PATH);

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    This seems to be a POSIX-compliant way of doing it:
    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <unistd.h>
    
    int main(int argc, char **argv)
    {
      char buf[PATH_MAX+1];
    
      if(!getcwd(buf, PATH_MAX))
      {
        puts("Unable to retrieve current working directory.\n");
        return 1;
      }
      printf("%s/%s\n", buf, argv[0]);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Banned
    Join Date
    Aug 2004
    Posts
    128
    this makes me think of a question :

    why would you want your program to multiply,
    you creating some kind of malicous program?

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    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\" ".

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Well, like I said...it's POSIX-compliant and Windows isn't anything-compliant
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    >> Windows isn't anything-compliant
    Except for Windows, which is the target platform of the OP's program.

    argv[0] is allowed to be a null string and there are no guarantees that the current working directory contains the currently executing image.

    gg

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Still, I said it appeared to be a POSIX-compliant way of doing it. If I say that a parachute will only open on sunny days will I be told by people that it doesn't open on rainy days?

    Question: Does POSIX allow argv[0] to be a NULL string or only the ANSI standard?
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    A strictly conforming POSIX application uses the ISO C standard, which clearly states that argv[0] may be a null string.

    >> I said it appeared to be a ... way of doing it
    And I said it's not a way of doing it, regardless of it's compliance to POSIX or anything else.

    gg

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by itsme86
    Still, I said it appeared to be a POSIX-compliant way of doing it. If I say that a parachute will only open on sunny days will I be told by people that it doesn't open on rainy days?
    When the person posing the question is looking for a parachute that opens on rainy does, yes you will.

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Unix Programming FAQ

    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').
    [Message Lengthening Device (MLDPlus ®) (Patents Pending)]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21