C++ [Windows] Path to the Folder Where the Executable Is Located

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 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)

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.

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.

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

Here's code to get the full path to the executing app:

Variable declarations:

char pBuf[256];
size_t len = sizeof(pBuf);

Windows:

int bytes = GetModuleFileName(NULL, pBuf, len);
return bytes ? bytes : -1;

Linux:

int bytes = MIN(readlink("/proc/self/exe", pBuf, len), len - 1);
if(bytes >= 0)
pBuf[bytes] = '\0';
return bytes;

Opening files by relative path on Windows using C

It depends on where the current working directory needs to be when you run each of the test executables. What I would do is use GetModuleFileName to get the location of the executable, then chop off the name of the exe and the bin to get the root directory as an absolute path.

Each executable name can be created by then concatenating the root directorty with (for example) "out\\test1\\test1.exe"

If you need to set the current directory to the test directory, just concatenate "out\\test1" to the root directory and use SetCurrentDirectory to set the current working directoy. Then you can run the executable with just its name e.g. test1.exe



Related Topics



Leave a reply



Submit