我要用一个钩子,来捕获特定的一个应用程序中的点击某个按钮的事件,当按钮点击的次数超过一定次数后,就不让其再响应这个按钮的事件,
我自己DLL写了钩子,老是有问题,我的思路主要如下,设置一个GETMESSAGE的钩子,在DLL设置一个计数器,每次钩子勾到这个按钮的消息以后,就将计数器加1,当超过了某个次数以后,就不调用CallNextHookEx。
主要代码如下:
LRESULT CALLBACK GetMsgProc(int code,WPARAM wParam,LPARAM lParam)
{
if(code < 0)
{
::MessageBox(NULL,_T("<0必须处理"),_T("提示"),MB_OK);
}
else if(code == HC_ACTION)
{
  MSG* pMsg = (MSG*)lParam;
  char szBuffer[100];
  GetWindowText(pMsg->hwnd,LPWSTR(szBuffer),100);
  if(strcmp(szBuffer,"Button") == 0)
  {
  if(++g_iCount >=5)
  {
  return 1;
  }
  }
}
return CallNextHookEx(g_hMsgHook,code,wParam,lParam);}不知道这样行不行,高手请赐教,谢谢!!!!!!!

解决方案 »

  1.   

    还需要通过LPARAM判断一下具体的消息内容是不是click消息
      

  2.   

    我试着改成这样了,但是,好想没什么反应,即使我没有调用CallNextHookEx,在应用程序里面照样能响应!!!!!!
      

  3.   

    LRESULT CALLBACK GetMsgProc(int code,WPARAM wParam,LPARAM lParam)
    {
    if(code < 0)
    {
    ::MessageBox(NULL,_T("<0必须处理"),_T("提示"),MB_OK);
    }
    else if(code == HC_ACTION)
    {
      //::MessageBox(NULL,_T("action"),_T("提示"),MB_OK);
      MSG* pMsg = (MSG*)lParam;
      char szBuffer[100];
      if(pMsg->hwnd == NULL)
      return 1;//运行到这里之后,还是能响应!!!
      GetWindowText(pMsg->hwnd,LPWSTR(szBuffer),100);
      if(strcmp(szBuffer,"Button") == 0 && pMsg->lParam == NM_CLICK)
      {
      ::MessageBox(NULL,_T("匹配"),_T("提示"),MB_OK);
      if(++g_iCount >=5)
      {
      ::MessageBox(NULL,_T("次数超限"),_T("提示"),MB_OK);
      return 1;
      }
      }
    }
    return CallNextHookEx(g_hMsgHook,code,wParam,lParam);}
      

  4.   

    是不是这个问题??
    在MSDN中查到的!LRESULT CALLBACK GetMsgProc(          int code,
        WPARAM wParam,
        LPARAM lParam
    );
    Parameterscode
    [in] Specifies whether the hook procedure must process the message. If code is HC_ACTION, the hook procedure must process the message. If code is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. 
    wParam//这个参数??????
    [in] Specifies whether the message has been removed from the queue. This parameter can be one of the following values. 
    PM_NOREMOVE
    Specifies that the message has not been removed from the queue. (An application called the PeekMessage function, specifying the PM_NOREMOVE flag.)
    PM_REMOVE
    Specifies that the message has been removed from the queue. (An application called GetMessage, or it called the PeekMessage function, specifying the PM_REMOVE flag.)
    lParam
    [in] Pointer to an MSG structure that contains details about the message. 
    是不是消息被勾住了之后,由于消息还是没有移除消息队列,所以,处理过程还是被调用了??
      

  5.   

    WPARAM wParam, 这个消息在队列里没有删除!
      

  6.   

    不是,是针对某个进程的钩子!
    void CMyHook::SetMessageHook()
    {
    g_hMsgHook = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,NULL,::GetCurrentThreadId());
    if(g_hMsgHook == NULL)
    {
    ::MessageBox(NULL,_T("装载钩子失败"),_T("提示"),MB_OK);
    }}
      

  7.   

    13楼,句柄是封装在DLL里,SetWindowsHookEx里的NULL值往前移到GetMsgProc前面定义!
      

  8.   

    HHOOK SetWindowsHookEx(  //装载钩子         
        int idHook,          //装载的钩子的类型
        HOOKPROC lpfn,       //钩子回调函数的地址,dwThreadId为0回调函数会在不同的进程中创建,它必须写在DLL中
        HINSTANCE hMod,      //DLL的句柄
        DWORD dwThreadId     //描述要钩住的线程ID,如果参数为0,会钩住桌面上的所有线程!
    );                      //若函数调用成功,返回值是成功开启的钩子句柄,如果失败,返回值为0!
    LRESULT CALLBACK GetMsgProc(          
    int code,        //nCode == HC_ACTION,钩子程序处理这个消息,如果nCode 的值小于零,则跳过这个消息
    WPARAM wParam,   //这个消息是否从消息队列中被删除,值为:PM_NOREMOVE(应用程序调用了PeekMessage)
                     //PM_REMOVE :描述这个消息已经从消息队列中删除(应用程序调用了 GetMessage 或 PeekMessage)
    LPARAM lParam    //MSG 结构体的指针
    );
      

  9.   

    //我是写在DLL中的啊!! 
    不懂了!!!郁闷!!
      

  10.   

    我调试发现,没进入这段代码里面!!!
    if(strcmp(szBuffer,"Button") == 0 && pMsg->lParam == NM_CLICK) 
      { 
      ::MessageBox(NULL,_T("匹配"),_T("提示"),MB_OK); 
      if(++g_iCount >=5) 
      { 
      ::MessageBox(NULL,_T("次数超限"),_T("提示"),MB_OK); 
      return 1; 
      } 
      } 
    高手帮忙看看,哪里有问题???或者是问题出在哪里???