A simple hooking library for C/C++
Go to file
2013-01-14 00:02:16 +07:00
CMakeLists.txt First commit 2013-01-13 22:15:49 +07:00
README.md Add examples 2013-01-14 00:02:16 +07:00
subhook_linux.c First commit 2013-01-13 22:15:49 +07:00
subhook_private.h First commit 2013-01-13 22:15:49 +07:00
subhook_windows.c First commit 2013-01-13 22:15:49 +07:00
subhook_x86.c First commit 2013-01-13 22:15:49 +07:00
subhook.c First commit 2013-01-13 22:15:49 +07:00
subhook.h First commit 2013-01-13 22:15:49 +07:00

subhook is a super-simple mini-library for setting function hooks at runtime on x86 CPUs and works on both Linux and Windows.

Examples

C

#include <subhook.h>

struct subhook *foo_hook;

void my_foo() {
  /* Sometimes you want to call the original function. */
  subhook_remove(foo_hook);

  printf("foo() called\n");
  foo();

  /* Install the hook back again to intercept further calls. */
  subhook_install(foo_hook);
}

int main() {
  foo_hook = subhook_new();

  /* 'source' is the function that we want to hook. */
  subhook_set_source((void*)foo);

  /* 'destination' is the function that will be called in place
   * of the original function */
  subhook_set_destination((void*)my_foo);

  /* Install our newly created hook so from now on any call to foo()
   * will be redirected to my_foo(). */ 
  subhook_install(foo_hook);
}

C++

#include <subhook.h>

SubHook foo_hook;

void my_foo() {
  SubHook::ScopedRemove(&foo_hook);

  printf("foo() called\n");
  foo();

  /* The hook wll be re-installed automatically upon return. */
}

int main() {
  hook.Install((void*)foo, (void*)my_foo);
}