键盘钩子问题??实在是不知道是为什么,汗!!!!!

liran1229 2008-03-27 03:33:46
我用VC6.0尝试的写了一个键盘钩子,出现了这样的问题。
只有打开VC6.0,鼠标点击VC6.0的窗口按下键盘才能记录键盘消息。
其他情况都不能记录这是为什么呢??
因为我写的键盘钩子不是全局的????
还是因为我编写的DLL引入的方式不对????
请高手帮忙解决疑惑,代码如下:
1.DLL文件(.cpp)
#include "stdafx.h"
#include <afxdllx.h>
#include <windows.h>
#include "keyboardHook.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma data_seg("publicdata") //定义全局数据段
HHOOK keyhook=NULL; //钩子句柄
HINSTANCE ghInstDLL =NULL;
#pragma data_seg()
#pragma comment(linker,"/SECTION:publicdata,RWS")


static AFX_EXTENSION_MODULE KeyboardHookDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("KEYBOARDHOOK.DLL Initializing!\n");
if (!AfxInitExtensionModule(KeyboardHookDLL, hInstance))
return 0;
ghInstDLL = hInstance;
new CDynLinkLibrary(KeyboardHookDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("KEYBOARDHOOK.DLL Terminating!\n");
AfxTermExtensionModule(KeyboardHookDLL);
}
return 1;
}

bool WINAPI SetHook()
{
keyhook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, ghInstDLL, 0);
if(NULL != keyhook)
{
return true;
}
else
return false;
}

void WINAPI ReleaseHook()
{
UnhookWindowsHookEx(keyhook);
}

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
FILE *fp = NULL;
BYTE keyStateArr[256];
WORD word;
char ch;
unsigned int scanCode = lParam;
if( HC_ACTION==nCode )
{
if(!(lParam & 0x80000000))//如果键正被按下
{
GetKeyboardState(keyStateArr);
ToAscii(wParam, scanCode, keyStateArr, &word, 0);
ch = (char) word;
fp = fopen("xxxxxx.txt", "a+");
fwrite(&ch, sizeof(ch), 1, fp);
fclose(fp);
}
}
return CallNextHookEx(keyhook,nCode,wParam,lParam);
}
2.EXE文件
#include "keyboardHook.h"
void main()
{
SetHook();
sleep(10000);
ReleaseHook();
}
用的是隐式的方式引入DLL的。
...全文
32 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
http://dev.csdn.net/article/33/33819.shtm
回复
相关推荐
发帖
Windows SDK/API
创建于2007-08-02

1206

社区成员

C++ Builder Windows SDK/API
申请成为版主
帖子事件
创建了帖子
2008-03-27 03:33
社区公告
暂无公告