Thread: Can a executable find the path to itself?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Can a executable find the path to itself?

    I’m writing a short executable that will be run by both clicking on it and being called from another program (IDL). Is there a function that I can put in this executable that returns the path to itself? Since, “myprogram.exe” might be moved to a different location I don’t want to hard wire the path, but I want an outputted text file to end up in the same location as the executable. I’ve tried _fullpath, which returns the correct path if you double click on it, but returns the incorrect path if called from IDL.

    Code:
    sprintf(szBuffer,_fullpath( NULL, "myprogram.exe", _MAX_PATH ));
    Double click on myprogram.exe -> C:\myprogram\ myprogram.exe

    Call from IDL -> C:\Location_of_IDL_File\myprogram.exe

    Any ideas, thanks,
    Sulli_jj

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    try using something like this..
    Code:
    char yourpath[BUFSIZE]
    
    GetModuleFileName(NULL,yourpath,BUFSIZE);
    this will store the path to your exe as well as your exe's name.
    hope I am understanding you...

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    Thanks g1i7ch, this works.

    Actually all I really want is the directory path, but I can remove the "myprogram.exe" from the end of the returned string easily.

    Again thanks,
    sulli_jj

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can also use argv[0], which contains the name of the executable. On Linux, however, the path is sometimes something like "../bin/ls", so if you want a full path, you could try getcwd(), which returns the current working directory. Unless the working directory is different from the executable path.

    You could just use "./filename" and set the current working directory to "." in the properties dialog box.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 12:57 AM
  2. Have an executable find its own name
    By guitarist809 in forum C++ Programming
    Replies: 4
    Last Post: 02-29-2008, 06:44 PM
  3. Find Dialog
    By Lithorien in forum Windows Programming
    Replies: 6
    Last Post: 04-25-2005, 05:28 PM
  4. 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
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM