Skip to content

Commit

Permalink
Merge pull request #278 from anutosh491/wchar_t
Browse files Browse the repository at this point in the history
Use GetModuleFileNameW for wchar_t array
  • Loading branch information
JohanMabille committed Dec 22, 2023
2 parents b072176 + 212c2b7 commit 6c03d7b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions include/xtl/xsystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6c03d7b

Please sign in to comment.