Exemplo n.º 1
0
        /// <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();
     }
 }
Exemplo n.º 3
0
 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));
 }
Exemplo n.º 5
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);
     }
 }
Exemplo n.º 6
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();
            }
        }
Exemplo n.º 11
0
 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.");
     }
 }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
 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();
         }
     }
 }
Exemplo n.º 14
0
 //安装钩子
 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);
     }
 }
Exemplo n.º 15
0
        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();
            }
        }
Exemplo n.º 16
0
        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);
            }
        }
Exemplo n.º 17
0
 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();
         }
     }
 }
Exemplo n.º 18
0
        /// <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);
            }
        }
Exemplo n.º 19
0
        /// <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();
                }
            }
        }
Exemplo n.º 20
0
        /// <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;
                }
            }
        }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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));
        }
Exemplo n.º 23
0
 public static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);
Exemplo n.º 24
0
 public PropertyOverride(HookHandle Hook, PropertyInfo Property, object Origin)
 {
     this.Property = Property;
     this.Hook     = Hook;
     this.Origin   = Origin;
 }
Exemplo n.º 25
0
 private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);
Exemplo n.º 26
0
 public static extern IntPtr CallNextHookEx(HookHandle idHook, int nCode, IntPtr wParam, IntPtr lParam);
Exemplo n.º 27
0
 public static extern IntPtr CallNextHookEx(HookHandle idHook, int nCode, IntPtr wParam, IntPtr lParam);