/// <summary> /// Restores <see cref="HookedMethod"/> to its previous state. /// </summary> public void Dispose() { var functionPointer = HookedMethod.MethodHandle.GetFunctionPointer(); using (MemoryManagement.Protect(functionPointer, (uint)_originalIntro.Length, MemProtection.ExecReadWrite)) { Marshal.Copy(_originalIntro, 0, functionPointer, _originalIntro.Length); } }
public HookHandle(IntPtr hookedFunc, IntPtr newFunc) { byte[] NewIntro = new byte[] { 0xE9, 0x90, 0x90, 0x90, 0x90, 0xC3 }; Array.Copy(BitConverter.GetBytes(newFunc.ToInt32() - hookedFunc.ToInt32() - 5), 0, NewIntro, 1, 4); byte[] OrigIntro; using (MemoryManagement.Protect(hookedFunc, NewIntro.Length, MemProtection.ExecReadWrite)) { OrigIntro = new byte[NewIntro.Length]; Marshal.Copy(hookedFunc, OrigIntro, 0, OrigIntro.Length); } Init(hookedFunc, OrigIntro, NewIntro); }
public static byte[] Hook(IntPtr OldFunc, IntPtr NewFunc) { byte[] JMP = new byte[] { 0xE9, 0x90, 0x90, 0x90, 0x90, 0xC3 }; Array.Copy(BitConverter.GetBytes(NewFunc.ToInt32() - OldFunc.ToInt32() - 5), 0, JMP, 1, 4); using (MemoryManagement.Protect(OldFunc, (uint)JMP.Length, MemProtection.ExecReadWrite)) { byte[] Orig = new byte[JMP.Length]; Marshal.Copy(OldFunc, Orig, 0, Orig.Length); Marshal.Copy(JMP, 0, OldFunc, JMP.Length); return(Orig); } }
void WriteIntro(byte[] Intro) { using (MemoryManagement.Protect(HookedFunc, Intro.Length, MemProtection.ExecReadWrite)) { Marshal.Copy(Intro, 0, HookedFunc, Intro.Length); } }