How to Find the Location of the Executable in C

How do I find the location of the executable in C?

To summarize:

  • On Unixes with /proc really straight and realiable way is to:

    • readlink("/proc/self/exe", buf, bufsize) (Linux)

    • readlink("/proc/curproc/file", buf, bufsize) (FreeBSD)

    • readlink("/proc/self/path/a.out", buf, bufsize) (Solaris)

  • On Unixes without /proc (i.e. if above fails):

    • If argv[0] starts with "/" (absolute path) this is the path.

    • Otherwise if argv[0] contains "/" (relative path) append it to cwd
      (assuming it hasn't been changed yet).

    • Otherwise search directories in $PATH for executable argv[0].

    Afterwards it may be reasonable to check whether the executable isn't actually a symlink.
    If it is resolve it relative to the symlink directory.

    This step is not necessary in /proc method (at least for Linux).
    There the proc symlink points directly to executable.

    Note that it is up to the calling process to set argv[0] correctly.
    It is right most of the times however there are occasions when the calling process cannot be trusted (ex. setuid executable).

  • On Windows: use GetModuleFileName(NULL, buf, bufsize)

how to get path to executable in C running on Windows?

Use GetModuleFileName() and pass NULL as the first argument:

DWORD last_error;
DWORD result;
DWORD path_size = 1024;
char* path = malloc(1024);

for (;;)
{
memset(path, 0, path_size);
result = GetModuleFileName(0, path, path_size - 1);
last_error = GetLastError();

if (0 == result)
{
free(path);
path = 0;
break;
}
else if (result == path_size - 1)
{
free(path);
/* May need to also check for ERROR_SUCCESS here if XP/2K */
if (ERROR_INSUFFICIENT_BUFFER != last_error)
{
path = 0;
break;
}
path_size = path_size * 2;
path = malloc(path_size);
}
else
{
break;
}
}

if (!path)
{
fprintf(stderr, "Failure: %d\n", last_error);
}
else
{
printf("path=%s\n", path);
}

Getting the absolute path of the executable, using C#?

MSDN has an article that says to use System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; if you need the directory, use System.IO.Path.GetDirectoryName on that result.

Or, there's the shorter Application.ExecutablePath which "Gets the path for the executable file that started the application, including the executable name" so that might mean it's slightly less reliable depending on how the application was launched.

C++ [Windows] Path to the folder where the executable is located

Use GetModuleFileName to find out where your exe is running from.

WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);

Then strip the exe name from path.

how to find the location of the executable in C in OS X

Mac OS X implements the functions:

int _NSGetExecutablePath(char * buf, uint32_t * length);
int * _NSGetArgc(void);
char *** _NSGetArgv(void);

The first of these is probably what you want here. The other two are interesting as well, though!

Get path of executable

There is no cross platform way that I know.

For Linux: pass "/proc/self/exe" to std::filesystem::canonical or readlink.

Windows: pass NULL as the module handle to GetModuleFileName.



Related Topics



Leave a reply



Submit