#hook #winapi #detour #windows

minhook

A Rust wrapper for MinHook, a minimalistic x86/x64 API hooking library for Windows

10 releases (6 breaking)

0.6.0 Sep 10, 2024
0.5.0 Mar 16, 2024
0.4.3 Mar 16, 2024
0.3.0 Apr 16, 2023

#37 in Windows APIs

Download history 143/week @ 2024-07-29 20/week @ 2024-08-05 9/week @ 2024-08-12 20/week @ 2024-08-19 1/week @ 2024-08-26 69/week @ 2024-09-02 216/week @ 2024-09-09 163/week @ 2024-09-16 106/week @ 2024-09-23 95/week @ 2024-09-30 65/week @ 2024-10-07 401/week @ 2024-10-14 359/week @ 2024-10-21 233/week @ 2024-10-28 357/week @ 2024-11-04 2263/week @ 2024-11-11

3,500 downloads per month
Used in 2 crates (via open-coroutine-hook)

Custom license

270KB
5.5K SLoC

Visual Studio Project 3.5K SLoC C 1.5K SLoC // 0.1% comments Rust 332 SLoC // 0.0% comments Visual Studio Solution 322 SLoC Shell 2 SLoC Batch 1 SLoC

minhook

Rust Crates.io rustdoc

A Rust wrapper for the MinHook library.

Usage

Add this to your Cargo.toml:

[dependencies]
minhook = "0.6.0"

Example

This example shows how to create a hook for a function, and also call the original function.

use minhook::{MinHook, MH_STATUS};

fn main() -> Result<(), MH_STATUS> {
    // Create a hook for the return_0 function, detouring it to return_1
    let return_0_address = unsafe { MinHook::create_hook(return_0 as _, return_1 as _)? };

    // Enable the hook
    unsafe { MinHook::enable_all_hooks()? };

    // Call the detoured return_0 function, it should return 1
    assert_eq!(return_0(), 1);

    // Transmute the original return_0 function address to a function pointer
    let return_0_original = unsafe { std::mem::transmute::<_, fn() -> i32>(return_0_address) };

    // Call the original return_0 function
    assert_eq!(return_0_original(), 0);

    Ok(())
}

fn return_0() -> i32 {
    0
}

fn return_1() -> i32 {
    1
}

License

This project is licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT).

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MIT license, shall be licensed as above, without any additional terms or conditions.

Dependencies

~350–780KB
~14K SLoC