From 2f21d4a7a1f63e7f29dc73275b44deb1852b4e99 Mon Sep 17 00:00:00 2001 From: nekosu Date: Sun, 2 Nov 2025 23:49:02 +0800 Subject: [PATCH] feat: get_library_path --- include/MaaUtils/Runtime.h | 1 + include/MaaUtils/SafeWindows.hpp | 1 + source/Runtime/Runtime_Posix.cpp | 10 ++++++++++ source/Runtime/Runtime_Win.cpp | 12 ++++++++++++ 4 files changed, 24 insertions(+) diff --git a/include/MaaUtils/Runtime.h b/include/MaaUtils/Runtime.h index 71b213f..e1d22fe 100644 --- a/include/MaaUtils/Runtime.h +++ b/include/MaaUtils/Runtime.h @@ -8,5 +8,6 @@ MAA_NS_BEGIN MAA_UTILS_API const std::filesystem::path& library_dir(); +MAA_UTILS_API std::filesystem::path get_library_path(void* addr); MAA_NS_END diff --git a/include/MaaUtils/SafeWindows.hpp b/include/MaaUtils/SafeWindows.hpp index 9306cef..fd8e531 100644 --- a/include/MaaUtils/SafeWindows.hpp +++ b/include/MaaUtils/SafeWindows.hpp @@ -12,4 +12,5 @@ #include +#include #include diff --git a/source/Runtime/Runtime_Posix.cpp b/source/Runtime/Runtime_Posix.cpp index d37741b..73cbc91 100644 --- a/source/Runtime/Runtime_Posix.cpp +++ b/source/Runtime/Runtime_Posix.cpp @@ -20,6 +20,16 @@ const std::filesystem::path& library_dir() return s_library_dir_cache; } +std::filesystem::path get_library_path(void* addr) +{ + Dl_info dl_info {}; + if (dladdr(addr, &dl_info) == 0) { + return {}; + } + + return { dl_info.dli_fname }; +} + void init_library_dir() { Dl_info dl_info {}; diff --git a/source/Runtime/Runtime_Win.cpp b/source/Runtime/Runtime_Win.cpp index 58e7add..1a1071f 100644 --- a/source/Runtime/Runtime_Win.cpp +++ b/source/Runtime/Runtime_Win.cpp @@ -16,6 +16,18 @@ const std::filesystem::path& library_dir() return s_library_dir_cache; } +std::filesystem::path get_library_path(void* addr) +{ + WCHAR path_buf[MAX_PATH + 5]; + DWORD path_len = GetMappedFileNameW(GetCurrentProcess(), addr, path_buf, MAX_PATH); + + if (path_len == 0) { + return {}; + } + + return { path_buf }; +} + void init_library_dir(HINSTANCE hinstDLL) { WCHAR buffer[MAX_PATH] = { 0 };