39
39
40
40
namespace xtl
41
41
{
42
+ std::string module_path (bool executable);
42
43
std::string executable_path ();
43
- std::string prefix_path ();
44
+ std::string prefix_path (bool executable );
44
45
45
46
/* *****************
46
47
* implementation *
47
48
******************/
48
49
49
50
inline std::string executable_path ()
51
+ {
52
+ return module_path (false );
53
+ }
54
+
55
+ inline std::string module_path (bool executable = true )
50
56
{
51
57
std::string path;
52
58
#if defined(UNICODE)
@@ -65,16 +71,21 @@ namespace xtl
65
71
// failed to determine run path
66
72
}
67
73
#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
+ }
68
79
#if defined(UNICODE)
69
- if (GetModuleFileNameW (nullptr , buffer, sizeof (buffer)) != 0 )
80
+ if (GetModuleFileNameW (handle , buffer, sizeof (buffer)) != 0 )
70
81
{
71
82
// Convert wchar_t to std::string
72
83
std::wstring wideString (buffer);
73
84
std::string narrowString (wideString.begin (), wideString.end ());
74
85
path = narrowString;
75
86
}
76
87
#else
77
- if (GetModuleFileNameA (nullptr , buffer, sizeof (buffer)) != 0 )
88
+ if (GetModuleFileNameA (handle , buffer, sizeof (buffer)) != 0 )
78
89
{
79
90
path = buffer;
80
91
}
@@ -107,9 +118,9 @@ namespace xtl
107
118
return path;
108
119
}
109
120
110
- inline std::string prefix_path ()
121
+ inline std::string prefix_path (bool executable = true )
111
122
{
112
- std::string path = executable_path ( );
123
+ std::string path = module_path (executable );
113
124
#if defined (_WIN32)
114
125
char separator = ' \\ ' ;
115
126
#else
0 commit comments