Thread: Have an executable find its own name

  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
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    int main(int, char* argv[])
    {
        printf("%s\n",argv[0]);
        return 0;
    }
    Or there's always GetModuleFileName

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Declaring unnamed parameters is C++, but you're using printf(), implying C. Confusing . . . .

    (I won't mention the lack of header files, because it's just an example . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There is no real cross-platform solution. argv[0] is unreliable and could be a lot of things. With CreateProcess() or exec(), it could be anything and totally unrelated to the actual executable.

    The platform-specific solutions:
    Win32: GetModuleFileName()
    Linux: readlink on /proc/self/exe
    Other POSIXly things: no idea
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by dwks View Post
    Declaring unnamed parameters is C++, but you're using printf(), implying C. Confusing . . . .

    (I won't mention the lack of header files, because it's just an example . . . .)
    Not using those ugly C++ extra library functions doesn't make it C.

    Anyway, what operation system is the OP using?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What operating system? Well, this is the Windows Programming Forum . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by dwks View Post
    What operating system? Well, this is the Windows Programming Forum . . . .
    I was probably confuzzled by CornedBee's reply.

    Anyway, use strrchr() to get the last occurence (+1) of backslash in the buffer used with GetModuleFileName().
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    if you use cout << argv[0] , it will display the whole path and the program name, so if you only want just the program name to be displayed, here's a program for that:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string FindName(string path)
    {
        int i,temp = 0;
        string name = "";
        
        cycle:
        for(i=temp; i<path.length(); i++) { 
            if(path.at(i) == '\\') {
                temp = i+1;
                goto cycle;
        }
    }
        for(i=temp; i<path.length(); i++)
        {
            name+=path.at(i);
            }
        return name;
    }
    
    int main(int argc, char* argv[])
    {
        cout << FindName(argv[0]) << endl;
        system("PAUSE");
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Excpet that argv[0] is actually whatever was passed into exec() in a Unix-based system, so it's by far not guaranteed to work.

    It is perfectly valid to call:
    Code:
    execl("\usr\bin\myapp", "notmyapp", "arg1", "arg2", NULL);
    In this case argv[0] would be "notmyapp", whilst the executable path and name is "\usr\bin\myapp".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    char* GetNameFromPath(char* lpBuffer){
        for(int i=strlen(lpBuffer)-1;i>0;i--){
            if(lpBuffer[i]=='\\'){ return &lpBuffer[i+1]; }
        }
        return lpBuffer;
    }
    
    int main(void){
        char lpBuffer[MAX_PATH];
        GetModuleFileName(GetModuleHandle(NULL),lpBuffer,MAX_PATH);
        cout << GetNameFromPath(lpBuffer) << endl;
        system("pause");
    }
    Last edited by maxorator; 03-10-2008 at 12:23 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by maxorator View Post
    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    char* GetNameFromPath(char* lpBuffer){
        for(int i=strlen(lpBuffer)-1;i>0;i--){
            if(lpBuffer[i]=='\\'){ return &lpBuffer[i+1]; }
        }
        return lpBuffer;
    }
    
    int main(void){
        char lpBuffer[MAX_PATH];
        GetModuleFileName(GetModuleHandle(NULL),lpBuffer,MAX_PATH);
        cout << GetNameFromPath(lpBuffer) << endl;
        system("pause");
    }
    The code gives me an error

    1>install.h(21) : error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'
    ~guitarist809~

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Either disable UNICODE or use GetModuleFileNameA() instead, apparently.
    http://www.mpcforum.com/showthread.php?t=188636
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by dwks View Post
    Either disable UNICODE or use GetModuleFileNameA() instead, apparently.
    http://www.mpcforum.com/showthread.php?t=188636
    You are... the greatest

    Thanks!
    ~guitarist809~

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, 01: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, 08: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, 09:28 AM