static_hook_basic/
static_hook_basic.rs1mod common;
5
6use dobby_rs_framework::prelude::*;
7
8static HOOK: StaticHook<fn(i32) -> i32> = StaticHook::new();
11
12#[inline(never)]
13fn detour_add(x: i32) -> i32 {
14 HOOK.call_before();
17
18 let out = (HOOK.original())(x + 100);
21
22 HOOK.call_after();
23
24 out + 10
27}
28
29fn main() -> dobby_rs_framework::Result<()> {
30 common::init_example_logging();
31
32 HOOK.set_before(|| log::info!("before"));
35 HOOK.set_after(|| log::info!("after"));
36
37 unsafe {
38 HOOK.install(
39 common::target_add as fn(i32) -> i32,
40 detour_add as fn(i32) -> i32,
41 )?;
42 }
43
44 let v = common::target_add(1);
45 println!("target_add(1) = {v}");
46
47 unsafe {
48 HOOK.uninstall()?;
49 }
50 Ok(())
51}