TNB Library
TnbWindowsHooker.h
[詳解]
1#pragma once
13#include "TnbDef.h"
14
15
16
17//TNB Library
18namespace TNB
19{
20
21
22
23#ifndef _TnbDOXYGEN //Document作成用シンボル
24
25#if ( _WIN32_WINNT < 0x0400 )
26
27 #define WH_KEYBOARD_LL 13
28 #define WH_MOUSE_LL 14
29
30 /*
31 * Low level hook flags
32 */
33 #define LLKHF_EXTENDED (KF_EXTENDED >> 8)
34 #define LLKHF_INJECTED 0x00000010
35 #define LLKHF_ALTDOWN (KF_ALTDOWN >> 8)
36 #define LLKHF_UP (KF_UP >> 8)
37 #define LLMHF_INJECTED 0x00000001
38
39 /*
40 * Structure used by WH_KEYBOARD_LL
41 */
42 typedef struct tagKBDLLHOOKSTRUCT {
43 DWORD vkCode;
44 DWORD scanCode;
45 DWORD flags;
46 DWORD time;
47 ULONG dwExtraInfo;
48 } KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
49
50 /*
51 * Structure used by WH_MOUSE_LL
52 */
53 typedef struct tagMSLLHOOKSTRUCT {
54 POINT pt;
55 DWORD mouseData;
56 DWORD flags;
57 DWORD time;
58 ULONG dwExtraInfo;
59 } MSLLHOOKSTRUCT, FAR *LPMSLLHOOKSTRUCT, *PMSLLHOOKSTRUCT;
60
61#endif // (_WIN32_WINNT < 0x0400)
62
63#ifndef WH_HARDWARE
64 #define WH_HARDWARE 8
65#endif
66
67// ウィンドウズフック関係
68namespace WinHook
69{
70 #ifdef TnbGLOBALHOOK
71 #pragma comment(linker, "/section:.tnbwinhook,RWS")
72 #pragma data_seg(".tnbwinhook")
73 #endif
74
75 // ウィンドウフック対象のインスタンスハンドル
76 _SELECTANY HINSTANCE g_hInstance = NULL;
77 // ウィンドウフック対象のスレッドID
78 _SELECTANY DWORD g_dwThreadId = 0;
79
80 #define _WINHOOKMAC(NAME) \
81 _SELECTANY HHOOK s_hhk##NAME = NULL; \
82 static HHOOK Hook##NAME(bool b, HOOKPROC PF) { \
83 if ( ! b && s_hhk##NAME != NULL ){ \
84 ::UnhookWindowsHookEx(s_hhk##NAME); s_hhk##NAME = NULL; \
85 } \
86 if ( b && s_hhk##NAME == NULL ){ \
87 s_hhk##NAME = SetWindowsHookEx(WH_##NAME, PF, g_hInstance, g_dwThreadId); \
88 } \
89 return s_hhk##NAME; \
90 }
91
92 _WINHOOKMAC( MSGFILTER )
93 _WINHOOKMAC( JOURNALRECORD )
94 _WINHOOKMAC( JOURNALPLAYBACK)
95 _WINHOOKMAC( KEYBOARD )
96 _WINHOOKMAC( GETMESSAGE )
97 _WINHOOKMAC( CALLWNDPROC )
98 _WINHOOKMAC( CBT )
99 _WINHOOKMAC( SYSMSGFILTER )
100 _WINHOOKMAC( MOUSE )
101 _WINHOOKMAC( HARDWARE )
102 _WINHOOKMAC( DEBUG )
103 _WINHOOKMAC( SHELL )
104 _WINHOOKMAC( FOREGROUNDIDLE )
105 _WINHOOKMAC( CALLWNDPROCRET )
106 _WINHOOKMAC( KEYBOARD_LL )
107 _WINHOOKMAC( MOUSE_LL )
108 #ifdef TnbGLOBALHOOK
109 #pragma data_seg()
110 #endif
111 #undef _WINHOOKMAC
112};
113
114#endif //_TnbDOXYGEN
115
116
117
184{
185 typedef HHOOK (*LPHOOKFUNC)(bool, HOOKPROC);
186
187 //Unhook処理用サブクラス
188 class CReject
189 {
190 public: ~CReject(void);
191 };
192
193 // 指定のHookタイプの関数アドレス取得
194 static LPHOOKFUNC m_GetFunc(int iHookType)
195 {
196 switch ( iHookType )
197 {
198 case WH_MSGFILTER: return WinHook::HookMSGFILTER;
199 case WH_JOURNALRECORD: return WinHook::HookJOURNALRECORD;
200 case WH_JOURNALPLAYBACK:return WinHook::HookJOURNALPLAYBACK;
201 case WH_KEYBOARD: return WinHook::HookKEYBOARD;
202 case WH_GETMESSAGE: return WinHook::HookGETMESSAGE;
203 case WH_CALLWNDPROC : return WinHook::HookCALLWNDPROC;
204 case WH_CBT: return WinHook::HookCBT;
205 case WH_SYSMSGFILTER: return WinHook::HookSYSMSGFILTER;
206 case WH_MOUSE: return WinHook::HookMOUSE;
207 case WH_HARDWARE: return WinHook::HookHARDWARE;
208 case WH_DEBUG: return WinHook::HookDEBUG;
209 case WH_SHELL: return WinHook::HookSHELL;
210 case WH_FOREGROUNDIDLE: return WinHook::HookFOREGROUNDIDLE;
211 case WH_CALLWNDPROCRET: return WinHook::HookCALLWNDPROCRET;
212 case WH_KEYBOARD_LL: return WinHook::HookKEYBOARD_LL;
213 case WH_MOUSE_LL: return WinHook::HookMOUSE_LL;
214 default: break;
215 }
216 return NULL;
217 }
218
223 CWindowsHooker(void);
224
225public:
226
235 static void OnDllMain(HANDLE hModule, DWORD reason)
236 {
237 switch ( reason )
238 {
239 case DLL_PROCESS_ATTACH:
240 WinHook::g_hInstance = static_cast<HINSTANCE>(hModule);
241 WinHook::g_dwThreadId = 0;
242 break;
243 case DLL_PROCESS_DETACH:
244 UnhookAll();
245 break;
246 case DLL_THREAD_ATTACH:
247 case DLL_THREAD_DETACH:
248 default:
249 break;
250 }
251 }
252
260 static void SetEnvironment(HINSTANCE hInstance, DWORD dwThreadId = 0)
261 {
262 WinHook::g_hInstance = hInstance;
263 WinHook::g_dwThreadId = dwThreadId;
264 }
265
274 static HHOOK Hook(int iHookType, HOOKPROC PF)
275 {
276 static CReject s_cReje;
277 LPHOOKFUNC P = m_GetFunc(iHookType);
278 if ( P != NULL ){ return P(true, PF); }
279 return NULL;
280 }
281
289 static bool Unhook(int iHookType)
290 {
291 LPHOOKFUNC P = m_GetFunc(iHookType);
292 if ( P != NULL ){ P(false, NULL); }
293 return P != NULL;
294 }
295
299 static void UnhookAll(void)
300 {
301 for ( int i = WH_MIN; i < 30; i++ )
302 {
303 Unhook(i);
304 }
305 }
306};
307
308
309
311inline CWindowsHooker::CReject::~CReject(void)
312{
314}
315
316
317
318};//TNB
TNBライブラリの定義ヘッダ
ウィンドウズフック管理
static bool Unhook(int iHookType)
[登録] フック解除
static HHOOK Hook(int iHookType, HOOKPROC PF)
[登録] フック登録
static void OnDllMain(HANDLE hModule, DWORD reason)
[設定] DllMain 時の処理 DLL のメイン関数がコールされた時にコールする関数です。 reason によって、処理を行います。
static void SetEnvironment(HINSTANCE hInstance, DWORD dwThreadId=0)
[設定] 環境登録
static void UnhookAll(void)
[登録] 全フックリスナー解除
TNB Library
Definition: TnbDoxyTitle.txt:2