diff --git a/src/misc.cpp b/src/misc.cpp index dc6dee47f..27762ad4b 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -577,12 +577,15 @@ bool is_whitespace(std::string_view s) { fs::path CommandLine::get_binary_directory(fs::path argv0) { #ifdef _WIN32 - #ifdef _MSC_VER - // Prefer the executable path reported by the CRT when available. - wchar_t* pgmptr = nullptr; - if (!_get_wpgmptr(&pgmptr) && pgmptr != nullptr && *pgmptr) - argv0 = fs::path(pgmptr); - #endif + // Prefer the executable path reported by Windows. Unlike _get_wpgmptr, + // this does not depend on whether the CRT used a narrow or wide entry + // point. Windows paths cannot exceed 32767 characters, so a fixed + // buffer is always sufficient. Falls back to argv0 if the API fails. + constexpr DWORD MaxPath = 32768; + wchar_t path[MaxPath]; + + if (const DWORD length = GetModuleFileNameW(nullptr, path, MaxPath)) + argv0 = fs::path(path, path + length); #endif auto binaryDirectory = argv0.parent_path();