From 9113b56d9d1cf0e3dcac3e9e54b109af40b2d24e Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Fri, 21 Nov 2025 13:15:59 +0300 Subject: [PATCH] Allow executable to be at a longer path name on windows --- src/SwitchCWD.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/SwitchCWD.cpp b/src/SwitchCWD.cpp index e42e00e..707aa36 100755 --- a/src/SwitchCWD.cpp +++ b/src/SwitchCWD.cpp @@ -25,9 +25,17 @@ void switch_cwd() { std::filesystem::current_path(std::filesystem::path(newCWD)); #elif _WIN32 - char buffer[4096]; - GetModuleFileName(NULL, buffer, 4096); - std::filesystem::path exePath(buffer); + // https://stackoverflow.com/a/33613252 but UTF-8 + constexpr int MAX_PATH_STEP = 2048; + std::string pathStr; + DWORD copied = 0; + do { + pathStr.resize(pathStr.size() + MAX_PATH_STEP); + copied = GetModuleFileName(NULL, pathStr.data(), pathStr.size()); + } while(copied >= pathStr.size()); + pathStr.resize(copied); + + std::filesystem::path exePath(pathStr); std::filesystem::current_path(exePath.parent_path()); #elif __linux__ std::filesystem::path exePath = std::filesystem::canonical("/proc/self/exe");