Skip to content

Commit

Permalink
wip-module-path
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed May 30, 2024
1 parent d11fb6b commit ad9ad5f
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 @@ -39,14 +39,20 @@

namespace xtl
{
std::string module_path(bool executable);
std::string executable_path();
std::string prefix_path();
std::string prefix_path(bool executable);

/******************
* implementation *
******************/

inline std::string executable_path()
{
return module_path(false);
}

inline std::string module_path(bool executable = true)
{
std::string path;
#if defined(UNICODE)
Expand All @@ -65,16 +71,21 @@ namespace xtl
// failed to determine run path
}
#elif defined (_WIN32)
HMODULE handle = nullptr;
if (!executable)
{
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCTSTR>(GetCurrentModule), &handle);
}
#if defined(UNICODE)
if (GetModuleFileNameW(nullptr, buffer, sizeof(buffer)) != 0)
if (GetModuleFileNameW(handle, buffer, sizeof(buffer)) != 0)
{
// Convert wchar_t to std::string
std::wstring wideString(buffer);
std::string narrowString(wideString.begin(), wideString.end());
path = narrowString;
}
#else
if (GetModuleFileNameA(nullptr, buffer, sizeof(buffer)) != 0)
if (GetModuleFileNameA(handle, buffer, sizeof(buffer)) != 0)
{
path = buffer;
}
Expand Down Expand Up @@ -107,9 +118,9 @@ namespace xtl
return path;
}

inline std::string prefix_path()
inline std::string prefix_path(bool executable = true)
{
std::string path = executable_path();
std::string path = module_path(executable);
#if defined (_WIN32)
char separator = '\\';
#else
Expand Down

0 comments on commit ad9ad5f

Please sign in to comment.