SetWindowsHookEx 与 WH_KEYBOARD 对我不起作用,我有什么问题? [英] SetWindowsHookEx with WH_KEYBOARD doesn't work for me, what do I wrong?

查看:59
本文介绍了SetWindowsHookEx 与 WH_KEYBOARD 对我不起作用,我有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <fstream>
#define _WIN32_WINNT 0x501
#include <windows.h>

using namespace std;

HHOOK hKeyboardHook = 0;
LRESULT CALLBACK KeyboardCallback(int code,WPARAM wParam,LPARAM lParam) {  
  cout << "a key was pressed" << endl;
  ofstream myfile;
  myfile.open ("hookcheck.txt", ios::ate | ios::app);
  myfile << "a key was pressed\n";
  myfile.close();
  return CallNextHookEx(hKeyboardHook,code,wParam,lParam);
}

int main() {

  HWND consoleWindow = GetConsoleWindow();
  HINSTANCE hInstCons = (HINSTANCE)GetWindowLong( consoleWindow, GWL_HINSTANCE );
  hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD, (HOOKPROC)KeyboardCallback, (HINSTANCE)consoleWindow, GetCurrentThreadId());

  MessageBox(NULL, "It is keyboard time!", "Let's Go", MB_OK);

}

循环进行时每次按键时的这段代码应该在控制台上打印消息并创建一个文件,但什么也没有发生.我做错了什么?

This code on every key press while the loop is going should print message on console and create a file, but nothing is happening. What do I wrong ?

推荐答案

我会引用 另一个主题:

控制台窗口完全由 CSRSS 处理,它是一个系统过程.在进程中安装一个钩子意味着注入你的 DLL进去.由于 CSRSS 非常重要(它对于运行系统,并且其中的代码作为 LocalSystem 运行,它是本地的超级管理员用户),则不允许向其中注入代码.那么你不能钩住它的任何窗户.

Console windows are handled entirely by CSRSS, which is a system process. Installing a hook into a process means injecting your DLL into it. Since CSRSS is so important (it's vital for running of the system, and code within runs as LocalSystem, which is the local super-admin user), you're not allowed to inject code into it. So you can't hook any of its windows.

在您的简单控制台应用程序中没有发生真正的窗口消息,因此不必调用您的钩子,在您的情况下,您甚至没有注入钩子,而是仅使用线程模式钩子.根据 MSDN 文档,当即将处理消息时调用它:

There are no real window messages taking place in your simple console application, so your hook does not have to be called, and in your case you are not even injecting your hook but using thread mode hook only. Per MSDN documentation it is called when messages are about to be processed:

一个应用程序定义的或库定义的回调函数,用于SetWindowsHookEx 函数.每当系统调用此函数时应用程序调用 GetMessage 或 PeekMessage 函数,然后是要处理的键盘消息(WM_KEYUP 或 WM_KEYDOWN).

An application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed.

现在让我向您展示如何开始接听电话:

Now let me show you what you can do to start receiving calls on your hook:

MessageBox(NULL, _T("It is keyboard time!"), _T("Let's Go"), MB_OK);

//for(int i=0; i<=10; i++) {
//  cout << i << endl;
//  Sleep(1000);
//}

执行 MessageBox 并在关闭它之前开始输入 - 您将开始收到挂钩调用.

Do MessageBox and before closing it start typing - you will start getting hook calls.

这篇关于SetWindowsHookEx 与 WH_KEYBOARD 对我不起作用,我有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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