From 443eb2db78050294a7c51c448dbdc0529373234c Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Thu, 30 May 2024 12:30:17 +0200 Subject: [PATCH] wip-module-path --- include/xtl/xsystem.hpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/include/xtl/xsystem.hpp b/include/xtl/xsystem.hpp index 3c75b5e..0fbc64e 100644 --- a/include/xtl/xsystem.hpp +++ b/include/xtl/xsystem.hpp @@ -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) @@ -65,8 +71,13 @@ 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(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); @@ -74,7 +85,7 @@ namespace xtl path = narrowString; } #else - if (GetModuleFileNameA(nullptr, buffer, sizeof(buffer)) != 0) + if (GetModuleFileNameA(handle, buffer, sizeof(buffer)) != 0) { path = buffer; } @@ -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