Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>(module_path), &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