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

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

Added keyboard test prog

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