WH_MOUSE和WH_MOUSE_LL钩子之间的区别是什么? [英] What are all the differences between WH_MOUSE and WH_MOUSE_LL hooks?

查看:532
本文介绍了WH_MOUSE和WH_MOUSE_LL钩子之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 WH_MOUSE 并不总是被调用。问题是我使用 WH_MOUSE 而不是 WH_MOUSE_LL

I've found that WH_MOUSE is not always called. Could the problem be that I'm using WH_MOUSE and not WH_MOUSE_LL?

代码:

class MouseHook
{
public:
  static signal<void(UINT, const MOUSEHOOKSTRUCT&)> clickEvent;

  static bool install() 
  {
    if (isInstalled()) return true;
    hook = ::SetWindowsHookEx(WH_MOUSE, (HOOKPROC)&mouseProc,  
                                ::GetModuleHandle(NULL), NULL);
    return(hook != NULL);
  }

  static bool uninstall() 
  {
    if (hook == NULL) return TRUE;
    bool fOk = ::UnhookWindowsHookEx(hook);
    hook = NULL;
    return fOk != FALSE;
  }

  static bool isInstalled() { return hook != NULL; }

private:
   static LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
   {    		
      if (nCode == HC_ACTION && 
        (wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN ||
         wParam == WM_RBUTTONDOWN || wParam == WM_NCRBUTTONDOWN ||
         wParam == WM_MBUTTONDOWN || wParam == WM_NCMBUTTONDOWN ))
      {
        MOUSEHOOKSTRUCT* mhs = (MOUSEHOOKSTRUCT*) lParam;
        clickEvent(wParam, *mhs);
      } 		

      return ::CallNextHookEx(hook, nCode, wParam, lParam);
    }

   static HHOOK hook;
};


推荐答案

区别在于调用回调时的行为。
如果你使用lowlevel版本,你不会在lpfn的限制,因为调用你的钩子函数的方式执行。有关详细信息,请阅读下面。
从MSDN的SetWindowsHookEx的文档引用:

The difference is in the behavior when the callback gets called. If you're using the lowlevel version you don't incur in the limitations posed by lpfn because of the way the call to your hook function is performed. Please read below for more information. Quoting from MSDN's doc for SetWindowsHookEx:


lpfn
[in]指向挂接过程的指针。如果dwThreadId参数为零或指定由不同进程创建的线程的标识符,则lpfn参数必须指向DLL中的钩子过程。否则,lpfn可以指向与当前进程相关联的代码中的挂钩过程。
lpfn [in] 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.

和从LowLevelKeyboardProc:

and from LowLevelKeyboardProc:


WH_KEYBOARD_LL钩子不会注入另一个处理。相反,上下文切换回安装了钩子的进程,并且在其原始上下文中被调用。然后,上下文切换回生成事件的应用程序。
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.

这篇关于WH_MOUSE和WH_MOUSE_LL钩子之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆