当前位置: 代码迷 >> WinCE >> WinCE5.0下键盘钩子有关问题
  详细解决方案

WinCE5.0下键盘钩子有关问题

热度:79   发布时间:2016-04-28 13:12:56.0
WinCE5.0下键盘钩子问题
小弟最近在WinCE下想学习做个键盘钩子程序,网上找了很多资料,但终不得要领,愚钝之下,特求助各位前辈!
  我用EVC4开发,基于ARM9内核的WinCE5.0系统。我的思路是先建立一个DLL工程,采用全局钩子,代码如下:
[code=C/C++][/code]
//**************************************DLL头文件
#include <windows.h>

#define WH_KEYBOARD_LL 20 
#define HC_ACTION 0 

typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
typedef HHOOK (WINAPI *_SetWindowsHookExW)(int, HOOKPROC, HINSTANCE, DWORD);
typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOK, int, WPARAM, LPARAM);
typedef LRESULT (WINAPI *_UnhookWindowsHookEx)(HHOOK);

static _SetWindowsHookExW SetHook; 
static _UnhookWindowsHookEx UnhookHook; 
static _CallNextHookEx CallNextHook; 

HINSTANCE g_hHookApiDll = NULL;
HHOOK g_hKbdHook = NULL;
HHOOK g_hMouseHook = NULL;

LRESULT CALLBACK KbdHookCallback(int nCode, WPARAM wParam, LPARAM lParam); //键盘钩子回调函数

BOOL ActivateHook(HINSTANCE hInstance, HOOKPROC HookCallback); //激活钩子
BOOL DeactivateHook();

//*************************************************DLL源文件
#include "HookDll.h"

//安装钩子
BOOL ActivateHook(HINSTANCE hInstance, HOOKPROC hookProc) 
{
SetHook = NULL; 
CallNextHook = NULL; 
UnhookHook = NULL; 

g_hHookApiDll = LoadLibrary(_T("coredll.dll")); //加载核心链接库
if(NULL == g_hHookApiDll) 

return FALSE; 


SetHook = (_SetWindowsHookExW)GetProcAddress(g_hHookApiDll, _T("SetWindowsHookExW")); //直接获取地址
if(NULL == SetHook) 

return FALSE; 


g_hKbdHook = SetHook(WH_KEYBOARD_LL, hookProc, hInstance, 0); //设置钩子
if(NULL == g_hKbdHook) 

return FALSE; 


CallNextHook = (_CallNextHookEx)GetProcAddress(g_hHookApiDll, _T("CallNextHookEx")); //下一个
if(NULL == CallNextHook) 

return FALSE; 


UnhookHook = (_UnhookWindowsHookEx)GetProcAddress(g_hHookApiDll, _T("UnhookWindowsHookEx")); //解除钩子
if(NULL == UnhookHook) 

return FALSE; 
}
return TRUE; 
}

//解除钩子过程
BOOL DeactivateHook() 

if(g_hKbdHook != NULL) 

UnhookHook(g_hKbdHook); 
g_hKbdHook = NULL; 


if(g_hHookApiDll != NULL) 

FreeLibrary(g_hHookApiDll); 
g_hHookApiDll = NULL; 

return TRUE; 


//键盘回调过程
LRESULT CALLBACK KbdHookCallback(int nCode, WPARAM wParam, LPARAM lParam) 

if(nCode >= HC_ACTION) 

if( wParam == WM_KEYUP)

//可进行处理


}
return CallNextHook(g_hKbdHook, nCode, wParam, lParam);
}

代码比较长,基本上是网上参考来的,DLL编译没有错误,但是我使用MFC建立一个测试工程HookTest就有错误,我将DLL头文件添加进去,然后在初始化对话框OnInitDialog中添加ActivateHook(AfxGetApp()->m_hInstance, KbdHookCallback);
链接有错误HookTestDlg.obj : error LNK2019: unresolved external symbol "int __cdecl ActivateHook(struct HINSTANCE__ *,long (__cdecl*)(int,unsigned int,long))" (?ActivateHook@@YAHPAUHINSTANCE__@@[email protected]@Z) referenced in function "protected: virtual int __cdec
l CHookTestDlg::OnInitDialog(void)" ([email protected]@@MAAHXZ)
HookTestDlg.obj : error LNK2019: unresolved external symbol "long __cdecl KbdHookCallback(int,unsigned int,long)" (?KbdHookCallback@@[email protected]) referenced in function "protected: virtual int __cdecl CHookTestDlg::OnInitDialog(void)" ([email protected]
ookTestDlg@@MAAHXZ)
ARMV4IDbg/HookTest.exe : fatal error LNK1120: 2 unresolved externals