Crates.io | getfn |
lib.rs | getfn |
version | 0.2.0 |
source | src |
created_at | 2021-02-22 23:44:44.890054 |
updated_at | 2023-11-03 16:10:42.837025 |
description | A utility crate for generating function pairs to refer to functions via custom expressions. |
homepage | |
repository | https://github.com/meltah/getfn |
max_upload_size | |
id | 359222 |
size | 26,688 |
Utilities for referring to functions by pointer.
Using the getfn!
macro, you can generate a pair of functions: the first
function acts as a "getter" function, and is prefixed with getfn_
. The
second function simply calls the function normally:
use getfn::getfn;
getfn! {
(|| { println!("hi!"); })
fn my_func();
}
my_func(); // hi!
let f = getfn_my_func();
f(); // hi!
You might be wondering why not simply use let f = my_func;
instead of
having a separate getter. The reason is that the resulting function pointer
in f
will not have the exact same address as the one passed into getfn!
.
This is necessary in cases like game modding, where to hook a function,
you must have its exact address. To aid these usecases, getfn
also offers
a get_addr!
macro, which is a DSL for getting the address of a function:
// gets the base address of the `MyGame.exe` module using the backend
// specified via features (currently supported are `winapi` and `libc`).
let f = getfn::get_addr!("MyGame.exe" + 0x20bf00);
These can be combined together using the symbol_fn!
macro:
use getfn::symbol_fn;
use std::ffi::c_void;
symbol_fn! {
("MyGame.exe" + 0x20bf00)
extern "C" fn my_game_func(a: i32) -> bool;
}
println!("{}", my_game_func(5));
let func = getfn_my_game_func();
extern "C" fn detour(a: i32) -> bool { a == 5 }
// .. hook `func` ..
let mut orig = std::ptr::null_mut();
MH_CreateHook(func as *mut c_void, detour, &mut orig as *mut _ as _);
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.