Skip to content

Commit ad9ad5f

Browse files
committed
wip-module-path
1 parent d11fb6b commit ad9ad5f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

include/xtl/xsystem.hpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@
3939

4040
namespace xtl
4141
{
42+
std::string module_path(bool executable);
4243
std::string executable_path();
43-
std::string prefix_path();
44+
std::string prefix_path(bool executable);
4445

4546
/******************
4647
* implementation *
4748
******************/
4849

4950
inline std::string executable_path()
51+
{
52+
return module_path(false);
53+
}
54+
55+
inline std::string module_path(bool executable = true)
5056
{
5157
std::string path;
5258
#if defined(UNICODE)
@@ -65,16 +71,21 @@ namespace xtl
6571
// failed to determine run path
6672
}
6773
#elif defined (_WIN32)
74+
HMODULE handle = nullptr;
75+
if (!executable)
76+
{
77+
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCTSTR>(GetCurrentModule), &handle);
78+
}
6879
#if defined(UNICODE)
69-
if (GetModuleFileNameW(nullptr, buffer, sizeof(buffer)) != 0)
80+
if (GetModuleFileNameW(handle, buffer, sizeof(buffer)) != 0)
7081
{
7182
// Convert wchar_t to std::string
7283
std::wstring wideString(buffer);
7384
std::string narrowString(wideString.begin(), wideString.end());
7485
path = narrowString;
7586
}
7687
#else
77-
if (GetModuleFileNameA(nullptr, buffer, sizeof(buffer)) != 0)
88+
if (GetModuleFileNameA(handle, buffer, sizeof(buffer)) != 0)
7889
{
7990
path = buffer;
8091
}
@@ -107,9 +118,9 @@ namespace xtl
107118
return path;
108119
}
109120

110-
inline std::string prefix_path()
121+
inline std::string prefix_path(bool executable = true)
111122
{
112-
std::string path = executable_path();
123+
std::string path = module_path(executable);
113124
#if defined (_WIN32)
114125
char separator = '\\';
115126
#else

0 commit comments

Comments
 (0)