/// <summary> /// 安装勾子 /// <summary> /// <param name="hookProcess">外部调用的键盘处理事件</param> public void InstallHook(ProcessKeyHandle clientMethod) { _clientMethod = clientMethod; // 安装键盘钩子 if (_hHookValue == 0) { _KeyBoardHookProcedure = new HookHandle(GetHookProc); _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); _hHookValue = SetWindowsHookEx( WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0); //如果设置钩子失败. if (_hHookValue == 0) { UninstallHook(); } } }
/// <summary> /// Releases the keyboard hook. /// </summary> public void Unhook() { if ((HookHandle.IsInvalid == false) && (HookHandle.IsClosed == false)) { HookHandle.Close(); } }
protected override HookHandle Subscribe() { return(HookHandle.Create( HookType.AppMouse, HookProcedure, IntPtr.Zero, ThreadNativeMethods.GetCurrentThreadId())); }
protected override HookHandle Subscribe() { return(HookHandle.Create( HookType.GlobalMouse, HookProcedure, System.Diagnostics.Process.GetCurrentProcess().MainModule.BaseAddress, 0)); }
/// <summary> /// 注册钩子 /// </summary> protected void RegistHook() { if (hHookValue == 0 || this.HokWindowPtr != this.GetWindowPtr()) { this.keyBoardHookProcedure = new HookHandle(OnHookProc); GCHandle.Alloc(this.keyBoardHookProcedure, GCHandleType.Normal); HokWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); hHookValue = SetWindowsHookEx(WH_KEYBOARD_LL, this.keyBoardHookProcedure, HokWindowPtr, 0); } }
public void UnHook() { if (this.hookHandle != null) { this.hookHandle.Close(); } this.hookHandle = null; this.hookProcDelegate = null; }
public static void Demo() { var target = typeof(Random).GetConstructor(new[] { typeof(int) }); var subst = typeof(ConstructorExample).GetMethod("CtorSubst"); using (_handle = Intercept.On(target, subst)) { var rnd = new Random(4); var num = rnd.Next(); // returns 0 Console.WriteLine(); } }
public static void Demo() { var target = typeof(DirectoryInfo).GetProperty("Exists").GetGetMethod(); var subst = typeof(InstancePropertyGetterExample).GetMethod("Exists_Subst"); using (_handle = Intercept.On(target, subst)) { var di = new DirectoryInfo("c:\\"); // Exists getter call will be replaced with Exists_Subst call var r = di.Exists; // r == false } }
public static void Demo() { var target = typeof(Environment).GetMethod("GetEnvironmentVariable", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, null); var subst = typeof(StaticMethodsExample).GetMethod("GetEnvironmentVariable_Subst"); using (_handle = Intercept.On(target, subst)) { var r = Environment.GetEnvironmentVariable("test"); Console.WriteLine("Replaced return value: " + r); Console.WriteLine(); } }
#pragma warning restore 169 public static void Demo() { var target = typeof(DateTime).GetMethod("ToShortDateString"); var subst = typeof(StructMethodsExample).GetMethod("ToShortDateString_Subst"); using (_handle = Intercept.On(target, subst)) { var date = DateTime.Now; var str = date.ToShortDateString(); Console.WriteLine("Replaced result: " + str); Console.WriteLine(); } }
public void Register() { _hookHandle = NativeMethods.SetWindowsHookEx(HookType.LowLevelKeyboardHook, _hookProcedureFunction, IntPtr.Zero, 0); if (_hookHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to register low-level keyboard hook."); } _mouseHookHandle = NativeMethods.SetWindowsHookEx(HookType.LowLevelMouseHook, _hookProcedureFunction, IntPtr.Zero, 0); if (_mouseHookHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to register low-level mouse hook."); } }
public void Hook() { var source = HookNativeMethods.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); this.hookProcDelegate = new HookProc(this.OnHookCall); this.hookHandle = HookNativeMethods.SetWindowsHookEx((int)this.hookId, hookProcDelegate, source, 0); if (this.hookHandle.IsInvalid) { var errorCode = Marshal.GetLastWin32Error(); throw new Win32Exception(errorCode); } }
public void InstallHook(ProcessMouseHandle clientMethod) { _clientMethod = clientMethod; if (_hHookValue == 0) { _MouseHookProcedure = new HookHandle(OnHookProc); _hHookValue = SetWindowsHookEx(WH_MOUSE_LL, _MouseHookProcedure, _hookWindowPtr, 0); if (_hHookValue == 0) { UninstallHook(); } } }
//安装钩子 public void InstallHook() { if (_hHookValue == 0) { _KeyBoardHookProcedure = new HookHandle(OnHookProc); _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); _hHookValue = SetWindowsHookEx(WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0); } if (_hHookValue == 0) { UnhookWindowsHookEx(_hHookValue); } }
public static void Demo() { var target = typeof(UriBuilder).GetProperty("Host").GetSetMethod(); var subst = typeof(InstancePropertySetterExample).GetMethod("Host_Subst"); using (_handle = Intercept.On(target, subst)) { var builder = new UriBuilder(); builder.Scheme = "http"; builder.Host = "localhost"; Console.WriteLine("Result: " + builder); Console.WriteLine(); } }
public void InstallHook() { if (_hHookValue == 0) { _KeyBoardHookProcedure = new HookHandle(OnHookProc); _hHookValue = SetWindowsHookEx( WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0); System.Diagnostics.Debug.WriteLine("install keyboard hook:" + _hHookValue); } }
static public void HookRestart(HookHandle hh) { HookClear(); hookHandle = hh; hookState = true; if (hHook == 0) { KeyBoardHookProcedure = KeyBoardHookProc; hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0); if (hHook == 0) { HookClear(); } } }
/// <summary> /// Attaches the hook. /// </summary> public void Attach() { if (this.IsAttached) { return; } lock (this.SyncRoot) { if (this.IsAttached) { return; } this.handle = SafeNativeMethods.InitHook(this.HookId, this.callback); } }
/// <summary> /// 安装勾子 /// </summary> /// <param name="hookProcess">外部调用的键盘处理事件</param> public void InstallHook(调用端函数 clientMethod) { 调用函数引用 = clientMethod; // 安装键盘钩子 if (是否以安装 == 0) { 执行函数引用 = new HookHandle(执行函数); 进程块 = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); 是否以安装 = SetWindowsHookEx(WH_KEYBOARD_LL, 执行函数引用, 进程块, 0); //如果设置钩子失败. if (是否以安装 == 0) { UninstallHook(); } } }
/// <summary> /// Detaches the hook. /// </summary> public void Detach() { if (!this.IsAttached) { return; } lock (this.SyncRoot) { if (!this.IsAttached) { return; } bool released = SafeNativeMethods.ReleaseHook(this.handle); if (released) { this.handle = null; } } }
public static ObjectNetworkedProperties Hook(object Obj, SetOverrideAction SetOverride) { PropertyInfo[] Props = Obj.GetType().GetProperties().Where((P) => P.GetCustomAttribute(typeof(NetworkedAttribute)) != null).ToArray(); ObjectNetworkedProperties NetworkedProps = new ObjectNetworkedProperties(); TypeBuilder TypeBuilder = ModBuilder.DefineType("Overrides" + Rnd.Next().ToString() + Rnd.Next().ToString(), TypeAttributes.Public); string[] PropOverrideNames = new string[Props.Length]; for (int i = 0; i < Props.Length; i++) { PropOverrideNames[i] = "_" + Rnd.Next().ToString() + Rnd.Next().ToString(); MethodBuilder MethBuilder = TypeBuilder.DefineMethod(PropOverrideNames[i], MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(object), Props[i].PropertyType }); FieldInfo OverridesField = typeof(PropertyHooks).GetField(nameof(Overrides)); PropertyInfo OverridesFieldIndexer = OverridesField.FieldType.GetProperties().Where((P) => P.GetIndexParameters().Length > 0).FirstOrDefault(); MemberExpression OverrideMember = Expression.MakeMemberAccess(null, OverridesField); IndexExpression OverrideVal = Expression.MakeIndex(OverrideMember, OverridesFieldIndexer, new Expression[] { Expression.Constant(Overrides.Count + i) }); ParameterExpression This = Expression.Parameter(typeof(object), "This"); ParameterExpression Val = Expression.Parameter(Props[i].PropertyType, "Val"); UnaryExpression ValAsObject = Expression.Convert(Val, typeof(object)); MethodCallExpression SetOverrideCall = Expression.Call(SetOverride.GetMethodInfo(), This, ValAsObject, OverrideVal); Expression.Lambda(SetOverrideCall, This, Val).CompileToMethod(MethBuilder); } Type OverridesType = TypeBuilder.CreateType(); for (int i = 0; i < Props.Length; i++) { MethodInfo SetMethodInfo = Props[i].GetSetMethod(true); Overrides.Add(new PropertyOverride(HookHandle.CreateHook(SetMethodInfo, OverridesType.GetMethod(PropOverrideNames[i])).Hook(), NetworkedProps.Add(Props[i]), Obj)); } return(NetworkedProps); }
public bool Equals(Crane other) { if (other == null) { return(false); } return(Handle.Equals(other.Handle) && HookHandle.Equals(other.HookHandle) && AudioHandle.Equals(other.AudioHandle) && PickupX1.Equals(other.PickupX1) && PickupX2.Equals(other.PickupX2) && PickupY1.Equals(other.PickupY1) && PickupY2.Equals(other.PickupY2) && DropoffTarget.Equals(other.DropoffTarget) && DropoffHeading.Equals(other.DropoffHeading) && PickupAngle.Equals(other.PickupAngle) && DropoffAngle.Equals(other.DropoffAngle) && PickupDistance.Equals(other.PickupDistance) && DropoffDistance.Equals(other.DropoffDistance) && PickupHeight.Equals(other.PickupHeight) && DropoffHeight.Equals(other.DropoffHeight) && HookAngle.Equals(other.HookAngle) && HookDistance.Equals(other.HookDistance) && HookHeight.Equals(other.HookHeight) && HookInitialPosition.Equals(other.HookInitialPosition) && HookCurrentPosition.Equals(other.HookCurrentPosition) && HookVelocity.Equals(other.HookVelocity) && VehiclePickedUpHandle.Equals(other.VehiclePickedUpHandle) && TimeForNextCheck.Equals(other.TimeForNextCheck) && Status.Equals(other.Status) && State.Equals(other.State) && VehiclesCollected.Equals(other.VehiclesCollected) && IsCrusher.Equals(other.IsCrusher) && IsMilitaryCrane.Equals(other.IsMilitaryCrane) && WasMilitaryCrane.Equals(other.WasMilitaryCrane) && IsTop.Equals(other.IsTop)); }
public static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);
public PropertyOverride(HookHandle Hook, PropertyInfo Property, object Origin) { this.Property = Property; this.Hook = Hook; this.Origin = Origin; }
private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);
public static extern IntPtr CallNextHookEx(HookHandle idHook, int nCode, IntPtr wParam, IntPtr lParam);