Crate getfn

Source
Expand description

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
// (currently assumed to be `winapi` accessible from `crate::__getfn_winapi_`)
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 _);

Re-exports§

pub use paste;

Macros§

alias
def_alias
get_addr
DSL for getting addresses
getfn
symbol_fn
symbol_static

Structs§

SymbolStatic