#include #include #include #ifdef WIN32 #include #include #include #endif namespace psemek::util { std::filesystem::path executable_path() { #if defined WIN32 std::string result(256, '\0'); while (true) { GetModuleFileNameA(NULL, result.data(), result.size()); auto error = GetLastError(); if (error == ERROR_SUCCESS) break; else if (error == ERROR_INSUFFICIENT_BUFFER) result.resize(result.size() * 2); else throw exception(util::to_string("failed to retrieve executable path: ", std::hex, error)); } return std::filesystem::path(result); #elif defined __linux__ return std::filesystem::canonical("/proc/self/exe"); #else throw exception("executable_path() is not implemented for this platform"); #endif } }