我用EVC4开发,基于ARM9内核的WinCE5.0系统。我的思路是先建立一个DLL工程,采用全局钩子,代码如下:
//**************************************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__@@P6AJHIJ@Z@Z) referenced in function "protected: virtual int __cdec
l CHookTestDlg::OnInitDialog(void)" (?OnInitDialog@CHookTestDlg@@MAAHXZ)
HookTestDlg.obj : error LNK2019: unresolved external symbol "long __cdecl KbdHookCallback(int,unsigned int,long)" (?KbdHookCallback@@YAJHIJ@Z) referenced in function "protected: virtual int __cdecl CHookTestDlg::OnInitDialog(void)" (?OnInitDialog@CH
ookTestDlg@@MAAHXZ)
ARMV4IDbg/HookTest.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
HookTest.exe - 3 error(s), 0 warning(s)
问题内容比较长,但希望大家帮帮忙,我整了太久了,没有多少思路啦!谢谢!!!
2 个解决方案
#2
按你的代码依旧在EVC下依旧有问题,问题在DLL工程里面,invalid integer constant expression指示#if _DEBUG这一行,是否版本太低还是我缺少了什么代码?
急!!!
#1
#2
按你的代码依旧在EVC下依旧有问题,问题在DLL工程里面,invalid integer constant expression指示#if _DEBUG这一行,是否版本太低还是我缺少了什么代码?
急!!!