Hi,
just wanted to contribute some code and release the pain of everyone who ever wanted to dynamically generate an interceptor func for a given Delegate.
Basically, detouring a function is like
Code:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int EndSceneDelegate(IntPtr devicePointer);
...
EndSceneDelegate d = Marshal.GetDelegateForFunctionPointer(new IntPtr(0x1234), typeof(EndSceneDelegate));
var notifier = new ReflectionDetourNotifier((t, h) => new RetnHook(new InProcessMemory(), t, h), d);
notifier.DetourCalled += (s, e) => { foreach (var p in e.Parameters) Console.WriteLine(p.ToString()); };
notifier.Hook.Apply();
It boils down to just pass in a Delegate to an unmanaged function and then receiving event notifications.
For anyone who's wondering of those factory funcs passed in to the constructor... read about TDD.
Actually the chosen names are really bad... so a short legend for those concepts (or at least how I use them):
Hooking is the interception of control flow with a given function with the same signature. It does not necessarily call or simulate the behavior of the original.
A detour is a special hook that returns control flow to the original function after doing its work (a redirection).
A detour notifier is just a little extension to the detour concept, which defines 'doing its work' as firing up an event. Through the use of this event model, one can intercept multiple functions, which may even differ in signature, with one generic event handler.
E.g.: Decide to detour EndScene or Present based on the used DirectX version and intercept with the same signature agnostic handler.
Credits go to Apoc's WhiteMagic which is the predecessor of this module.
http://github.com/Bananenbrot/BananaHook
v0.1: some refactoring and stuff... added Int3Hook via VEH as an example for extensibility