KeyHookLib.h
1#ifdef KEYHOOKLIB_EXPORTS
2#define KEYHOOKLIB_API __declspec(dllexport)
3#else
4#define KEYHOOKLIB_API __declspec(dllimport)
5#endif
6
7//自定义消息
8#define HM_KEY WM_USER + 101
9//声明输出函数
10BOOL KEYHOOKLIB_API WINAPI SetKeyHook(BOOL bInstall,DWORD dwThreadId = 0,HWND hWndCaller = NULL);
11 KeyHookLib.cpp
1#include "stdafx.h"
2#include <Windows.h>
3#include "KeyHookLib.h"
4
5//共享数据段
6#pragma data_seg("YCIShared")
7HWND g_hWndCaller = NULL;
8HHOOK g_hHook = NULL;
9#pragma data_seg()
10
11BOOL APIENTRY DllMain( HANDLE hModule,
12 DWORD ul_reason_for_call,
13 LPVOID lpReserved
14 )
15{
16 switch (ul_reason_for_call)
17 {
18 case DLL_PROCESS_ATTACH:
19 case DLL_THREAD_ATTACH:
20 case DLL_THREAD_DETACH:
21 case DLL_PROCESS_DETACH:
22 break;
23 }
24 return TRUE;
25}
26
27//帮助函数(根据处理函数的地址,得到处理函数所在模块的基址)
28HMODULE WINAPI ModuleFromAddress(PVOID pv)
29{
30 MEMORY_BASIC_INFORMATION mbi;
31 if (::VirtualQuery(pv,&mbi,sizeof(mbi)) != 0)
32 {
33 return (HMODULE)mbi.AllocationBase;
34 }
35 else
36 {
37 return NULL;
38 }
39}
40
41//钩子处理函数
42LRESULT CALLBACK KeyHookProc(int nCode,WPARAM wParam,LPARAM lParam)
43{
44 if (nCode < 0 || nCode == HC_NOREMOVE)
45 {
46 return ::CallNextHookEx(g_hHook,nCode,wParam,lParam);
47 }
48 if (lParam & 0x40000000)
49 {
50 return ::CallNextHookEx(g_hHook,nCode,wParam,lParam);
51 }
52 ::PostMessage(g_hWndCaller,HM_KEY,wParam,lParam);
53 return ::CallNextHookEx(g_hHook,nCode,wParam,lParam);
54}
55
56//定义输出函数(安装和卸载钩子)
57BOOL WINAPI SetKeyHook(BOOL bInstall,DWORD dwThreadId,HWND hWndCaller)
58{
59 BOOL bOk;
60 g_hWndCaller = hWndCaller;
61 if (bInstall)
62 {
63 g_hHook = ::SetWindowsHookEx(WH_KEYBOARD,KeyHookProc,ModuleFromAddress
64
65(KeyHookProc),dwThreadId);
66 bOk = (g_hHook != NULL);
67 }
68 else
69 {
70 bOk = ::UnhookWindowsHookEx(g_hHook);
71 g_hHook = NULL;
72 }
73 return bOk;
74} KeyHookLib.def
1EXPORTS
2 SetKeyHook
3SECTIONS
4 YCIShared Read Write Shared Dll的引用:
exe文件在初始化时,调用目标目标函数(装载消息钩子)
1// CKeyHookDlg 消息处理程序
2typedef BOOL (WINAPI FARFUN)(BOOL,DWORD,HWND);
3 FARFUN *p;
4BOOL CKeyHookDlg::OnInitDialog()
5{
6 CDialog::OnInitDialog();
7
8 // TODO: 在此添加额外的初始化代码
9 HINSTANCE hMyDll = LoadLibraryA("KeyHookLib.dll");
10 p = (FARFUN*)GetProcAddress(hMyDll,"SetKeyHook");
11 if (p)
12 {
13 p(TRUE,0,m_hWnd);
14 }
15 return TRUE;
16} 消息映射代码:当exe接收到键盘消息时,执行OnHookKey函数;当点取消按钮时,调用目标函数(卸载钩子)。
1BEGIN_MESSAGE_MAP(CKeyHookDlg, CDialog)
2 ON_WM_PAINT()
3 ON_WM_QUERYDRAGICON()
4 ON_MESSAGE(HM_KEY,OnHookKey)
5 ON_BN_CLICKED(IDCANCEL, &CKeyHookDlg::OnBnClickedCancel)
6END_MESSAGE_MAP()
1long CKeyHookDlg::OnHookKey(WPARAM wParam,LPARAM lParam)
2{
3 char szKey[80];
4 GetKeyNameText(lParam,szKey,80);
5 CString strItem;
6 strItem.Format("用户按键:%s\r\n",szKey);
7
8 CString strEdit;
9 GetDlgItem(IDC_EDIT)->GetWindowText(strEdit);
10 GetDlgItem(IDC_EDIT)->SetWindowText(strItem + strEdit);
11 MessageBeep(MB_OK);
12 return 0;
13}
1void CKeyHookDlg::OnBnClickedCancel()
2{
3 p(FALSE,0,m_hWnd);
4 OnCancel();
5 return;
6}
posted on 2012-08-31 01:43
寻步 阅读(504)
评论(0) 编辑 收藏 引用