Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally determine the prefix path based on the location of the DLL and not the executable #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Loading