Skip to content

Commit 698a1af

Browse files
committed
Add create hook api and uninitialize integration tests
1 parent f63cc28 commit 698a1af

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

tests/create_hook_api.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use minhook::MinHook;
2+
3+
#[test]
4+
fn test_create_hook_api() {
5+
// Hook get process id function
6+
unsafe {
7+
// Create a hook for the `GetCurrentProcessId` function using the extended API
8+
let original = MinHook::create_hook_api(
9+
"kernel32.dll",
10+
"GetCurrentProcessId",
11+
get_current_process_id_hook as _,
12+
)
13+
.unwrap();
14+
15+
// Grab the current process id
16+
let original_pid = std::process::id();
17+
18+
// Enable all hooks (this is necessary since we do not have a handle to the GetCurrentProcessId function)
19+
MinHook::enable_all_hooks().unwrap();
20+
21+
// Call the Rust std library function to get the current process id
22+
// It should return the value we set in the hook `get_current_process_id_hook`
23+
assert_eq!(std::process::id(), 42);
24+
25+
// Transmute the original function address to a function pointer to make it callable
26+
let original_fn: fn() -> u32 = std::mem::transmute(original);
27+
28+
// Call the original function using the original function pointer
29+
assert_eq!(original_fn(), original_pid);
30+
31+
// Disable the hook
32+
MinHook::disable_all_hooks().unwrap();
33+
34+
// Ensure the original function is restored
35+
assert_eq!(std::process::id(), original_pid);
36+
}
37+
38+
fn get_current_process_id_hook() -> u32 {
39+
42
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use minhook::MinHook;
2+
3+
#[test]
4+
fn test_hook() {
5+
// Minhook will automatically be initialized, so there is no need to ever call initialize().
6+
// However, we can call uninitialize() when we are done with MinHook.
7+
// It is not unsafe to call uninitialize(), even multiple times, but it will only uninitialize MinHook once.
8+
MinHook::uninitialize();
9+
}

0 commit comments

Comments
 (0)