关于Low Level Keyboard Hook的郁闷,万恶的SDK啊~~

来源:互联网 发布:黄维德的周瑜 知乎 编辑:程序博客网 时间:2024/05/01 06:52

今天要做点事情,需要屏蔽Win键的几个组合键,于是想到用WH_KEYBOARD_LL,随便看了下VS6.0自带的MSDN里LowLevelKeyboardProc的描述:
The LowLevelKeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue. The keyboard input can come from the local keyboard driver or from calls to thekeybd_event function. If the input comes from a call to keybd_event, the input was "injected".

于是写了类似下面的代码:
void f()
{
    HHOOK hHook = NULL;
    HANDLE hThread = NULL;
  __try
  {
      hHOOK = SetWindowHookEx(hInstance,WH_KEYBOARD_LL,LowLevelKeyboardProc,0);
      if(NULL == hHook) __leave;

      hThread=(HANDLE) _beginthread(.......); 
      if ((HANDLE)1 == hThread) __leave;

      WaitForSingleObject(hThread);
  }
  __finally
 {
      if (NULL != hThread) CloseHandle(hThread);
      if (NULL != hHook) UnhookWindowsHookEx(hHook);
  }
}

结果发现,安装的HOOK不起作用,研究了很久也不明白怎么回事,正在郁闷中,突然看到Win2K3的SDK Documentation里对LowLevelKeyboardProc的描述是:
The LowLevelKeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue. The keyboard input can come from the local keyboard driver or from calls to the keybd_event function. If the input comes from a call to keybd_event, the input was "injected". However, the WH_KEYBOARD_LL hook is not injected into another process. Instead, the context switches back to the process that installed the hook and it is called in its original context. Then the context switches back to the application that generated the event.  

天啊,平白无故的多出来一段,真是万恶的SDK啊~~!这样的话,问题就很清楚了,安装Hook的线程没有消息循环,所以没有机会执行LowLevelKeyboardProc。于是改造:

void f()
{
    HANDLE hHOOKThread = NULL;
    HANDLE hThread = NULL;

  __try
  {
      DWORD TID;
 
      hHOOKThread = (HANDLE) _beginthreadex(NULL,0,HookThreadProc,NULL,0,&TID);
      if(NULL == hHOOKThread) __leave;

      hThread=(HANDLE) _beginthread(.......); 
      if ((HANDLE)1 == hThread) __leave;

      WaitForSingleObject(hThread);

      PostThreadMessage(TID,WM_QUIT,0,0);
  }
  __finally
 {
      if (NULL != hThread) CloseHandle(hThread);
      if (NULL !=hHOOKThread) CloseHandle(hHOOKThread);
      if (NULL != hHook) UnhookWindowsHookEx(hHook);
  }
}

unsigned __stdcall HookThreadProc(void * lpParam)
{
      HOOK hHook = SetWindowsHookEx(hInstance,WH_KEYBOARD_LL,LowLevelKeyboardProc,0);
      if (NULL == hHook) return;
      MSG msg;
      while(GetMessage(&msg,NULL,0,0));
      UnhookWindowsHookEx(hHook);
}

问题解决。