Skip to main content

inline_hooks_builder/
inline_hooks_builder.rs

1//! EN: Batch install hooks with `inline_hooks(...).install()`.
2//! CN: 使用 `inline_hooks(...).install()` 批量安装 hook。
3
4mod common;
5use dobby_rs_framework::prelude::*;
6
7// EN: Detours are shared via `common::detours`.
8// CN: detour 通过 `common::detours` 共享。
9
10fn main() -> dobby_rs_framework::Result<()> {
11    // EN/CN: Keep this example tiny: install, then uninstall.
12
13    #[cfg(unix)]
14    let builder: InlineHooksBuilder<'_> = inline_hooks(common::DEMO_LIB)
15        .hook_alias(
16            "puts",
17            "puts",
18            common::detours::unix::detour_puts
19                as unsafe extern "C" fn(*const core::ffi::c_char) -> core::ffi::c_int,
20        )
21        .extra_action_fn(|m| common::resolve_and_print(m, "puts"));
22    #[cfg(unix)]
23    let mut session: HookSession = unsafe { builder.install()? };
24
25    #[cfg(windows)]
26    let builder: InlineHooksBuilder<'_> = inline_hooks(common::DEMO_LIB)
27        .hook_alias(
28            "GetTickCount",
29            "GetTickCount",
30            common::detours::windows::detour_get_tick_count as unsafe extern "system" fn() -> u32,
31        )
32        .extra_action_fn(|m| common::resolve_and_print(m, "GetTickCount"));
33    #[cfg(windows)]
34    let mut session: HookSession = unsafe { builder.install()? };
35
36    unsafe { session.unhook_all()? };
37    Ok(())
38}