a7716021 2016-05-24 16:59 采纳率: 100%
浏览 1388
已采纳

关于HOOK的一个小问题

C++在使用HOOK的时候遇到了一些小问题。

SetWindowsHookEx(WH_KEYBOARD_LL,KeyboardProc,GetModuleHandle(_T("WillSetHook")), 0 );

设置没问题 都成功了。

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//KeyboardHookStruct KHS = new KeyboardHookStruct();
//KHS = (KeyboardHookStruct)lParam;
if (WM_KEYDOWN == wParam /
&& (KHS.flags & 128)/64 == 0*/)
{
keybd_event(66,0,0,0);
keybd_event(66,0,KEYEVENTF_KEYUP,0);
return 1;
}

return CallNextHookEx(NULL, nCode, wParam, lParam);

}
这个是我的钩子函数,也能正确调用,没问题。问题就出在这个LPARAM这个参数上。因为我用的是WH_KEYBOARD_LL钩子,所以LPARAM这个参数是一个KeyboardHookStruct类型的结构体(或者是类),但是这个结构体要怎么定义呢?,或者需要包含什么头文件呢?

我自己尝试定义了这个结构体(类):
// public class KeyboardHookStruct
// {
// public int vkCode;

// public int scanCode;

// public int flags;
// public int time;

// public int dwExtraInfo;
// };
// struct class KeyboardHookStruct
// {
// int vkCode;

// int scanCode;

// int flags;
// int time;

// int dwExtraInfo;
// };
类型和网上给出的类型是一样的,但是却无法编译过,可能是我基础不太扎实。
求助大神教我改怎么去定义这个结构体,在哪里定义(我自定义的类内还是类外?),具体的定义方法,我的钩子函数里 该怎么去写,才能将LPARAM里面的值拿出来用。

(被注释掉的代码因编译错误而被注释掉了)

  • 写回答

3条回答

  • 赵4老师 2016-05-25 01:52
    关注

    KBDLLHOOKSTRUCT
    The KBDLLHOOKSTRUCT structure contains information about a low-level keyboard input event.

    typedef struct tagKBDLLHOOKSTRUCT {
    DWORD vkCode; // virtual key code
    DWORD scanCode; // scan code
    DWORD flags; // flags
    DWORD time; // time stamp for this message
    DWORD dwExtraInfo; // extra info from the driver or keybd_event
    } KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

    Members
    vkCode
    Specifies a virtual-key code. The code must be a value in the range 1 to 254.
    scanCode
    Specifies a hardware scan code for the key.
    flags
    Specifies the extended-key flag, event-injected flag, context code, and transition-state flag. Value Description
    0 Specifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0.
    1-3 Reserved.
    4 Specifies whether the event was injected. The value is 1 if the event was injected; otherwise, it is 0.
    5 Specifies the context code. The value is 1 if the ALT key is pressed; otherwise, it is 0.
    6 Reserved.
    7 Specifies the transition state. The value is 0 if the key is pressed and 1 if it is being released.

    An application can use the following values to test the keystroke flags: Value Purpose
    LLKHF_EXTENDED Test the extended-key flag.

    LLKHF_INJECTED Test the event-injected flag.

    LLKHF_ALTDOWN Test the context code.

    LLKHF_UP Test the transition-state flag.

    time
    Specifies the time stamp for this message.
    dwExtraInfo
    Specifies extra information associated with the message.

    Declared in winuser.h.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了