wince下支持三种钩子:
1.#define WH_JOURNALRECORD 0使应用程序可以监视输入事件。典型地,应用程序使用该HOOK记录鼠标、键盘输入事件以供以后回放。该HOOK是全局HOOK,并且不能在指定线程中使用。
2.#define WH_JOURNALPLAYBACK 1使应用程序可以向系统消息队列中插入消息。该HOOK可以回放以前由WH_JOURNALRECORD HOOK录制的鼠标、键盘输入事件。在WH_JOURNALPLAYBACK Hook安装到系统时,鼠标、键盘输入事件将被屏蔽。该HOOK同样是一个全局HOOK,不能在指定线程中使用。
WH_JOURNALPLAYBACK Hook返回一个时间暂停值,它告诉系统,在处理当前回放的消息时,系统等待百分之几秒。这使得此HOOK可以控制在回放时的时间事件
3.#define WH_KEYBOARD_LL 20
其中最常用的是键盘钩子,其它两个偶没有用过。
1.
通过SetWindowsHookEx ()函数。
2.
UnhookWindowsHookEx()函数。
3.
函数HookProc。
4.
CallNexHookEx()函数。
钩子的建立
1.
// KeyBoardHook.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "KeyBoardHook.h"
#include <Pwinuser.h>
#include "BasalMessage.h"
//#include "FileManage.h"
//告诉编译器将变量放入它自己的数据共享节中
#pragma data_seg("KeyHookData")
HINSTANCE hInst = NULL;
#pragma data_seg()
//告诉编译器设置共享节的访问方式为:读,写,共享
#pragma comment(linker, "/SECTION:KeyHookData,RWS")
BOOL APIENTRY DllMain( HANDLE hModule,
{
{
}
// This is an example of an exported variable
KEYBOARDHOOK_API int nKeyBoardHook=0;
// This is an example of an exported function.
KEYBOARDHOOK_API int fnKeyBoardHook(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see KeyBoardHook.h for the class definition
CKeyBoardHook::CKeyBoardHook()
{
return;
}
extern "C" KEYBOARDHOOK_API void InstallHook(void)
{
if (hInst)
{
}
}
extern "C" KEYBOARDHOOK_API void UnHook(void)
{
if (hKeyHook)
{
}
hInst = NULL;
}
extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
TCHAR t_WndClassName[50] = { 0 };
HWND hCurActiveWnd = NULL;
HWND hCurForegroundWnd = NULL;
BOOL bIsTaskBarMsg = FALSE;
BOOL bIsOEMmsg = FALSE;
PKBDLLHOOKSTRUCT pkbhs = (PKBDLLHOOKSTRUCT)lParam;
if (WM_KEYDOWN == wParam)
{
case 0x31:
case 0x32:
case 0x33:
case 0x34:
case 0x35:
case 0x36:
case 0x37:
case 0x38:
case 0x39:
case 0x30:
}
case 0x30:
case 0x31:
case 0x32:
case 0x33:
case 0x34:
case 0x35:
case 0x36:
case 0x37:
case 0x38:
case 0x39:
}
else
{
}
return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
}
extern "C" KEYBOARDHOOK_API void SetAppHWND(HWND hCurAppWnd)
{
hAppWnd = hCurAppWnd;
}
extern "C" KEYBOARDHOOK_API void SetTopHWND(HWND hCurTopWnd)
{
hTopWnd = hCurTopWnd;
}
extern "C" KEYBOARDHOOK_API void SetHoldUpMsg(BOOL bHoldUp)
{
bHoldUpMsg = bHoldUp;
}
extern "C" KEYBOARDHOOK_API void SetOnlySendOEMMsg(BOOL bOnlySendOEM)
{
bOnlySendOEMMsg = bOnlySendOEM;
}
extern "C" KEYBOARDHOOK_API void SetNeedPassOnceMsg(BOOL bWhetherNeed)
{
bNeedPassOnceMsg = bWhetherNeed;
}
2.
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the KEYBOARDHOOK_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// KEYBOARDHOOK_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef KEYBOARDHOOK_EXPORTS
#define KEYBOARDHOOK_API __declspec(dllexport)
#else
#define KEYBOARDHOOK_API __declspec(dllimport)
#endif
// This class is exported from the KeyBoardHook.dll
class KEYBOARDHOOK_API CKeyBoardHook {
public:
CKeyBoardHook(void);
// TODO: add your methods here.
};
extern KEYBOARDHOOK_API int nKeyBoardHook;
KEYBOARDHOOK_API int fnKeyBoardHook(void);
extern "C" KEYBOARDHOOK_API void InstallHook(void);
extern "C" KEYBOARDHOOK_API void UnHook(void);
extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam);
extern "C" KEYBOARDHOOK_API void SetAppHWND(HWND hCurAppWnd);
extern "C" KEYBOARDHOOK_API void SetTopHWND(HWND hCurTopWnd);
extern "C" KEYBOARDHOOK_API void SetHoldUpMsg(BOOL bHoldUp);
extern "C" KEYBOARDHOOK_API void SetOnlySendOEMMsg(BOOL bOnlySendOEM);
extern "C" KEYBOARDHOOK_API void SetNeedPassOnceMsg(BOOL bWhetherNeed);
3.
if (hModule)
InHook();
DLL的编写,也可以参考以下网址中的内容:
http://www.bc-cn.net/Article/kfyy/cyy/jszl/200709/6328_2.html
WinCE上支持三种类型的Hook
#define WH_JOURNALRECORD 0
#define WH_JOURNALPLAYBACK 1
#define WH_KEYBOARD_LL 20
不过前两个使用的时候是使用如下函数:
QASetWindowsJournalHook
QAUnhookWindowsJournalHo
后者使用SetWindowsHookExW和UnhookWindowsHookEx