From 18fec7b8b4c86155735cfc4ad14e14c674ec89b6 Mon Sep 17 00:00:00 2001 From: whs31 <40-ryazancev_dl@users.noreply.gitlab.radar076.ru> Date: Tue, 28 May 2024 02:18:10 +0300 Subject: [PATCH] Self-viewing library handle --- examples/read_process_memory.cc | 10 +++++----- include/winapi20/impl/libloaderapi_impl.h | 5 +---- src/impl/libloaderapi_impl.cc | 19 ++++++++++++++++--- tests/test_libloaderapi.cc | 8 ++++++++ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/examples/read_process_memory.cc b/examples/read_process_memory.cc index e7fea77..03bd3e4 100644 --- a/examples/read_process_memory.cc +++ b/examples/read_process_memory.cc @@ -20,11 +20,11 @@ auto main() -> int // get vector of all modules auto modules = module_snapshot.modules(); // exclude all non-game modules (not containing "dota 2 beta" in path) - modules.erase(std::remove_if( - modules.begin(), - modules.end(), - [](auto& m) { return m.path.string().find("dota 2 beta") == std::string::npos; }), modules.end() - ); +// modules.erase(std::remove_if( +// modules.begin(), +// modules.end(), +// [](auto& m) { return m.path.string().find("dota 2 beta") == std::string::npos; }), modules.end() +// ); fmt::print("listing game modules:\n"); fmt::print("\t{}", fmt::join(modules, "\n\t")); diff --git a/include/winapi20/impl/libloaderapi_impl.h b/include/winapi20/impl/libloaderapi_impl.h index 19c6517..dfbec2a 100644 --- a/include/winapi20/impl/libloaderapi_impl.h +++ b/include/winapi20/impl/libloaderapi_impl.h @@ -33,10 +33,7 @@ namespace winapi::dll WeakReference = 0x00000002 }; - WINAPI20_EXPORT explicit Library( - std::string name, - HandleFlags flags = HandleFlags::None - ) noexcept(false); + WINAPI20_EXPORT explicit Library(OwnershipMode mode, HandleFlags flags = HandleFlags::None) noexcept(false); WINAPI20_EXPORT explicit Library( OwnershipMode mode, diff --git a/src/impl/libloaderapi_impl.cc b/src/impl/libloaderapi_impl.cc index 54242a3..cf635dd 100644 --- a/src/impl/libloaderapi_impl.cc +++ b/src/impl/libloaderapi_impl.cc @@ -13,6 +13,14 @@ using std::optional; using std::nullopt; using std::string_view; +namespace { + [[nodiscard]] auto current_module_handle() -> HMODULE { + auto h = HMODULE(); + ::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast(current_module_handle), &h); + return h; + } +} + namespace winapi::dll { Library::~Library() { @@ -20,10 +28,15 @@ namespace winapi::dll ::FreeLibrary(this->handle().as()); } - Library::Library(std::string name, Library::HandleFlags flags) noexcept(false) + Library::Library(OwnershipMode mode, Library::HandleFlags flags) noexcept(false) + : m_pid(PID::current()) + , m_cleanup(mode == OwnershipMode::Own) { - // get this library handle - + if(auto const h = ::current_module_handle(); h == nullptr) + throw windows_exception("failed to get current module handle"); + else + this->m_handle = Handle(h, Cleanup::Manual); + this->m_name = this->file_path().filename().string(); } Library::Library(std::string name, Handle&& handle, PID pid, bool cleanup) diff --git a/tests/test_libloaderapi.cc b/tests/test_libloaderapi.cc index 4902b90..70cc934 100644 --- a/tests/test_libloaderapi.cc +++ b/tests/test_libloaderapi.cc @@ -36,4 +36,12 @@ TEST(Library, View) auto ntdll = dll::Library(dll::Library::View, "ntdll.dll", PID::current()); auto ntdll_fn = ntdll["NtProtectVirtualMemory"]; EXPECT_TRUE(ntdll_fn.has_value()); +} + +TEST(Library, Self) +{ + auto lib = dll::Library(dll::Library::View); + + EXPECT_EQ(lib.file_path().filename().string(), "libwinapi20.dll"); + fmt::print("{}\n", lib.file_path().string()); } \ No newline at end of file