Guide
A detailed guide on how to use the plugin effectively, ensuring you maximize its features and functionality for your needs.
Hooking C/C++ Functions Dynamically in Any Language
It is a plugin that enables dynamic runtime hooking of C++ functions — including both virtual function table (vtable) hooks and code detour hooks. It’s based on the powerful polyhook2 backend and is designed to be easily integrated into your plugin or scripting environment.
This guide covers:
- Hooking detour (raw function address) functions
- Hooking virtual functions (by index or by function pointer)
- Registering pre- and post-callbacks
- Inspecting and modifying function arguments and return values
- Unhooking and managing callbacks
Basic Concepts
What is a Hook?
A hook is a method to intercept function calls. You can:
- Inspect/modify function arguments before the call (pre)
- Observe/alter return value after the call (post)
- Override or entirely skip the original function (supercede)
Types of Hooks
HookDetour
: Hooks a standalone or static function.HookVirtual
: Hooks a class method via vtable index or function pointer.
Data Types
Use DataType
enum to describe argument and return types:
Use these consistently when describing function signatures to the hook APIs.
Hooking Functions
1. Detour Hook
Use HookDetour
to hook a global/static function:
pFunc
: Pointer to the function to hook.returnType
: Return type usingDataType
.args
: List of argument types.varIndex
: Use-1
for normal calls; set if variable argument.
Example:
2. Virtual Hook (by index)
Hook a method from an object’s vtable:
Example:
You can also get the vtable index from a function pointer:
Getting the vtable index requires knowledge of the class layout and may not be portable across different compilers or platforms. Use with caution, works only if using C++ or C-like languages.
3. Virtual Hook (by function pointer)
Instead of using vtable index:
Unhooking
UnhookDetour(void* pFunc)
UnhookVirtual(void* pClass, int index)
UnhookVirtualByFunc(void* pClass, void* pFunc)
UnhookAll()
– removes all hooksUnhookAllVirtual(void* pClass)
– removes all vtable hooks for a class
Callback System
1. Register Callback
type
:CallbackType::Pre
orCallbackType::Post
handler
: Function pointer
Handler Signature:
Use this to inspect or modify arguments and returns.
2. ReturnAction
Access Arguments & Return
Getting arguments:
Setting arguments:
Getting return value:
Overriding return:
Full Example
Hook int Add(int a, int b)
and modify result:
Utility Functions
FindDetour
,FindVirtual
,FindVirtualByFunc
: Lookup hooksAreCallbacksRegistered
,IsCallbackRegistered
: IntrospectionGetFunctionAddr
: Get function pointer (possibly detoured)GetOriginalAddr
: Get trampoline/original function
Summary
Action | Function |
---|---|
Hook Detour | HookDetour |
Hook Virtual (Index) | HookVirtual |
Hook Virtual (Func) | HookVirtualByFunc |
Add Callback | AddCallback |
Access Args | GetArgumentX , SetArgumentX |
Access Return | GetReturnX , SetReturnX |
Unhook | UnhookDetour , UnhookVirtual , etc. |
Get Addresses | GetFunctionAddr , GetOriginalAddr |
Use this guide as a reference when developing with your PolyHook-based plugin. Always validate pointer arguments and ensure type safety when casting arguments or return values.