Created
December 4, 2019 06:18
-
-
Save Jacob-Tate/7b326a086cf3f9d46e32315841101109 to your computer and use it in GitHub Desktop.
Get EXE Location C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for posting this here.