Skip to content

Hooking

BlueShank edited this page Sep 1, 2026 · 2 revisions

cc_hooking is a minimal, dependency-free, cross-platform inline hook library (x86 + x64), used internally for the engine detours (see Patcher).

  • x86: 5-byte jmp rel32
  • x64: 14-byte jmp [rip+0] + abs64

API

All functions live in the CrashCapture::Hook namespace:

  • Hook::Install(void* target, void* detour, void** trampoline): bool
    • hook target so calls land in detour instead.
    • The original instructions are kept in a trampoline and its address is written through trampoline, so the detour can call the original.
    • Returns false if the hook could not be created.
  • Hook::Uninstall(void* target): bool
    • remove the hook, restoring the function's original bytes.
  • Hook::RemoveAll(): void
    • uninstall every hook installed through this library.
  • Hook::Count(): int
    • how many hooks are currently installed.

Example

Always make sure the detour has the right signature for the target:

#include "tools/cc_hooking.h"

// the target's real signature
typedef void (*target_func)(void* self, int a);

// this will store the original via trampoline
static target_func original_func = 0;

static void detour_func(void* self, int a) {
    // do what you want here
    original_func(self, a);
}

void hook_it(void* target_ptr)
{
    // this will attempt to create the hook, returns false if it couldn't
    if (!CrashCapture::Hook::Install(target_ptr, (void*)detour_func, (void**)&original_func)) {
        // site not hookable, handle it
    }
}

void unhook_it(void* target_ptr)
{
    // this will remove the hook, restoring the function
    CrashCapture::Hook::Uninstall(target_ptr);
}

Notes

  • The detour must match the target's calling convention and arguments exactly; the trampoline gives you the untouched original to forward to.
  • Uninstall hooks before your code unloads so the target never calls into freed memory.
  • Hooks installed by the plugin itself are all removed on shutdown (RemoveAll).

Clone this wiki locally