18 std::vector<wchar_t> buffer(MAX_PATH);
19 HMODULE hModule = GetModuleHandle(NULL);
22 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
25 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
26 buffer.resize(buffer.size() * 2);
27 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
30 if (size == 0)
throw std::runtime_error(
"Failed to get executable path.");
31 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
32 std::wstring::size_type pos = exe_path.find_last_of(L
"\\/");
33 if (pos != std::wstring::npos) {
34 exe_path = exe_path.substr(0, pos);
37# if __cplusplus >= 201703L
38 auto utf8_path = std::filesystem::path(exe_path).u8string();
39# if defined(__cpp_char8_t)
40 return std::string(
reinterpret_cast<const char*
>(utf8_path.data()), utf8_path.size());
45 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
46 return converter.to_bytes(exe_path);
50 char result[PATH_MAX];
51 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
53 if (count == -1)
throw std::runtime_error(
"Failed to get executable path.");
55 std::string exe_path(result, count);
58 size_t pos = exe_path.find_last_of(
"\\/");
59 if (pos != std::string::npos) {
60 exe_path = exe_path.substr(0, pos);