Exemplo n.º 1
0
        /// <summary>
        /// This is a generic wndproc. It is the callback for all hooked
        /// windows. If we get into this function, we look up the hwnd in the
        /// global list of all hooked windows to get its message map. If the
        /// message received is present in the message map, its callback is
        /// invoked with the parameters listed here.
        /// </summary>
        /// <param name="hwnd">The handle to the window that received the
        /// message</param>
        /// <param name="msg">The message</param>
        /// <param name="wParam">The message's parameters (part 1)</param>
        /// <param name="lParam">The messages's parameters (part 2)</param>
        /// <returns>If the callback handled the message, the callback's return
        /// value is returned form this function. If the callback didn't handle
        /// the message, the message is forwarded on to the previous wndproc.
        /// </returns>
        private static int WindowProc(
            IntPtr hwnd, uint msg, uint wParam, int lParam)
        {
            if (hwndDict.ContainsKey(hwnd))
            {
                HookedProcInformation hpi = hwndDict[hwnd];
                if (hpi.messageMap.ContainsKey(msg))
                {
                    WndProcCallback callback = hpi.messageMap[msg];
                    bool            handled  = false;
                    int             retval   = callback(hwnd, msg, wParam, lParam, ref handled);
                    if (handled)
                    {
                        return(retval);
                    }
                }

                // if we didn't hook the message passed or we did, but the
                // callback didn't set the handled property to true, call
                // the original window procedure
                return(hpi.CallOldWindowProc(hwnd, msg, wParam, lParam));
            }

            System.Diagnostics.Debug.Assert(
                false, "WindowProc called for hwnd we don't know about");
            return(Win32.DefWindowProc(hwnd, msg, wParam, lParam));
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method removes the specified message from the message map for
        /// the specified hwnd.
        /// </summary>
        /// <param name="ctl">The control whose message we are unhooking
        /// </param>
        /// <param name="msg">The message no longer want to hook</param>
        public static void UnhookWndProc(Control ctl, uint msg)
        {
            // look for the HookedProcInformation in the control and hwnd
            // dictionaries
            HookedProcInformation hpi = null;

            if (ctlDict.ContainsKey(ctl))
            {
                hpi = ctlDict[ctl];
            }
            else if (hwndDict.ContainsKey(ctl.Handle))
            {
                hpi = hwndDict[ctl.Handle];
            }

            // if we couldn't find a HookedProcInformation, throw
            if (hpi == null)
            {
                throw new ArgumentException("No hook exists for this control");
            }

            // look for the message we are removing in the messageMap
            if (hpi.messageMap.ContainsKey(msg))
            {
                hpi.messageMap.Remove(msg);
            }
            else
            {
                // if we couldn't find the message, throw
                throw new ArgumentException(
                          string.Format(
                              "No hook exists for message ({0}) on this control",
                              msg));
            }
        }
Exemplo n.º 3
0
        public static void UnhookWndProc(Control ctl, bool disposing)
        {
            HookedProcInformation information = null;

            if (ctlDict.ContainsKey(ctl))
            {
                information = ctlDict[ctl];
            }
            else if (hwndDict.ContainsKey(ctl.Handle))
            {
                information = hwndDict[ctl.Handle];
            }
            if (information == null)
            {
                throw new ArgumentException("No hook exists for this control");
            }
            if (ctlDict.ContainsKey(ctl) && disposing)
            {
                ctlDict.Remove(ctl);
            }
            if (hwndDict.ContainsKey(ctl.Handle))
            {
                information.Unhook();
                hwndDict.Remove(ctl.Handle);
                if (!disposing)
                {
                    ctlDict[ctl] = information;
                }
            }
        }
Exemplo n.º 4
0
        // This method removes the specified message from the message map for
        // the specified hwnd.
        public static void UnhookWndProc(Control ctl, uint msg)
        {
            // Look for the HookedProcInformation in the
            // ctrDict and hwndDict dictionaries.
            HookedProcInformation hpi = null;

            if (CtlDict.ContainsKey(ctl))
            {
                hpi = CtlDict[ctl];
            }
            else if (HwndDict.ContainsKey(ctl.Handle))
            {
                hpi = HwndDict[ctl.Handle];
            }
            // if we couldn't find a HookedProcInformation, throw
            if (hpi == null)
            {
                throw new ArgumentException("No hook exists for this control");
            }

            // look for the message we are removing in the messageMap
            if (hpi.MessageMap.ContainsKey(msg))
            {
                hpi.MessageMap.Remove(msg);
            }
            else
            {
                // if we couldn't find the message, throw
                throw new ArgumentException(
                          $"No hook exists for message ({msg}) on this control"
                          );
            }
        }
Exemplo n.º 5
0
        public static void HookWndProc(Control ctl, WndProcCallback callback, uint msg)
        {
            HookedProcInformation information = null;

            if (ctlDict.ContainsKey(ctl))
            {
                information = ctlDict[ctl];
            }
            else if (hwndDict.ContainsKey(ctl.Handle))
            {
                information = hwndDict[ctl.Handle];
            }
            if (information == null)
            {
                information          = new HookedProcInformation(ctl, new Win32.WndProc(WndProcHooker.WindowProc));
                ctl.HandleCreated   += new EventHandler(WndProcHooker.ctl_HandleCreated);
                ctl.HandleDestroyed += new EventHandler(WndProcHooker.ctl_HandleDestroyed);
                ctl.Disposed        += new EventHandler(WndProcHooker.ctl_Disposed);
                if (ctl.Handle != IntPtr.Zero)
                {
                    information.SetHook();
                }
            }
            if (ctl.Handle == IntPtr.Zero)
            {
                ctlDict[ctl] = information;
            }
            else
            {
                hwndDict[ctl.Handle] = information;
            }
            information.messageMap[msg] = callback;
        }
Exemplo n.º 6
0
        private static void ctl_HandleDestroyed(object sender, EventArgs e)
        {
            Control ctl = sender as Control;

            if (hwndDict.ContainsKey(ctl.Handle))
            {
                HookedProcInformation local1 = hwndDict[ctl.Handle];
                UnhookWndProc(ctl, false);
            }
        }
Exemplo n.º 7
0
        private static void ctl_HandleCreated(object sender, EventArgs e)
        {
            Control key = sender as Control;

            if (ctlDict.ContainsKey(key))
            {
                HookedProcInformation information = ctlDict[key];
                hwndDict[key.Handle] = information;
                ctlDict.Remove(key);
                information.SetHook();
            }
        }
Exemplo n.º 8
0
        // Makes a connection between a message on a specified window handle
        // and the callback to be called when that message is received. If the
        // window was not previously hooked it is added to the global list of
        // all the window procedures hooked.
        // Parameters:
        // ctl - The control whose wndproc we are hooking.
        // callback - The method to call when the specified.
        // message is received for the specified window.
        // msg - The message being hooked.
        public static void HookWndProc(
            Control ctl,
            WndProcCallback callback,
            uint msg)
        {
            HookedProcInformation hpi = null;

            if (CtlDict.ContainsKey(ctl))
            {
                hpi = CtlDict[ctl];
            }
            else if (HwndDict.ContainsKey(ctl.Handle))
            {
                hpi = HwndDict[ctl.Handle];
            }
            if (hpi == null)
            {
                // If new control, create a new
                // HookedProcInformation for it.
                hpi = new HookedProcInformation(
                    ctl,
                    WindowProc);
                ctl.HandleCreated   += ctl_HandleCreated;
                ctl.HandleDestroyed += ctl_HandleDestroyed;
                ctl.Disposed        += ctl_Disposed;

                // If the handle has already been created set the hook. If it
                // hasn't been created yet, the hook will get set in the
                // ctl_HandleCreated event handler.
                if (ctl.Handle != IntPtr.Zero)
                {
                    hpi.SetHook();
                }
            }

            // Stick hpi into the correct dictionary.
            if (ctl.Handle == IntPtr.Zero)
            {
                CtlDict[ctl] = hpi;
            }
            else
            {
                HwndDict[ctl.Handle] = hpi;
            }

            // Add the message/callback into the message map.
            hpi.MessageMap[msg] = callback;
        }
Exemplo n.º 9
0
        /// <summary>
        /// The event handler called when a control's handle is created. We
        /// call SetHook() on the associated HookedProcInformation object and
        /// move it from <see>ctlDict</see> to <see>hwndDict</see>.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void ctl_HandleCreated(object sender, EventArgs e)
        {
            Control ctl = sender as Control;

            if (ctlDict.ContainsKey(ctl))
            {
                HookedProcInformation hpi = ctlDict[ctl];
                hwndDict[ctl.Handle] = hpi;
                ctlDict.Remove(ctl);
                hpi.SetHook();
            }
            else
            {
                System.Diagnostics.Debug.Assert(false);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// The event handler called when a control's handle is destroyed.
        /// We remove the HookedProcInformation from <see>hwndDict</see> and
        /// put it back into <see>ctlDict</see> in case the control get re-
        /// created and we still want to hook its messages.
        /// </summary>
        /// <param name="sender">The object that raised this event</param>
        /// <param name="e">The arguments for this event</param>
        static void ctl_HandleDestroyed(object sender, EventArgs e)
        {
            // When the handle for a control is destroyed, we want to
            // unhook its wndproc and update our lists
            Control ctl = sender as Control;

            if (hwndDict.ContainsKey(ctl.Handle))
            {
                HookedProcInformation hpi = hwndDict[ctl.Handle];
                UnhookWndProc(ctl, false);
            }
            else
            {
                System.Diagnostics.Debug.Assert(false);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Makes a connection between a message on a specified window handle
        /// and the callback to be called when that message is received. If the
        /// window was not previously hooked it is added to the global list of
        /// all the window procedures hooked.
        /// </summary>
        /// <param name="ctl">The control whose wndproc we are hooking</param>
        /// <param name="callback">The method to call when the specified
        /// message is received for the specified window</param>
        /// <param name="msg">The message we are hooking.</param>
        public static void HookWndProc(
            Control ctl, WndProcCallback callback, uint msg)
        {
            HookedProcInformation hpi = null;

            if (ctlDict.ContainsKey(ctl))
            {
                hpi = ctlDict[ctl];
            }
            else if (hwndDict.ContainsKey(ctl.Handle))
            {
                hpi = hwndDict[ctl.Handle];
            }
            if (hpi == null)
            {
                // We havne't seen this control before. Create a new
                // HookedProcInformation for it
                hpi = new HookedProcInformation(ctl,
                                                new Win32.WndProc(WndProcHooker.WindowProc));
                ctl.HandleCreated   += new EventHandler(ctl_HandleCreated);
                ctl.HandleDestroyed += new EventHandler(ctl_HandleDestroyed);
                ctl.Disposed        += new EventHandler(ctl_Disposed);

                // If the handle has already been created set the hook. If it
                // hasn't been created yet, the hook will get set in the
                // ctl_HandleCreated event handler
                if (ctl.Handle != IntPtr.Zero)
                {
                    hpi.SetHook();
                }
            }

            // stick hpi into the correct dictionary
            if (ctl.Handle == IntPtr.Zero)
            {
                ctlDict[ctl] = hpi;
            }
            else
            {
                hwndDict[ctl.Handle] = hpi;
            }

            // add the message/callback into the message map
            hpi.messageMap[msg] = callback;
        }
Exemplo n.º 12
0
        private static int WindowProc(IntPtr hwnd, uint msg, uint wParam, int lParam)
        {
            if (!hwndDict.ContainsKey(hwnd))
            {
                return(Win32.DefWindowProc(hwnd, msg, wParam, lParam));
            }
            HookedProcInformation information = hwndDict[hwnd];

            if (information.messageMap.ContainsKey(msg))
            {
                WndProcCallback callback = information.messageMap[msg];
                bool            handled  = false;
                int             num      = callback(hwnd, msg, wParam, lParam, ref handled);
                if (handled)
                {
                    return(num);
                }
            }
            return(information.CallOldWindowProc(hwnd, msg, wParam, lParam));
        }
Exemplo n.º 13
0
        public static void UnhookWndProc(Control ctl, uint msg)
        {
            HookedProcInformation information = null;

            if (ctlDict.ContainsKey(ctl))
            {
                information = ctlDict[ctl];
            }
            else if (hwndDict.ContainsKey(ctl.Handle))
            {
                information = hwndDict[ctl.Handle];
            }
            if (information == null)
            {
                throw new ArgumentException("No hook exists for this control");
            }
            if (!information.messageMap.ContainsKey(msg))
            {
                throw new ArgumentException(string.Format("No hook exists for message ({0}) on this control", msg));
            }
            information.messageMap.Remove(msg);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Restores the previous wndproc for the specified window.
        /// </summary>
        /// <param name="ctl">The control whose wndproc we no longer want to
        /// hook</param>
        /// <param name="disposing">if true we remove don't readd the
        /// HookedProcInformation
        /// back into ctlDict</param>
        public static void UnhookWndProc(Control ctl, bool disposing)
        {
            HookedProcInformation hpi = null;

            if (ctlDict.ContainsKey(ctl))
            {
                hpi = ctlDict[ctl];
            }
            else if (hwndDict.ContainsKey(ctl.Handle))
            {
                hpi = hwndDict[ctl.Handle];
            }

            if (hpi == null)
            {
                throw new ArgumentException("No hook exists for this control");
            }

            // If we found our HookedProcInformation in ctlDict and we are
            // disposing remove it from ctlDict
            if (ctlDict.ContainsKey(ctl) && disposing)
            {
                ctlDict.Remove(ctl);
            }

            // If we found our HookedProcInformation in hwndDict, remove it
            // and if we are not disposing stick it in ctlDict
            if (hwndDict.ContainsKey(ctl.Handle))
            {
                hpi.Unhook();
                hwndDict.Remove(ctl.Handle);
                if (!disposing)
                {
                    ctlDict[ctl] = hpi;
                }
            }
        }
Exemplo n.º 15
0
        // Makes a connection between a message on a specified window handle
        // and the callback to be called when that message is received. If the
        // window was not previously hooked it is added to the global list of
        // all the window procedures hooked.
        // Parameters:
        // ctl - The control whose wndproc we are hooking.
        // callback - The method to call when the specified.
        // message is received for the specified window.
        // msg - The message being hooked.
        public static void HookWndProc(
            Control ctl, WndProcCallback callback, uint msg)
        {
            HookedProcInformation hpi = null;
            if (ctlDict.ContainsKey(ctl))
                hpi = ctlDict[ctl];
            else if (hwndDict.ContainsKey(ctl.Handle))
                hpi = hwndDict[ctl.Handle];
            if (hpi == null) {
                // If new control, create a new
                // HookedProcInformation for it.
                hpi = new HookedProcInformation(ctl,
                    new Win32.WndProc(WndProcHooker.WindowProc));
                ctl.HandleCreated += new EventHandler(ctl_HandleCreated);
                ctl.HandleDestroyed += new EventHandler(ctl_HandleDestroyed);
                ctl.Disposed += new EventHandler(ctl_Disposed);

                // If the handle has already been created set the hook. If it
                // hasn't been created yet, the hook will get set in the
                // ctl_HandleCreated event handler.
                if (ctl.Handle != IntPtr.Zero)
                    hpi.SetHook();
            }

            // Stick hpi into the correct dictionary.
            if (ctl.Handle == IntPtr.Zero)
                ctlDict[ctl] = hpi;
            else
                hwndDict[ctl.Handle] = hpi;

            // Add the message/callback into the message map.
            hpi.messageMap[msg] = callback;
        }