Skip to main content

HookHandle

Struct HookHandle 

Source
pub struct HookHandle { /* private fields */ }

Implementations§

Source§

impl HookHandle

Source

pub fn target_ptr(&self) -> *mut c_void

Source

pub fn detour_ptr(&self) -> *mut c_void

Source

pub fn original_ptr(&self) -> *mut c_void

Source

pub unsafe fn original<T: Copy>(&self) -> T

Source

pub unsafe fn unhook(self) -> Result<()>

Examples found in repository?
examples/macros.rs (line 23)
14fn main() -> dobby_rs_framework::Result<()> {
15    common::init_example_logging();
16
17    // EN: `dobby_hook!` installs a hook using function items.
18    // CN: `dobby_hook!` 直接用函数名安装 hook。
19    let h = dobby_rs_framework::dobby_hook!(common::target_add, detour_add)?;
20
21    println!("target_add(1) after dobby_hook = {}", common::target_add(1));
22
23    unsafe { h.unhook()? };
24    Ok(())
25}
More examples
Hide additional examples
examples/symbols_aliases.rs (line 49)
26    pub fn run() -> dobby_rs_framework::Result<()> {
27        let sym = CString::new("puts").unwrap();
28        let img = CString::new(common::LIBC_IMAGE).unwrap();
29
30        // EN: Resolve address and register an alias.
31        // CN: 解析地址并注册 alias。
32        let addr = resolve_and_register_alias("libc_puts", None, sym.as_c_str())?;
33        println!("resolved puts addr = {addr:p}");
34
35        // EN: Resolve with explicit image.
36        // CN: 指定 image 名解析。
37        let _ = resolve_and_register_alias_in("libc_puts_in", img.as_c_str(), sym.as_c_str())?;
38        register_alias_with_symbol_in("libc_puts_meta_in", img.as_c_str(), sym.as_c_str(), addr)?;
39
40        // EN: Hook by symbol (default image search).
41        // CN: 按 symbol 安装 hook(默认 image 搜索)。
42        let h1 = unsafe {
43            hook_symbol_default(
44                sym.as_c_str(),
45                common::detours::unix::detour_puts as *const () as *mut c_void,
46                Some("puts"),
47            )?
48        };
49        unsafe { h1.unhook()? };
50
51        // EN: Fully generic form.
52        // CN: 完整通用形式。
53        let h2 = unsafe {
54            hook_symbol(
55                None,
56                sym.as_c_str(),
57                common::detours::unix::detour_puts as *const () as *mut c_void,
58                Some("puts2"),
59            )?
60        };
61        unsafe { h2.unhook()? };
62
63        Ok(())
64    }
65}
66
67// EN: --- Windows demo: hook a simple kernel32 API by symbol.
68// CN: --- Windows 演示:按 symbol hook kernel32 的简单 API。
69#[cfg(windows)]
70mod windows_demo {
71    use super::*;
72
73    pub fn run() -> dobby_rs_framework::Result<()> {
74        let image = CString::new("kernel32.dll").unwrap();
75        let sym = CString::new("GetTickCount").unwrap();
76
77        let _ = resolve_and_register_alias_in("k32_get_tick", image.as_c_str(), sym.as_c_str())?;
78        let h = unsafe {
79            hook_symbol_in(
80                image.as_c_str(),
81                sym.as_c_str(),
82                common::detours::windows::detour_get_tick_count as *const () as *mut c_void,
83                Some("GetTickCount"),
84            )?
85        };
86        unsafe { h.unhook()? };
87        Ok(())
88    }
89}
90
91fn main() -> dobby_rs_framework::Result<()> {
92    common::init_example_logging();
93
94    // EN: Alias registry is just a map: alias -> address (+optional metadata).
95    // CN: alias 注册表本质就是:alias -> 地址(以及可选的元信息)。
96    let add_addr = common::target_add as *const () as *mut c_void;
97    register_alias("demo_add", add_addr)?;
98    println!("alias demo_add addr = {:p}", get_alias("demo_add").unwrap());
99
100    // EN: Hook by alias.
101    // CN: 按 alias 安装 hook。
102    let h = unsafe { hook_alias("demo_add", detour_add as *const () as *mut c_void)? };
103    println!("target_add(1) after hook_alias = {}", common::target_add(1));
104    unsafe { h.unhook()? };
105
106    // EN/CN: Optional: store richer alias metadata.
107    let fake_sym = CString::new("target_add").unwrap();
108    register_alias_with_symbol("demo_add_meta", None, fake_sym.as_c_str(), add_addr)?;
109
110    #[cfg(unix)]
111    unix_demo::run()?;
112    #[cfg(windows)]
113    windows_demo::run()?;
114
115    Ok(())
116}
examples/install_replace.rs (line 75)
49fn main() -> dobby_rs_framework::Result<()> {
50    common::init_example_logging();
51
52    // EN: 1) `install` -> typed handle.
53    // CN: 1) `install` -> 返回带类型的句柄。
54    let h1 = unsafe {
55        install(
56            common::target_mul as fn(i32) -> i32,
57            detour_mul as fn(i32) -> i32,
58        )?
59    };
60    println!("target_mul(3) after install = {}", common::target_mul(3));
61    unsafe { h1.unhook()? };
62
63    // EN: 2) `install_addr` -> raw pointer variant.
64    // CN: 2) `install_addr` -> 按地址安装(裸指针版本)。
65    let h1_addr = unsafe {
66        install_addr(
67            common::target_mul as *const () as *mut c_void,
68            detour_mul as *const () as *mut c_void,
69        )?
70    };
71    println!(
72        "target_mul(3) after install_addr = {}",
73        common::target_mul(3)
74    );
75    unsafe { h1_addr.unhook()? };
76
77    // EN: 3) `replace` -> temporary swap, with easy access to original.
78    // CN: 3) `replace` -> 临时替换,并且可以拿到 original。
79    let r = unsafe {
80        replace(
81            common::target_sub as fn(i32) -> i32,
82            replacement_sub as fn(i32) -> i32,
83        )?
84    };
85    println!("target_sub(10) after replace = {}", common::target_sub(10));
86    println!("original target_sub(10) = {}", (r.original())(10));
87    unsafe { r.unreplace()? };
88
89    // EN: 4) `install_with` -> before/after callbacks (invoked by detour).
90    // CN: 4) `install_with` -> before/after 回调(由 detour 调用)。
91    let h2 = unsafe {
92        install_with(
93            target_with_callbacks as fn(i32) -> i32,
94            detour_with_callbacks as fn(i32) -> i32,
95            Some(|| log::info!("before (install_with)")),
96            Some(|| log::info!("after (install_with)")),
97        )?
98    };
99    println!(
100        "target_with_callbacks(1) after install_with = {}",
101        target_with_callbacks(1)
102    );
103    unsafe { h2.unhook()? };
104
105    Ok(())
106}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.