Skip to content

Commit

Permalink
Calling functions from any library in system
Browse files Browse the repository at this point in the history
  • Loading branch information
whs31 committed May 26, 2024
1 parent 26849de commit befed4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion include/winapi20/impl/libloaderapi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <winapi20/detail/export.h>
#include <winapi20/detail/definitions.h>
#include <winapi20/detail/template_util.h>
#include <winapi20/detail/cvt.h>
#include <winapi20/wrappers/handle.h>
#include <winapi20/wrappers/pid.h>
#include <winapi20/wrappers/memaddr.h>
Expand Down Expand Up @@ -64,6 +65,6 @@ namespace winapi::dll
private:
std::string m_name;
Handle m_handle;
bool m_cleanup;
bool m_cleanup = false;
};
}
2 changes: 1 addition & 1 deletion include/winapi20/wrappers/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace winapi
{}

template <typename R = pointer_type, typename... T>
inline auto __fastcall operator()(T&&... args) const noexcept -> pointer_type {
inline auto __fastcall operator()(T&&... args) const noexcept -> R {
return ((R (__fastcall*) (T...)) this->m_)(args...);
}

Expand Down
40 changes: 40 additions & 0 deletions tests/test_libloaderapi.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <winapi20/impl/libloaderapi_impl.h>
#include <winapi20/impl/tlhelp32_impl.h>
#include <winapi20/impl/errhandlingapi_impl.h>
#include <winapi20/detail/windows_headers.h>

using namespace std;
using namespace ::testing;
using namespace winapi;
using namespace winapi;
using namespace winapi::th32;

TEST(Library, View)
{
using IncludeFlags = Snapshot::IncludeFlags;

auto snapshot = th32::Snapshot(IncludeFlags::Process, PID::current());
auto pid = snapshot.find_first_by_name<th32::ProcessEntry>("explorer.exe");
EXPECT_TRUE(snapshot.valid());
EXPECT_TRUE(pid.has_value());
EXPECT_FALSE(pid->pid.is_current());

auto snapshot2 = th32::Snapshot(IncludeFlags::Module | IncludeFlags::Process, pid->pid);
auto module = snapshot2.find_first_by_name<th32::ModuleEntry>("kernel32.dll");
EXPECT_TRUE(snapshot2.valid());
EXPECT_TRUE(module.has_value());
EXPECT_TRUE(module->base_address > 0);
EXPECT_TRUE(module->pid == pid->pid);
EXPECT_FALSE(module->name.empty());
EXPECT_FALSE(module->path.empty());
EXPECT_TRUE(module->handle);

auto lib = dll::Library::view("kernel32.dll", pid->pid);
EXPECT_TRUE(lib.has_value());
auto fn = (*lib)["MulDiv"];
EXPECT_TRUE(fn.has_value());
auto res = fn->operator()<int>(10, 2, 5);
EXPECT_EQ(res, ::MulDiv(10, 2, 5));
}

0 comments on commit befed4a

Please sign in to comment.