Microsoft's Detours Library
Detours is a software package for re-routing Win32 APIs underneath applications.
Last updated
Detours is a software package for re-routing Win32 APIs underneath applications.
Last updated
The Detours library replaces the first few instructions of the function we are hooking with a jump instruction to the user-provided detour function. This jump is called a trampoline.
The library uses transactions to install and uninstall hooks from the targeted function.
When using any hooking method, the first step is to get the memory address of the function to be hooked. Refer back to previous sections on custom GetProcAddress
, GetModuleHandle
functions to retrieve the memory address.
Next we create a function to replace the function we are hooking. The replacement function should be the same datatype, and preferably take the same parameters.
IMPORTANT: To use the Detours library's functions, the Detours repository must be downloaded and compiled to get the static library files (.lib) files needed for the compilation. In addition to that the header file should be included, this is explained in the Detours wiki under the section.
For additional help adding .lib files to a project, review .
- Begin a new transaction for attaching or detaching detours. This function should be called first when hooking and unhooking.
- Update the current transaction. This is used by Detours library to Enlist a thread in the current transaction.
- Install the hook on the target function in a current transaction. This won't be committed until DetourTransactionCommit
is called.
- Remove the hook from the targeted function in a current transaction. This won't be committed until DetourTransactionCommit
is called.
- Commit the current transaction for attaching or detaching detours.