hook_msgboxa/
hook_msgboxa.rs1use hooking::Hook;
2use std::ffi::CStr;
3
4#[link(name = "user32")]
5unsafe extern "system" {
6 unsafe fn MessageBoxA(
7 hWnd: *mut std::ffi::c_void,
8 lpText: *const i8,
9 lpCaption: *const i8,
10 uType: u32,
11 ) -> i32;
12}
13
14unsafe extern "C" fn hook_destination(
15 _: *mut std::ffi::c_void,
16 lp_text: *const i8,
17 lp_caption: *const i8,
18 _: u32,
19) -> i32 {
20 let original_msgbox: extern "C" fn(*mut std::ffi::c_void, *const i8, *const i8, u32) -> i32 = unsafe {
21 std::mem::transmute(
22 hooking::original_function_ptr()
23 .expect("invoked from hook")
24 .as_ptr(),
25 )
26 };
27
28 println!(
29 "Called with title: {:?} | Body: {:?}",
30 unsafe { CStr::from_ptr(lp_text) },
31 unsafe { CStr::from_ptr(lp_caption) }
32 );
33
34 original_msgbox(
35 std::ptr::null_mut(),
36 c"msgbox was hooked!".as_ptr(),
37 c"Intercepted hook".as_ptr(),
38 0,
39 )
40}
41
42fn main() {
43 let mut hook = unsafe {
44 Hook::by_name(
45 Some(c"user32.dll"),
46 c"MessageBoxA",
47 hook_destination as *mut _,
48 )
49 .unwrap()
50 };
51
52 println!("Applying hook");
53
54 unsafe {
55 hook.apply_hook().unwrap();
56 MessageBoxA(
57 std::ptr::null_mut(),
58 c"Am i hooked?".as_ptr(),
59 c"hooked-rs".as_ptr(),
60 0,
61 );
62 }
63
64 unsafe {
65 hook.remove_hook().unwrap();
66 MessageBoxA(
67 std::ptr::null_mut(),
68 c"Not hooked anymore".as_ptr(),
69 c"hooked-rs".as_ptr(),
70 0,
71 );
72 }
73}