Skip to content

Instantly share code, notes, and snippets.

@Jacob-Tate
Created December 4, 2019 06:18
Show Gist options
  • Save Jacob-Tate/7b326a086cf3f9d46e32315841101109 to your computer and use it in GitHub Desktop.
Save Jacob-Tate/7b326a086cf3f9d46e32315841101109 to your computer and use it in GitHub Desktop.
Get EXE Location C++
//Returns the absolute path of the executable
std::filesystem::path file_manip::abs_exe_path()
{
#if defined(_MSC_VER)
wchar_t path[FILENAME_MAX] = { 0 };
GetModuleFileNameW(nullptr, path, FILENAME_MAX);
return std::filesystem::path(path);
#else
char path[FILENAME_MAX];
ssize_t count = readlink("/proc/self/exe", path, FILENAME_MAX);
return std::filesystem::path(std::string(path, (count > 0) ? count: 0));
#endif
}
std::filesystem::path file_manip::abs_exe_directory()
{
#if defined(_MSC_VER)
wchar_t path[FILENAME_MAX] = { 0 };
GetModuleFileNameW(nullptr, path, FILENAME_MAX);
return std::filesystem::path(path).parent_path().string();
#else
char path[FILENAME_MAX];
ssize_t count = readlink("/proc/self/exe", path, FILENAME_MAX);
return std::filesystem::path(std::string(path, (count > 0) ? count: 0)).parent_path().string();
#endif
}
@phuesler
Copy link

Thank you for posting this here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment