C ++控制台应用程序,SetWindowsHookEx,从不调用回调 [英] C++ Console app, SetWindowsHookEx, Callback is never called

查看:970
本文介绍了C ++控制台应用程序,SetWindowsHookEx,从不调用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有嵌入式v8引擎的控制台应用程序,我想添加一个钩子来注册键事件。这一切在我使用Qt和QtScript之前,但我正在移植到VC ++ 2008中的直接C ++。应用程序编译和运行,但挂钩从未调用,这里是相关的代码:



在main()

  HWND hwndC = GetConsoleWindow 
HINSTANCE hInst =(HINSTANCE)GetWindowLong(hwndC,GWL_HINSTANCE);
if(SetWindowsHookEx(WH_KEYBOARD_LL,HookProc,hInst,NULL)== 0){
printf(无法设置hook\\\
);
} else {
printf(Hook established \\\
);
}
g-> RunScript(argc,argv);

并且处理:

  LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
printf(HookProc called\\\
);
PKBDLLHOOKSTRUCT p =(PKBDLLHOOKSTRUCT)(lParam);
if(wParam == WM_KEYDOWN){
keyDown(p,g);
} else if(wParam == WM_KEYUP){
keyUp(p,g);
}
fflush(stdout);
return CallNextHookEx(NULL,nCode,wParam,lParam);
}

这本质上是从v8示例代码扩展shell.cc。我不知道它是否以某种方式阻塞?我承认不知道我在这里做什么,只是玩耍和学习,但这一个让我沮丧。



在keyDown里面说,我有这样的:

  v8 :: Handle< v8 :: String> callback_name = v8 :: String :: New(onKeyDown); 
v8 :: Handle< v8 :: Value> callback_val = g-> _context-> Global() - > Get(callback_name);
if(!callback_val-> IsFunction()){
printf(No onKeyDown handler found\\\
);
return;
}
v8 :: Handle< v8 :: Function> callback = v8 :: Handle< v8 :: Function> :: Cast(callback_val);
const int argc = 1;
v8 :: Handle< v8 :: Value> argv [argc] = {v8 :: Int32 :: New(char(p-> vkCode))};
printf(Calling onKeyDown\\\
);
v8 :: Handle< v8 :: Value> result = callback-> Call(g-> _context-> Global(),argc,argv);

其中一些实际上可能不会工作,但是它从来没有被调用,当我运行程序,并定义:onKeyDown = function(key){...};我可以看到onKeyDown工作很好,我可以使用我所有的绑定的c ++方法等从JS,所以这个东西只是驱动我蝙蝠。



任何帮助,也许指向一些教育材料将非常感谢。



函数在c:LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)从来没有被调用,或从来没有看到一个printf,输出在开始说:挂钩建立,所以Windows报告钩子建立。 p>

/ Jason

解决方案

低级别的钩子,例如WH_KEYBOARD_LL,您的应用程序会消息循环。这是Windows可以打破你的线程并调用你注册的HookProc回调的唯一方法。



一个控制台模式应用程序不像普通的Windows GUI应用程序。从你的代码段来看,添加一个也不容易。您需要创建一个主题。


I have a little console application that has an embedded v8 engine, and I would like to add a hook to register key events. This all worked before when I was using Qt and QtScript, but I am porting it all over to straight C++ in VC++ 2008. The application compiles and runs, but the hook is never called, here is the relevant code:

In main()

HWND hwndC = GetConsoleWindow() ;
    HINSTANCE hInst = (HINSTANCE)GetWindowLong( hwndC, GWL_HINSTANCE );
    if (SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, hInst, NULL) == 0) {
        printf("Failed to set hook\n");
    } else {
        printf("Hook established\n");
    }
    g->RunScript(argc,argv);

And the proc:

LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    printf("HookProc called\n");
    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
    if (wParam == WM_KEYDOWN) {
       keyDown(p,g);
    } else if (wParam == WM_KEYUP) {
        keyUp(p,g);
    }
    fflush(stdout);
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

This is essentially an expansion on shell.cc from the v8 sample code. I wonder if it is somehow blocking? I admit to not really knowing what I am doing here, just playing around and learning but this one has me stumped.

Inside of keyDown say, I have something like this:

    v8::Handle<v8::String> callback_name = v8::String::New("onKeyDown");
    v8::Handle<v8::Value> callback_val = g->_context->Global()->Get(callback_name);
    if (!callback_val->IsFunction()) {
        printf("No onKeyDown handler found\n");
        return;
    }
    v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast(callback_val);
    const int argc = 1;
    v8::Handle<v8::Value> argv[argc] = { v8::Int32::New(char(p->vkCode)) };
    printf("Calling onKeyDown\n");
    v8::Handle<v8::Value> result = callback->Call(g->_context->Global(), argc, argv);

Some of this may actually not work in the end, but it just never gets called, when I run the program, and define: onKeyDown = function(key) {...}; I can see that onKeyDown is working just fine, I can use all of my bound c++ method etc from JS, so this thing is just driving me batty.

Any help, maybe pointers to some educational materials would be much appreciated.

Just to be clear, this function in c: LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) is never getting called, or never seeing a printf, and the output at the start says: Hook established, so windows is reporting the hook is established.

/Jason

解决方案

A low-level hook, like WH_KEYBOARD_LL requires that your application pumps a message loop. That's the only way that Windows can break into your thread and make the call to the HookProc callback you registered.

A console mode app doesn't pump a message loop like regular Windows GUI apps do. Judging from your snippet, it isn't going to be easy to add one either. You'll need to create a thread.

这篇关于C ++控制台应用程序,SetWindowsHookEx,从不调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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