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 return std::filesystem::path(exe_path).u8string();
40 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
41 return converter.to_bytes(exe_path);
45 char result[PATH_MAX];
46 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
48 if (count == -1)
throw std::runtime_error(
"Failed to get executable path.");
50 std::string exe_path(result, count);
53 size_t pos = exe_path.find_last_of(
"\\/");
54 if (pos != std::string::npos) {
55 exe_path = exe_path.substr(0, pos);