Ejemplo n.º 1
0
        public void Dispose()
        {
            if (MouseHookHandle != 0)
            {
                Debug.Print("Unhook mouse");
                HookNativeDefinitions.UnhookWindowsHookEx(MouseHookHandle);
                MouseHookHandle = 0;
            }

            if (KeyboardHookHandle != 0)
            {
                Debug.Print("Unhook Keyboard");
                HookNativeDefinitions.UnhookWindowsHookEx(KeyboardHookHandle);
                KeyboardHookHandle = 0;
            }
        }
Ejemplo n.º 2
0
 private void HookMouse(bool bHook)
 {
     if (MouseHookHandle == 0 && bHook)
     {
         using (var mainMod = Process.GetCurrentProcess().MainModule)
             MouseHookHandle = HookNativeDefinitions.SetWindowsHookEx(HookNativeDefinitions.WH_MOUSE_LL, MouseHookGCRootedDelegate, HookNativeDefinitions.GetModuleHandle(mainMod.ModuleName), 0);
         //If the SetWindowsHookEx function fails.
         if (MouseHookHandle == 0)
         {
             MessageBox.Show("SetWindowsHookEx Failed " + new Win32Exception(Marshal.GetLastWin32Error()));
             return;
         }
     }
     else if (bHook == false)
     {
         Debug.Print("Unhook mouse");
         HookNativeDefinitions.UnhookWindowsHookEx(MouseHookHandle);
         MouseHookHandle = 0;
     }
 }