Exemple #1
0
 public void setMouseHook()
 {
     Console.WriteLine("Move");
     if (hHook == 0)
     {
         // Create an instance of HookProc.
         _mproc = new LowLevelMouseHookProc(MouseHookProc);
         IntPtr hInstance = LoadLibrary("User32");
         Console.WriteLine("SetWindowsHookEx");
         hHook = SetWindowsHookEx(WH_MOUSE_LL,
                                  _mproc,
                                  hInstance,
                                  0);
         //If the SetWindowsHookEx function fails.
         if (hHook == 0)
         {
             System.Windows.MessageBox.Show("SetWindowsHookEx Failed");
             return;
         }
     }
     else
     {
         bool ret = UnhookWindowsHookEx(hHook);
         //If the UnhookWindowsHookEx function fails.
         if (ret == false)
         {
             System.Windows.MessageBox.Show("UnhookWindowsHookEx Failed");
             return;
         }
         hHook = 0;
     }
 }
        private void SetMouseHook(bool enable)
        {
            if (enable && this.hHook == IntPtr.Zero)
            {
                this.mouseHookProc = this.MouseHookProc;
                this.hHook         = Processes.SetLowLevelMouseHook(ref this.mouseHookProc);
            }
            else if (!enable && this.hHook != IntPtr.Zero)
            {
                bool success = User32.UnhookWindowsHookEx(this.hHook);
                if (success == false)
                {
                    logger.Error($"Failed to un-register a low-level mouse hook. Error code: {Marshal.GetLastWin32Error()}");
                }

                this.hHook         = IntPtr.Zero;
                this.mouseHookProc = null;
            }
        }
Exemple #3
0
        public static IntPtr SetLowLevelMouseHook(ref LowLevelMouseHookProc mouseHookProc)
        {
            IntPtr hHook;

            using (Process process = Process.GetCurrentProcess())
            {
                using (ProcessModule module = process.MainModule)
                {
                    IntPtr hModule = Kernel32.GetModuleHandle(module.ModuleName);

                    hHook = User32.SetWindowsHookEx(HookType.WH_MOUSE_LL, mouseHookProc, hModule, 0);
                    if (hHook == IntPtr.Zero)
                    {
                        logger.Error($"Failed to register a low-level mouse hook. Error code: {Marshal.GetLastWin32Error()}");
                    }
                }
            }

            return(hHook);
        }
Exemple #4
0
 public static extern int SetWindowsHookEx(int hookType, LowLevelMouseHookProc hookProcedure, IntPtr instanceHandle, int threadId);
Exemple #5
0
 public static extern IntPtr SetWindowsHookEx(HookType hookType, LowLevelMouseHookProc lpfn, IntPtr hMod, uint dwThreadId);
Exemple #6
0
 public MouseHookHotkeyVisitor()
 {
     this.hMouseHook    = IntPtr.Zero;
     this.mouseHookProc = this.MouseHookProc;
 }
Exemple #7
0
 static extern int SetWindowsHookEx(int idHook, LowLevelMouseHookProc lpfn,
                                    IntPtr hInstance, int threadId);
 static extern int SetWindowsHookEx(int idHook, LowLevelMouseHookProc lpfn,
 IntPtr hInstance, int threadId);
        public void setMouseHook()
        {
            Console.WriteLine("Move");
            if (hHook == 0)
            {
                // Create an instance of HookProc.
                _mproc = new LowLevelMouseHookProc(MouseHookProc);
                IntPtr hInstance = LoadLibrary("User32");
                Console.WriteLine("SetWindowsHookEx");
                hHook = SetWindowsHookEx(WH_MOUSE_LL,
                            _mproc,
                            hInstance,
                            0);
                //If the SetWindowsHookEx function fails.
                if (hHook == 0)
                {
                    System.Windows.MessageBox.Show("SetWindowsHookEx Failed");
                    return;
                }
            }
            else
            {
                bool ret = UnhookWindowsHookEx(hHook);
                //If the UnhookWindowsHookEx function fails.
                if (ret == false)
                {
                    System.Windows.MessageBox.Show("UnhookWindowsHookEx Failed");
                    return;
                }
                hHook = 0;

            }
        }
Exemple #10
0
 /// <summary>The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.</summary>
 /// <param name="hookType">Specifies the type of hook procedure to be installed.</param>
 /// <param name="callback">Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.</param>
 /// <param name="hMod">Handle to the DLL containing the hook procedure, hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread that containt the hook procedure</param>
 /// <param name="dwThreadId">Identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.</param>
 /// <returns>handle to the hook procedure. If the function fails, the return value is NULL. To get extended error information, call GetLastError.</returns>
 [DllImport("user32", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(HookType hookType, LowLevelMouseHookProc callback, IntPtr hMod, uint dwThreadId);