我的键盘钩子怎么截获不到消息啊?
我的键盘钩子怎么不能截获到键盘的操作啊?
dll代码:
#pragma data_seg(".SHARDAT")
static HHOOK hkb=NULL;
#pragma data_seg()
HINSTANCE hins;
BOOL __declspec(dllexport)__stdcall installhook()
{
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
return TRUE;
}
BOOL __declspec(dllexport)__stdcall unstallhook()
{
if(hkb!=NULL)
UnhookWindowsHookEx((HHOOK)hkb);
return TRUE;
}
LRESULT __declspec(dllexport)__stdcall CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if(((DWORD)lParam&0x80000000) && (HC_ACTION==nCode))
{
char c[1];
switch(wParam) //键盘按键标识
{
case '1':{ c[0]=1; SaveLog(c); break; } //当数字键1被按下,记录
case '2':{ c[0]=2; SaveLog(c); break; }
case '3':{ c[0]=3; SaveLog(c); break; }
case '4':{ c[0]=4; SaveLog(c); break; }
case 'A':{ c[0]='A'; SaveLog(c); break; } //当字母键A
case 'B':{ c[0]='B'; SaveLog(c); break; }
case 'C':{ c[0]='C'; SaveLog(c); break; }
case 'D':{ c[0]='D'; SaveLog(c); break; }
}
}
LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}
进程代码:
static HINSTANCE hinstDLL;
void CTestdlgDlg::OnStarthook() //开始按钮代码
{
// TODO: Add your control notification handler code here
CreateMutex(NULL, FALSE, "testdlg");
if(GetLastError()==ERROR_ALREADY_EXISTS)
MessageBox("错误");
//读取DLL
typedef BOOL (CALLBACK *inshook)();
inshook instkbhook;
if(hinstDLL=LoadLibrary((LPCTSTR)"hookdll.dll"))
{
instkbhook=(inshook)GetProcAddress(hinstDLL,"installhook");
instkbhook();
}
else
{
MessageBox("当前目录找不到Hook.dll文件,程序初始化失败");
}
}
void CTestdlgDlg::OnStophook() //停止按钮
{
// TODO: Add your control notification handler code here
typedef BOOL (CALLBACK *unshook)();
unshook unstkbhook;
unstkbhook=(unshook)GetProcAddress(hinstDLL,"unstallhook");
unstkbhook();
FreeLibrary(hinstDLL);
}
为什么我按了开始按钮后,然后在键盘上有一系列的操作后,我的记录文件中并没有任何记录呢?
谢了先!!