Skip to content

Commit

Permalink
Merge pull request #4 from whs31/feature-current-lib-1
Browse files Browse the repository at this point in the history
Self-viewing library handle
  • Loading branch information
whs31 authored May 27, 2024
2 parents 8e35861 + 18fec7b commit 498721c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
10 changes: 5 additions & 5 deletions examples/read_process_memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
5 changes: 1 addition & 4 deletions include/winapi20/impl/libloaderapi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 16 additions & 3 deletions src/impl/libloaderapi_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,30 @@ 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<LPCSTR>(current_module_handle), &h);
return h;
}
}

namespace winapi::dll
{
Library::~Library() {
if(this->handle() and this->m_cleanup)
::FreeLibrary(this->handle().as<HMODULE>());
}

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)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_libloaderapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit 498721c

Please sign in to comment.