Wiki

Clone wiki

nanohook / Home

nanohook

nanohook is a library that uses API hooking to allow the user to provide alternative implementations of Win32 and Win64 functions at runtime.

Example: replacing ExitProcess()

This example code replaces ExitProcess, declared in windows.h, so that it says "BYE!!" before exiting the program.

#include <nanohook/nanohook.hpp>
#include <windows.h>
#include <cstdio>

nanohook::hook ExitProcess_hook;

VOID WINAPI ExitProcess_replacement(UINT code)
{
    std::puts("BYE!!");

    // Now we call the original ExitProcess function to actually exit.
    VOID (WINAPI *old)(UINT) = ExitProcess_hook.original();
    old(code);
}

int main()
{
    // Install the hook
    nanohook::hook h(ExitProcess, ExitProcess_replacement);
    ExitProcess_hook.swap(h);

    // Call ExitProcess, which will now invoke our hook, ExitProcess_replacement
    // rather than the original ExitProcess.
    ExitProcess(666);

    return 0;
}

More

Updated