如何在Win32控制台窗口上设置CBT挂钩? [英] How can I set up a CBT hook on a Win32 console window?

查看:170
本文介绍了如何在Win32控制台窗口上设置CBT挂钩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用以下代码为我的C ++控制台应用程序设置CBT钩子:

I've been trying to set up a CBT hook for my C++ Console application with the following code:

 ...includes...

 typedef struct _HOOKDATA
 {
    int type;
    HOOKPROC hookproc;
    HHOOK hhook;
 }_HOOKDATA;

 _HOOKDATA hookdata;

 //CBT
 LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
 { 
    //do not proccess message
    if(code < 0)
    {
       cout<<"code less than 0"<<endl;
       return CallNextHookEx(hookdata.hhook,code,wParam,lParam);
    }

    switch(code)
   {
      case HCBT_ACTIVATE:
           break;
      case HCBT_CREATEWND:
           cout<<"CREATEWND"<<endl;
           break;
      case HCBT_MINMAX:
           cout<<"MINMAX"<<endl;
           break;
      default: //unknown
           cout<<"DEFAULT"<<endl;
           break;
   }

   return CallNextHookEx(hookdata.hhook, code, wParam, lParam);
}

int main()
{
   hookdata.type = WH_CBT; 
   hookdata.hookproc = CBTProc; 
   hookdata.hhook = ::SetWindowsHookEx(hookdata.type, CBTProc, 
                                    GetModuleHandle( 0 ), GetCurrentThreadId());

   if(hookdata.hhook == NULL)
   {
     cout<<"FAIL"<<endl;
     system("pause");
   }

   system("pause");
   return 0;
}

程序似乎工作,因为没有编译错误或运行时错误。另外我不会得到一个'FAIL'消息在main()函数中说明SetWindowHookEx的工作正常。但是,我没有得到CBTProc函数中声明的任何消息;甚至没有DEFAULT消息。

The program seems to be working because there is not compile errors nor run time errors. Also I do not get a 'FAIL' message stated in the main() function meaning SetWindowHookEx is working OK. However, I don't get any of the messages stated in the CBTProc function; not even the 'DEFAULT' message. Can anyone pin-point what is the logic error in the code?

感谢。

推荐答案

问题是SetWindowHookEx是基于Win32消息处理模型。控制台窗口是内核本身的子级,并且不创建自己的消息泵或窗口。

The problem is that SetWindowHookEx is based upon the Win32 message handling model. Console windows are children of the Kernel itself and do not create their own message pumps or windows.

AFAIK不能直接执行您想要的操作。

AFAIK doing what you want directly is not possible.

这篇关于如何在Win32控制台窗口上设置CBT挂钩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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