Thread: Can a executable find the path to itself?

  1. #1
    Registered User
    Join Date
    Jun 2006

    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.

    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,

  2. #2
    Registered User
    Join Date
    Jun 2006
    try using something like this..
    char 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
    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,

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    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.

    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:

    My website:
    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