5

Possible Duplicate:
how to find the location of the executable in C

I'm writting an multi-platform app in C++ using GTK+ and I have a problem. I must get program path. E.g., when program is in /home/user/program (or C:\Users\user\program.exe), i have /home/user/ (or C:\Users\user\).

Can and how I can do this?

Community
  • 1
  • 1
m4tx
  • 4,139
  • 5
  • 37
  • 61

3 Answers3

8

For Win32/MFC c++ programs:

char myPath[_MAX_PATH+1];
GetModuleFileName(NULL,myPath,_MAX_PATH);

Also observe the remarks at http://msdn.microsoft.com/en-us/library/windows/desktop/ms683156%28v=vs.85%29.aspx,

In essence: WinMain does not include the program name in lpCmdLine, main(), wmain() and _tmain() should have it at argv[0], but:

Note: The name of the executable in the command line that the operating system provides to a process is not necessarily identical to that in the command line that the calling process gives to the CreateProcess function. The operating system may prepend a fully qualified path to an executable name that is provided without a fully qualified path.

Oliver Zendel
  • 2,695
  • 34
  • 29
5

argv[0] contains the program name with path. Am I missing something here?

Jaywalker
  • 3,079
  • 3
  • 28
  • 44
  • Probably not, unless you're having a problem with the contents of `argv[0]`. – Daniel Lidström Dec 23 '10 at 12:10
  • 6
    You should't trust `argv[0]` - see [link](http://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi) – shi May 07 '15 at 14:06
3

On windows..

#include <stdio.h>  /* defines FILENAME_MAX */
#ifdef WINDOWS
    #include <direct.h>
    #define GetCurrentDir _getcwd
#else
    #include <unistd.h>
    #define GetCurrentDir getcwd
 #endif

 char cCurrentPath[FILENAME_MAX];

 if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
     {
     return errno;
     }

cCurrentPath[sizeof(cCurrentPath) - 1] = '/0'; /* not really required */

printf ("The current working directory is %s", cCurrentPath);

Linux

char szTmp[32];
sprintf(szTmp, "/proc/%d/exe", getpid());
int bytes = MIN(readlink(szTmp, pBuf, len), len - 1);
if(bytes >= 0)
        pBuf[bytes] = '\0';
return bytes;

And you should look at this question..

How do I get the directory that a program is running from?

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Is not there a universal solution? :) – m4tx Dec 23 '10 at 09:36
  • 1
    why downvote? the solution is a very interesting alternative IMO, especially if the main is not in your hands, e.g. if you are writing a library. +1 – davka Dec 23 '10 at 11:00
  • 3
    This anwser is incorrect; you are supplying the current working directory, which is totally different from the application path... In the thread you linked, there is the same confusion – Oliver Zendel May 24 '12 at 08:54
  • 1
    @OliverZendel, this answer is half correct. In windows, it's wrong, and your answer is correct. On linux it is perfectly correct. `readlink` of `/proc/pid/exe` does the same as `GetModuleFileName` in windows. Either way, the accepted answer is completely wrong. – Shahbaz Dec 12 '12 at 13:35