2013-05-17 19:57:53 +03:00
2013-05-17 19:57:53 +03:00
2013-01-16 02:56:47 +07:00
2013-01-16 02:41:49 +07:00
2013-01-16 02:41:49 +07:00
2013-01-19 00:25:39 +07:00
2013-01-19 00:25:39 +07:00

SubHook is a super-simple library for setting function hooks at run time and it works on both Linux and Windows. Currently only x86 is supported but the API is easily portable to other architectures.

Examples

In the following examples foo is a hypothetical function or function pointer that takes a single argument int x and uses the same calling convention as my_foo (for instance, on x86 most compilers use the cdecl calling convention unless you specify otherwise).

C

#include <stdio.h>
#include <subhook.h>

struct subhook *foo_hook;

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

  printf("foo(%d) called\n", x);
  foo(x);

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

int main() {
  foo_hook = subhook_new();

  /* The "source" is the function that we want to hook. */
  subhook_set_src((void*)foo);

  /* The "destination" is the function that will get called instead of the
   * original function. */
  subhook_set_dst((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);
  
  /* Free the memory when you're done. */
  subhook_free(foo_hook);
}

C++

#include <iostream>
#include <subhook.h>

SubHook foo_hook;

void my_foo(int x) {
  // ScopedRemove removes the specified hook and automatically re-installs it
  // when it goes out of scope (thanks to C++ destructors).
  SubHook::ScopedRemove remove(&foo_hook);

  std::cout << "foo(" << x < ") called" << std::endl;
  foo(x);
}

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

S
Description
No description provided
Readme 263 KiB
Languages
C 61.6%
C++ 21.7%
CMake 13.2%
Assembly 3.5%