1

我正在开发一个窗口应用程序 c# .net。该应用程序用于通过远程桌面连接连接其他机器(窗口机器)并监控用户在该远程机器上所做的事情。

为了监控用户操作,我在连接到远程机器时使用 SetWindowsHookEx() 方法安装了 WH_KEYBOARD_LL 和 WH_MOUSE_LL 挂钩的自定义挂钩程序作为全局挂钩程序。

当用户从远程机器上注销时,我需要使用 UnhookWindowsHookEx() 释放钩子程序。通过显示消息出现问题:“フックハンドルが无效です”(这意味着无效的挂钩手柄)。

这个错误并不总是发生。它有时会发生。

我是 c# .net 开发的初学者。我不知道这个错误。

所以,我想知道为什么在调用 UnhookWindowsHookEx() 时会发生该错误。

请任何人帮助我。

我的代码是:

try
{
  HookManager.KeyDown -= HookManager_KeyDown;
  HookManager.KeyUp -= HookManager_KeyUp;
  HookManager.MouseUp -= HookManager_MouseUp;
}
catch (Exception ex)
{
  // error is written to log file if uninstalling hook procedure is failed
  LogController.Instance.Fatal(ex.Source + CommonConstants.TAB + ex.Message);
}

.....

上述代码执行以下代码:

public static partial class HookManager
{
  private static int s_KeyboardHookHandle;

  private static event KeyEventHandler s_KeyDown;

  public static event KeyEventHandler KeyDown
  {
       add
       {
           EnsureSubscribedToGlobalKeyboardEvents();
           s_KeyDown += value;
       }
       remove
       {
           s_KeyDown -= value;
           TryUnsubscribeFromGlobalKeyboardEvents();
       }
   }

   private static void EnsureSubscribedToGlobalKeyboardEvents()
    {
        // install Keyboard hook only if it is not installed and must be installed
        if (s_KeyboardHookHandle == 0)
        {
            //See comment of this field. To avoid GC to clean it up.
            s_KeyboardDelegate = KeyboardHookProc;
            //install hook
            s_KeyboardHookHandle = SetWindowsHookEx(
                WH_KEYBOARD_LL,
                s_KeyboardDelegate,
                Marshal.GetHINSTANCE(
                    Assembly.GetExecutingAssembly().GetModules()[0]),
                0);
            //If SetWindowsHookEx fails.
            if (s_KeyboardHookHandle == 0)
            {
                //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                int errorCode = Marshal.GetLastWin32Error();
                //do cleanup

                //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                throw new Win32Exception(errorCode);
            }
        }
    }

   private static void TryUnsubscribeFromGlobalKeyboardEvents()
    {
        //if no subsribers are registered unsubsribe from hook
        if (s_KeyDown == null &&
            s_KeyUp == null &&
            s_KeyPress == null)
        {
            ForceUnsunscribeFromGlobalKeyboardEvents();
        }
    }

   private static void ForceUnsunscribeFromGlobalKeyboardEvents()
    {
        if (s_KeyboardHookHandle != 0)
        {
            //uninstall hook
            int result = UnhookWindowsHookEx(s_KeyboardHookHandle);
            //reset invalid handle
            s_KeyboardHookHandle = 0;
            //Free up for GC
            s_KeyboardDelegate = null;
            //if failed and exception must be thrown
            if (result == 0)
            {
                //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                int errorCode = Marshal.GetLastWin32Error();
                //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                throw new Win32Exception(errorCode);
            }
        }
    }

 private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
    {
        //indicates if any of underlaing events set e.Handled flag
        bool handled = false;

        if (nCode >= 0)
        {
            //read structure KeyboardHookStruct at lParam
            KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
            //raise KeyDown
            if (s_KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
            {
                Keys keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                KeyEventArgs e = new KeyEventArgs(keyData);
                s_KeyDown.Invoke(null, e);
                handled = e.Handled;
            }

            // raise KeyPress
            if (s_KeyPress != null && wParam == WM_KEYDOWN)
            {
                bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                byte[] keyState = new byte[256];
                GetKeyboardState(keyState);
                byte[] inBuffer = new byte[2];
                if (ToAscii(MyKeyboardHookStruct.VirtualKeyCode,
                          MyKeyboardHookStruct.ScanCode,
                          keyState,
                          inBuffer,
                          MyKeyboardHookStruct.Flags) == 1)
                {
                    char key = (char)inBuffer[0];
                    if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);
                    KeyPressEventArgs e = new KeyPressEventArgs(key);
                    s_KeyPress.Invoke(null, e);
                    handled = handled || e.Handled;
                }
            }

            // raise KeyUp
            if (s_KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
            {
                Keys keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                KeyEventArgs e = new KeyEventArgs(keyData);
                s_KeyUp.Invoke(null, e);
                handled = handled || e.Handled;
            }

        }

        //if event handled in application do not handoff to other listeners
        if (handled)
            return -1;

        //forward to other application
        return CallNextHookEx(s_KeyboardHookHandle, nCode, wParam, lParam);
    }
4

0 回答 0