source: trunk/testapp/keyboard/OKeyHook/OKeyHook.cpp@ 10367

Last change on this file since 10367 was 7353, checked in by phaller, 24 years ago

.

File size: 4.4 KB
Line 
1 // OKeyHook.cpp : Defines the entry point for the DLL application.
2//
3
4#include "stdafx.h"
5#include "OKeyHook.h"
6
7
8
9
10#ifdef __cplusplus
11 extern "C" {
12#endif
13
14//
15// exported functions
16//
17#define OKEYTEST_IMPORT __declspec(dllimport)
18
19typedef (* OKEYTEST_IMPORT TpfnLogString)(LPSTR lpstrOrigin, LPSTR lpstrText);
20typedef (* OKEYTEST_IMPORT TpfnLogMessage)(LPSTR lpstrOrigin, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
21
22
23//
24// module global functions
25//
26TpfnLogString pfnLogString = NULL;
27TpfnLogMessage pfnLogMessage = NULL;
28HHOOK hhookStandard; // standard keyboard hook
29HHOOK hhookLowLevel; // lowlevel keyboard hook (NT4)
30HOOKPROC hpStandard;
31HOOKPROC hpLowLevel;
32HMODULE hHOOKDLL;
33
34#ifndef WH_KEYBOARD_LL
35#define WH_KEYBOARD_LL 13
36#endif
37
38
39//
40// prototypes
41//
42void OKEYHOOK_API hook_Enable(BOOL fEnable);
43LRESULT CALLBACK hook_Standard(int code, WPARAM wParam, LPARAM lParam);
44LRESULT CALLBACK hook_LowLevel(INT nCode, WPARAM wParam, LPARAM lParam);
45
46
47
48BOOL APIENTRY DllMain( HANDLE hModule,
49 DWORD ul_reason_for_call,
50 LPVOID lpReserved
51 )
52{
53 switch (ul_reason_for_call)
54 {
55 case DLL_PROCESS_ATTACH:
56 hHOOKDLL = (HMODULE)hModule;
57 break;
58
59 case DLL_THREAD_ATTACH:
60 case DLL_THREAD_DETACH:
61 case DLL_PROCESS_DETACH:
62 hook_Enable(FALSE);
63 break;
64 }
65 return TRUE;
66}
67
68
69
70//
71// enable / disable hooks
72//
73void OKEYHOOK_API hook_Enable(BOOL fEnable)
74{
75 if (fEnable == TRUE)
76 {
77 HINSTANCE hInstExe = LoadLibrary("OKEYTEST.EXE");
78 if (NULL == hInstExe)
79 return;
80
81 pfnLogString = (TpfnLogString)GetProcAddress(hInstExe, "logString");
82 if (NULL == pfnLogString)
83 return;
84
85 pfnLogMessage = (TpfnLogMessage)GetProcAddress(hInstExe, "logMessage");
86 if (NULL == pfnLogMessage)
87 return;
88
89 hpStandard = (HOOKPROC)&hook_Standard;
90 hpLowLevel = (HOOKPROC)&hook_LowLevel;
91
92 // enable the hooks
93 if ( (NULL == hhookStandard) && (NULL != hpStandard) )
94 {
95 hhookStandard = SetWindowsHookEx(WH_KEYBOARD, hpStandard, hHOOKDLL, 0);
96 if (NULL != hhookStandard)
97 pfnLogString("OKEYHOOK", "Standard keyboard hook was installed");
98 }
99
100 if ( (hhookLowLevel == NULL) && (NULL != hpLowLevel) )
101 {
102 hhookLowLevel = SetWindowsHookEx(WH_KEYBOARD_LL, hpLowLevel, hHOOKDLL, 0);
103 if (NULL != hhookLowLevel)
104 pfnLogString("OKEYHOOK", "Low-Level keyboard hook was installed");
105 }
106 }
107 else
108 {
109 if (NULL != hhookStandard)
110 {
111 if (FALSE == UnhookWindowsHookEx(hhookStandard))
112 pfnLogString("OKEYHOOK", "Standard keyboard hook was not uninstalled");
113 else
114 {
115 pfnLogString("OKEYHOOK", "Standard keyboard hook was uninstalled");
116 hhookStandard = NULL;
117 }
118 }
119
120 if (NULL != hhookLowLevel)
121 {
122 if (FALSE == UnhookWindowsHookEx(hhookLowLevel))
123 pfnLogString("OKEYHOOK", "Low-Level keyboard hook was not uninstalled");
124 else
125 {
126 pfnLogString("OKEYHOOK", "Low-Level keyboard hook was uninstalled");
127 hhookLowLevel = NULL;
128 }
129 }
130 }
131}
132
133
134//
135// standard windows keyboard hook
136//
137
138LRESULT CALLBACK hook_Standard(int nCode, WPARAM wParam, LPARAM lParam)
139{
140 // get the extra info
141 DWORD dwExtra = GetMessageExtraInfo();
142
143 // get the key's name
144 CHAR szBufKey[40];
145 GetKeyNameText(lParam, szBufKey, sizeof(szBufKey));
146
147 // log the message
148 CHAR szBuf[512];
149 wsprintf(szBuf,
150 "nCode=%08xh, wParam=%08xh, lParam=%08xh, extra=%08xh, name=%s",
151 nCode,
152 wParam,
153 lParam,
154 dwExtra,
155 szBufKey);
156
157 pfnLogString("KeyHook", szBuf);
158
159
160 // call next hook
161 if (nCode >= 0)
162 return CallNextHookEx(hhookStandard, nCode, wParam, lParam);
163 else
164 return 0;
165}
166
167
168
169typedef struct tagKBDLLHOOKSTRUCT {
170 DWORD vkCode;
171 DWORD scanCode;
172 DWORD flags;
173 DWORD time;
174 DWORD dwExtraInfo;
175} KBDLLHOOKSTRUCT, *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
176
177
178// This is for NT only
179LRESULT CALLBACK hook_LowLevel(INT nCode, WPARAM wParam, LPARAM lParam)
180{
181 // look into the structure
182 // @@@PH
183 LPKBDLLHOOKSTRUCT pkbhs = (LPKBDLLHOOKSTRUCT)lParam;
184
185 // log the message
186 CHAR szBuf[512];
187 wsprintf(szBuf,
188 "nCode=%08xh wParam=%08xh vkCode=%04xh scancode=%04xh, flags=%04xh, extra=%08xh, time=%08xh",
189 nCode,
190 wParam,
191 pkbhs->vkCode,
192 pkbhs->scanCode,
193 pkbhs->flags,
194 pkbhs->dwExtraInfo,
195 pkbhs->time);
196
197 pfnLogString("LowLevel", szBuf);
198
199 // call next hook
200 if (nCode >= 0)
201 return CallNextHookEx(hhookLowLevel, nCode, wParam, lParam);
202 else
203 return 0;
204}
205
206
207
208#ifdef __cplusplus
209 }
210#endif
211
Note: See TracBrowser for help on using the repository browser.