我用hook截获了一个键盘消息后为什么会弹出两个对话框,应该只有一次的

ggglivw 2007-04-30 10:36:29
#include "stdafx.h"
#include "hook.h"
#include <stdio.h>
HINSTANCE g_hInst;
#pragma data_seg ("shared")
static HHOOK g_hHook=NULL;
#pragma data_seg ()
LRESULT CALLBACK KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hInst = HINSTANCE(hModule);
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}


DLLEXPORT int CALLBACK InstallHOOK()
{
g_hHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,g_hInst,0);
if (g_hHook)
return TRUE;
else
return FALSE;
}

DLLEXPORT int CALLBACK UninstallHOOK()
{

if (UnhookWindowsHookEx(g_hHook)==0)
return FALSE;
else
return TRUE;

}

LRESULT CALLBACK KeyboardProc(
int iCode, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{

if((GetAsyncKeyState(VK_CONTROL)&0X8000)&&wParam==VK_F12)
{
MessageBox(NULL, "DLL", "www", MB_OK);
}

return CallNextHookEx(g_hHook,iCode,wParam,lParam);

}

MessageBox(NULL, "DLL", "www", MB_OK)会出现两次,怎么回事啊???
其他的.h .cpp .def就只是一些定义而已
...全文
294 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggglivw 2007-04-30
  • 打赏
  • 举报
回复
晕啊,弄了半天还是没有弄好
ggglivw 2007-04-30
  • 打赏
  • 举报
回复
我找到了,lparam做位跟状态的判断,wParam==VK_F12,应该是这样吧
wltg2001 2007-04-30
  • 打赏
  • 举报
回复
MSDN上没有吗?我看一下。
WM_KEYDOWN:lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
16-23
Specifies the scan code. The value depends on the OEM.
24
Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28
Reserved; do not use.
29
Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
30
Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
31
Specifies the transition state. The value is always zero for a WM_KEYDOWN message.
WM_KEYUP:
lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. The repeat count is always one for a WM_KEYUP message.
16-23
Specifies the scan code. The value depends on the OEM.
24
Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28
Reserved; do not use.
29
Specifies the context code. The value is always 0 for a WM_KEYUP message.
30
Specifies the previous key state. The value is always 1 for a WM_KEYUP message.
31
Specifies the transition state. The value is always 1 for a WM_KEYUP message.

ggglivw 2007-04-30
  • 打赏
  • 举报
回复
Message parameter???有没有详细一点的资料啊,网上也没有这个参数具体传的什么东西,每一位代表什么意思
wltg2001 2007-04-30
  • 打赏
  • 举报
回复
你两个消息的lparam应该有所区别,你查一下MSDN,看一下对这两个消息的这个参数的描述。
ggglivw 2007-04-30
  • 打赏
  • 举报
回复
我是在HOOK中设置的,那里应该怎么设置啊?最好是组合键弹起CONTROL+F12,我要写一个老板键,就像青鹏游戏类似的功能
wltg2001 2007-04-30
  • 打赏
  • 举报
回复
你按一下键盘对应的确实是两个消息,一个按下,一个弹上,对应的是WM_KEYDOWN和WM_KEYUP
ggglivw 2007-04-30
  • 打赏
  • 举报
回复
别的网友的代码可能是vc++6.0的,我用的是vs2003,会不会是编译器的原因
ggglivw 2007-04-30
  • 打赏
  • 举报
回复
我知道没有啊,
// HOOK.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "hook.h"
#include <stdio.h>

HINSTANCE g_hInst;
static HHOOK g_hHook=NULL;

LRESULT CALLBACK KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam);

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hInst = HINSTANCE(hModule);
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}


DLLEXPORT int CALLBACK InstallHOOK()
{
g_hHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,g_hInst,0);
if (g_hHook)
return TRUE;
else
return FALSE;
}

DLLEXPORT int CALLBACK UninstallHOOK()
{

if (UnhookWindowsHookEx(g_hHook)==0)
return FALSE;
else
return TRUE;

}

LRESULT CALLBACK KeyboardProc(
int iCode, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{

MessageBox(NULL, "dll", "www", MB_OKCANCEL);
return CallNextHookEx(g_hHook,iCode,wParam,lParam);

}
会不会是#include "stdafx.h"的问题,但是其他的key都没有问题,就是F12,所以才很头痛
huaren801007 2007-04-30
  • 打赏
  • 举报
回复
在KeyboardProc中,其实会执行两次,一次是键盘按下,另一次是KeyUp

KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*)lParam;
if(!(p->flags & LLKHF_UP))
{
// 按下处理
// 不想让其他处理的话,返回TRUE
}
wltg2001 2007-04-30
  • 打赏
  • 举报
回复
是不是你系统中有另外的钩子处理了F12了。
ggglivw 2007-04-30
  • 打赏
  • 举报
回复
无遇到了一个很诡异的问题
LRESULT CALLBACK KeyboardProc(
int iCode, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{

MessageBox(NULL, "DLL", "www", MB_OK);

return CallNextHookEx(g_hHook,iCode,wParam,lParam);

}
我按其他的键都没有问题,只要一按F12就死机,怎么回事啊,

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧