public static void Run() { DMethodA MethodADelegate = new DMethodA(MethodA); DMethodB MethodBDelegate = new DMethodB(MethodB); GC.KeepAlive(MethodADelegate); GC.KeepAlive(MethodBDelegate); LHTestMethodA = Marshal.GetFunctionPointerForDelegate(MethodADelegate); LHTestMethodB = Marshal.GetFunctionPointerForDelegate(MethodBDelegate); LocalHook.EnableRIPRelocation(); // install hooks LocalHook[] MyHooks = new LocalHook[] { LocalHook.Create( LHTestMethodA, LHTestHookA, 1), LocalHook.Create( LHTestMethodB, LHTestHookB, 2), }; LHTestMethodADelegate = (DMethodA)Marshal.GetDelegateForFunctionPointer(LHTestMethodA, typeof(DMethodA)); LHTestMethodBDelegate = (DMethodB)Marshal.GetDelegateForFunctionPointer(LHTestMethodB, typeof(DMethodB)); // we want to intercept all threads... MyHooks[0].ThreadACL.SetInclusiveACL(new Int32[1]); MyHooks[1].ThreadACL.SetInclusiveACL(new Int32[1]); // LHTestMethodBDelegate.Invoke(0, 0, ""); MyHooks[0].ThreadACL.SetExclusiveACL(new Int32[1]); MyHooks[1].ThreadACL.SetExclusiveACL(new Int32[1]); // LHTestMethodBDelegate.Invoke(0, 0, ""); /* * This is just to make sure that all related objects are referenced. * At the beginning there were several objects like delegates that have * been collected during execution! The NET-Framework will produce bugchecks * in such cases... */ GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); IntPtr t = Marshal.GetFunctionPointerForDelegate(LHTestHookA); Int64 t1 = System.Diagnostics.Stopwatch.GetTimestamp(); for (int i = 0; i < LHTestThreadCount; i++) { new Thread(new ThreadStart(LHTestThread)).Start(); } LHTestCompleted.WaitOne(); t1 = ((System.Diagnostics.Stopwatch.GetTimestamp() - t1) * 1000) / System.Diagnostics.Stopwatch.Frequency; // verify results if ((LHTestCounterMA != LHTestCounterMAH) || (LHTestCounterMAH != LHTestCounterMB) || (LHTestCounterMB != LHTestCounterMBH) || (LHTestCounterMB != LHTestThreadCount * 10000)) { throw new Exception("LocalHook test failed."); } Console.WriteLine("Localhook test passed in {0} ms.", t1); }
public static void Run() { DMethodA MethodADelegate = new DMethodA(MethodA); DMethodB MethodBDelegate = new DMethodB(MethodB); GC.KeepAlive(MethodADelegate); GC.KeepAlive(MethodBDelegate); LHTestMethodA = Marshal.GetFunctionPointerForDelegate(MethodADelegate); LHTestMethodB = Marshal.GetFunctionPointerForDelegate(MethodBDelegate); LocalHook.EnableRIPRelocation(); // install hooks LocalHook[] MyHooks = new LocalHook[] { LocalHook.Create( LHTestMethodA, LHTestHookA, 1), LocalHook.Create( LHTestMethodB, LHTestHookB, 2), }; LHTestMethodADelegate = (DMethodA)Marshal.GetDelegateForFunctionPointer(LHTestMethodA, typeof(DMethodA)); LHTestMethodBDelegate = (DMethodB)Marshal.GetDelegateForFunctionPointer(LHTestMethodB, typeof(DMethodB)); // we want to intercept all threads... MyHooks[0].ThreadACL.SetInclusiveACL(new Int32[1]); MyHooks[1].ThreadACL.SetInclusiveACL(new Int32[1]); // LHTestMethodBDelegate.Invoke(0, 0, ""); MyHooks[0].ThreadACL.SetExclusiveACL(new Int32[1]); MyHooks[1].ThreadACL.SetExclusiveACL(new Int32[1]); // LHTestMethodBDelegate.Invoke(0, 0, ""); /* * This is just to make sure that all related objects are referenced. * At the beginning there were several objects like delegates that have * been collected during execution! The NET-Framework will produce bugchecks * in such cases... */ GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); IntPtr t = Marshal.GetFunctionPointerForDelegate(LHTestHookA); Int64 t1 = System.Diagnostics.Stopwatch.GetTimestamp(); for (int i = 0; i < LHTestThreadCount; i++) { new Thread(new ThreadStart(LHTestThread)).Start(); } LHTestCompleted.WaitOne(); t1 = ((System.Diagnostics.Stopwatch.GetTimestamp() - t1) * 1000) / System.Diagnostics.Stopwatch.Frequency; // verify results if ((LHTestCounterMA != LHTestCounterMAH) || (LHTestCounterMAH != LHTestCounterMB) || (LHTestCounterMB != LHTestCounterMBH) || (LHTestCounterMB != LHTestThreadCount * 10000)) throw new Exception("LocalHook test failed."); Console.WriteLine("Localhook test passed in {0} ms.", t1); }