From 212c2b797ec8a6f07352479461f0a37b042949ab Mon Sep 17 00:00:00 2001 From: anutosh491 Date: Fri, 22 Dec 2023 11:10:40 +0530 Subject: [PATCH] Use GetModuleFileNameW for wchar_t array --- include/xtl/xsystem.hpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/include/xtl/xsystem.hpp b/include/xtl/xsystem.hpp index 14cdf3b..3c75b5e 100644 --- a/include/xtl/xsystem.hpp +++ b/include/xtl/xsystem.hpp @@ -49,7 +49,11 @@ namespace xtl inline std::string executable_path() { std::string path; - char buffer[1024]; +#if defined(UNICODE) + wchar_t buffer[1024]; +#else + char buffer[1024]; +#endif std::memset(buffer, '\0', sizeof(buffer)); #if defined(__linux__) if (readlink("/proc/self/exe", buffer, sizeof(buffer)) != -1) @@ -61,14 +65,21 @@ namespace xtl // failed to determine run path } #elif defined (_WIN32) - if (GetModuleFileName(nullptr, buffer, sizeof(buffer)) != 0) + #if defined(UNICODE) + if (GetModuleFileNameW(nullptr, buffer, sizeof(buffer)) != 0) { - path = buffer; + // Convert wchar_t to std::string + std::wstring wideString(buffer); + std::string narrowString(wideString.begin(), wideString.end()); + path = narrowString; } - else + #else + if (GetModuleFileNameA(nullptr, buffer, sizeof(buffer)) != 0) { - // failed to determine run path + path = buffer; } + #endif + // failed to determine run path #elif defined (__APPLE__) std::uint32_t size = sizeof(buffer); if(_NSGetExecutablePath(buffer, &size) == 0)