Have an executable find its own name

This is a discussion on Have an executable find its own name within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I'm trying to make a program that, when compiled, the executable will know its name. For example, the ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Have an executable find its own name

    Hey everyone,

    I'm trying to make a program that, when compiled, the executable will know its name.

    For example, the filename could be asdf.exe, and when I cout the name, it will show asdf.exe, or when its renamed to qwerty.exe, the file will output qwerty.exe.

    Probably really simple,

    I tried using __FILE__, but that just shows file's name that you're working on...
    ~guitarist809~

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, one easy way to know what's your program name (and path) is to use the argument from the main function, ie

    Code:
    #include <iostream>
    int main(int argc, char **argv)
    {
       std::cout << argv[0] << std::endl;
       return 0;
    }
    Depending on how you lauch the application, the full path of the executable might appear, but this is not a big of a problem.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,456
    You can't use a preprocessor to obtain runtime information.

    You're reliant on implementation defined behaviour, generally, or some function call specific to the operating system.

    Arguments to the main() function provide the only means specified in the standard.
    Code:
    #include <iostream>
    
    int main(int argc, char **argv)
    {
         std::cout << argv[0] << '\n';
    }
    There are a few caveats on this ...... The string printed out by this is formally implementation defined but, in practice, often contains the name of the executable (with or without fully qualified path information). It can also contain an empty string ("") if the implementation can't supply the name. On some systems, argv[0] yields some token that refers to the executable (eg an alias or symbol that needs to be translated to obtain the name). If you need path information (i.e. the directory the executable is in) and argv[0] does not contain that, it is often necessary to search through various defined locations (eg PATH environment variable under unix and windows, the current working directory, etc).

    Some modern operating systems provide a function call to obtain the executable name. You will need to read your system documentation to find those.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Thanks foxman and grumpy!

    the argv[0] appears to have worked =]

    You rule
    ~guitarist809~

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    There's also GetModuleFileName on Windows if you don't mind a platform specific answer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 12:07 AM
  3. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

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