Detour (Hooking)
Thedetour::hook class provides a simple and safe interface for creating function hooks, allowing you to intercept and modify the behavior of game functions.
Overview
Function hooking (detouring) allows you to:- Intercept calls to game functions
- Execute custom code before/after the original function
- Modify function parameters or return values
- Temporarily disable or restore hooks
Class: detour::hook
Constructors
Default Constructor
Parameterized Constructor
Pointer to the function to hook
Pointer to your detour/hook function
The constructor does not automatically activate the hook. Call
replace() to activate it.Methods
create
Pointer to the function to hook
Pointer to your detour/hook function
true if hook was successfully created, false otherwise
replace
true if hook was successfully activated, false otherwise
Call this after
create() to activate the hook. The original function will be backed up automatically.remove
true if hook was successfully removed, false otherwise
restore
true if hook was successfully restored, false otherwise
is_hooked
true if hook is active, false otherwise
get_original
Function pointer type to cast the original function to
Use this within your detour to call the original function. The type
T should match the function signature.Usage Pattern
Basic Hook Example
Advanced Examples
Method Hook with This Pointer
Conditional Hook Activation
Parameter Modification
Return Value Modification
Best Practices
Important Guidelines:
- Always check
is_hooked()before removing hooks to avoid double-free - Store hook objects globally or ensure they outlive the hook lifetime
- Match calling conventions (
__stdcall,__thiscall,__cdecl) exactly - Handle
thispointer correctly for member functions (__thiscall) - Remove hooks on unload to prevent crashes
- Test hooks thoroughly - incorrect hooks can crash the game
- Use pattern scanning to find function addresses dynamically
Common Calling Conventions
Troubleshooting
Hook not working:- Verify function address is correct using a debugger
- Check calling convention matches the original function
- Ensure
replace()was called aftercreate()
- Verify function signature matches exactly
- Check stack alignment and parameter passing
- Ensure you’re calling the original function correctly
- Remove hooks properly on unload
- Verify
get_original<T>()template type matches function signature - Ensure you’re passing all parameters correctly