|
|
1.1 root 1:
2: /******************************************************************************\
3: * This is a part of the Microsoft Source Code Samples.
4: * Copyright (C) 1993 Microsoft Corporation.
5: * All rights reserved.
6: * This source code is only intended as a supplement to
7: * Microsoft Development Tools and/or WinHelp documentation.
8: * See these sources for detailed information regarding the
9: * Microsoft samples programs.
10: \******************************************************************************/
11:
12: #include <windows.h> // includes basic windows functionality
13: #include <stdio.h> // includes standard file i/o functionality
14: #include <string.h> // includes string functions
15: #include "hooktest.h"
16:
17: #define NUMHOOKS 7
18:
19: /* Global variables */
20:
21: typedef struct _MYHOOKDATA {
22: int nType;
23: HOOKPROC hkprc;
24: HHOOK hhook;
25: } MYHOOKDATA;
26:
27: MYHOOKDATA myhookdata[NUMHOOKS];
28:
29: HWND hwndMain;
30: HANDLE hInst;
31:
32:
33: /****************************************************************************
34: *
35: * FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
36: *
37: * PURPOSE: calls initialization function, processes message loop
38: *
39: * COMMENTS:
40: *
41: *
42: ****************************************************************************/
43:
44: int APIENTRY WinMain(
45: HINSTANCE hInstance,
46: HINSTANCE hPrevInstance,
47: LPSTR lpCmdLine,
48: int nCmdShow
49: )
50: {
51:
52: MSG msg; /* message */
53:
54: if (!InitApplication(hInstance)) /* Initialize shared things */
55: return (FALSE); /* Exits if unable to initialize */
56:
57: hInst = hInstance;
58:
59: /* Perform initializations that apply to a specific instance */
60:
61: if (!InitInstance(hInstance, nCmdShow))
62: return (FALSE);
63:
64: // register window message for FindText() and ReplaceText() hook procs
65: /* Acquire and dispatch messages until a WM_QUIT message is received. */
66:
67: while (GetMessage(&msg, /* message structure */
68: NULL, /* handle of window receiving the message */
69: 0, /* lowest message to examine */
70: 0)) /* highest message to examine */
71: {
72: TranslateMessage(&msg); /* Translates virtual key codes */
73: DispatchMessage(&msg); /* Dispatches message to window */
74: }
75: return (msg.wParam); /* Returns the value from PostQuitMessage */
76:
77: // avoid compiler warnings at W3
78: lpCmdLine;
79: hPrevInstance;
80: }
81:
82:
83: /****************************************************************************
84: *
85: * FUNCTION: InitApplication(HANDLE)
86: *
87: * PURPOSE: Initializes window data and registers window class
88: *
89: * COMMENTS:
90: *
91: * In this function, we initialize a window class by filling out a data
92: * structure of type WNDCLASS and calling the Windows RegisterClass()
93: * function.
94: *
95: ****************************************************************************/
96:
97: BOOL InitApplication(HANDLE hInstance) /* current instance */
98: {
99: WNDCLASS wc;
100:
101: /* Fill in window class structure with parameters that describe the */
102: /* main window. */
103:
104: wc.style = 0; /* Class style(s). */
105: wc.lpfnWndProc = (WNDPROC)MainWndProc; /* Function to retrieve messages for */
106: /* windows of this class. */
107: wc.cbClsExtra = 0; /* No per-class extra data. */
108: wc.cbWndExtra = 0; /* No per-window extra data. */
109: wc.hInstance = hInstance; /* Application that owns the class. */
110: wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
111: wc.hCursor = LoadCursor(NULL, IDC_ARROW);
112: wc.hbrBackground = GetStockObject(WHITE_BRUSH);
113: wc.lpszMenuName = "HookMenu"; /* Name of menu resource in .RC file. */
114: wc.lpszClassName = "HookWClass"; /* Name used in call to CreateWindow. */
115:
116: /* Register the window class and return success/failure code. */
117:
118: return (RegisterClass(&wc));
119:
120: }
121:
122:
123: /****************************************************************************
124: *
125: * FUNCTION: InitInstance(HANDLE, int)
126: *
127: * PURPOSE: Saves instance handle and creates main window
128: *
129: * COMMENTS:
130: *
131: * In this function, we save the instance handle in a static variable and
132: * create and display the main program window.
133: *
134: ****************************************************************************/
135:
136: BOOL InitInstance(
137: HANDLE hInstance, /* Current instance identifier. */
138: int nCmdShow) /* Param for first ShowWindow() call. */
139: {
140:
141: /* Save the instance handle in static variable, which will be used in */
142: /* many subsequence calls from this application to Windows. */
143:
144: hInst = hInstance;
145:
146: /* Create a main window for this application instance. */
147:
148: hwndMain = CreateWindow(
149: "HookWClass", /* See RegisterClass() call. */
150: "Hooks Sample Application", /* Text for window title bar. */
151: WS_OVERLAPPEDWINDOW, /* Window style. */
152: CW_USEDEFAULT, /* Default horizontal position. */
153: CW_USEDEFAULT, /* Default vertical position. */
154: CW_USEDEFAULT, /* Default width. */
155: CW_USEDEFAULT, /* Default height. */
156: NULL, /* Overlapped windows have no parent. */
157: NULL, /* Use the window class menu. */
158: hInstance, /* This instance owns this window. */
159: NULL /* Pointer not needed. */
160: );
161:
162: /* If window could not be created, return "failure" */
163:
164: if (!hwndMain)
165: return (FALSE);
166:
167: /* Make the window visible; update its client area; and return "success" */
168:
169: ShowWindow(hwndMain, nCmdShow); /* Show the window */
170: UpdateWindow(hwndMain); /* Sends WM_PAINT message */
171: return (TRUE); /* Returns the value from PostQuitMessage */
172:
173: }
174:
175: /****************************************************************************
176: *
177: * FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
178: *
179: * PURPOSE: Processes messages
180: *
181: * COMMENTS:
182: *
183: * This function processes all messages sent to the window. When the
184: * user chooses one of the options from one of the menus, the command
185: * is processed here and passed onto the function for that command.
186: * This function also processes the special "FindReplace" message that
187: * this application registers for hook processing of the FindText()
188: * and ReplaceText() common dialog functions.
189: *
190: ****************************************************************************/
191:
192:
193:
194: LONG APIENTRY MainWndProc(hwndMain, uMsg, wParam, lParam)
195: HWND hwndMain;
196: UINT uMsg;
197: UINT wParam;
198: LONG lParam;
199: {
200: static BOOL aHooks[NUMHOOKS];
201: int index;
202: static HMENU hmenu;
203:
204:
205: switch (uMsg) {
206: case WM_CREATE:
207:
208: /* Save the menu handle. */
209:
210: hmenu = GetMenu(hwndMain);
211:
212: /*
213: * Initialize structures with hook data. The menu-item
214: * identifiers are defined as 0 through 6 in the
215: * header file. They can be used to identify array
216: * elements both here and during the WM_COMMAND
217: * message.
218: */
219:
220: myhookdata[IDM_CALLWNDPROC].nType = WH_CALLWNDPROC;
221: myhookdata[IDM_CALLWNDPROC].hkprc = CallWndProc;
222:
223: myhookdata[IDM_CBT].nType = WH_CBT;
224: myhookdata[IDM_CBT].hkprc = CBTProc;
225: myhookdata[IDM_DEBUG].nType = WH_DEBUG;
226: myhookdata[IDM_DEBUG].hkprc = DebugProc;
227: myhookdata[IDM_GETMESSAGE].nType = WH_GETMESSAGE;
228: myhookdata[IDM_GETMESSAGE].hkprc = GetMsgProc;
229: myhookdata[IDM_KEYBOARD].nType = WH_KEYBOARD;
230:
231: myhookdata[IDM_KEYBOARD].hkprc = KeyboardProc;
232: myhookdata[IDM_MOUSE].nType = WH_MOUSE;
233: myhookdata[IDM_MOUSE].hkprc = MouseProc;
234: myhookdata[IDM_MSGFILTER].nType = WH_MSGFILTER;
235: myhookdata[IDM_MSGFILTER].hkprc = MessageProc;
236:
237: /* Initialize all flags in the array to FALSE. */
238: memset(aHooks, FALSE, sizeof(aHooks));
239:
240: return 0;
241:
242: case WM_COMMAND:
243:
244: switch (LOWORD(wParam)) {
245:
246: case IDM_EXIT:
247: PostQuitMessage(0);
248: break;
249:
250: /*
251: * The user selected a hook command from the menu.
252: */
253:
254: case IDM_CALLWNDPROC:
255: case IDM_CBT:
256: case IDM_DEBUG:
257: case IDM_GETMESSAGE:
258: case IDM_KEYBOARD:
259: case IDM_MOUSE:
260: case IDM_MSGFILTER:
261:
262: /*
263: * Use the menu-item identifier as an index
264: * into the array of structures with hook data.
265:
266: */
267:
268: index = LOWORD(wParam);
269:
270: /*
271: * If the selected type of hook procedure isn't
272: * installed yet, install it and check the
273: * associated menu item.
274: */
275:
276: if (!aHooks[index]) {
277: myhookdata[index].hhook = SetWindowsHookEx(
278: myhookdata[index].nType,
279: myhookdata[index].hkprc,
280: (HINSTANCE) NULL, GetCurrentThreadId());
281: CheckMenuItem(hmenu, index,
282: MF_BYCOMMAND | MF_CHECKED);
283: aHooks[index] = TRUE;
284: }
285:
286: /*
287: * If the selected type of hook procedure is
288: * already installed, remove it and remove the
289: * check mark from the associated menu item.
290: */
291:
292:
293: else {
294: UnhookWindowsHookEx(myhookdata[index].hhook);
295: CheckMenuItem(hmenu, index,
296: MF_BYCOMMAND | MF_UNCHECKED);
297: aHooks[index] = FALSE;
298: InvalidateRect(hwndMain, NULL, TRUE);
299: UpdateWindow(hwndMain);
300: }
301:
302: }
303: break;
304:
305: case WM_DESTROY:
306: PostQuitMessage(0);
307: break;
308:
309: default:
310: return DefWindowProc(hwndMain, uMsg, wParam, lParam);
311: }
312: return 0;
313: }
314:
315: /****************************************************************
316: WH_CALLWNDPROC hook procedure
317: ****************************************************************/
318:
319: LRESULT CALLBACK CallWndProc(nCode, wParam, lParam)
320: int nCode;
321: WPARAM wParam;
322: LPARAM lParam;
323: {
324: CHAR szCWPBuf[256];
325: HDC hdc;
326:
327: static int c = 0;
328: int cch;
329:
330: if (nCode < 0) /* do not process message */
331: return CallNextHookEx(myhookdata[IDM_CALLWNDPROC].hhook, nCode,
332: wParam, lParam);
333:
334: hdc = GetDC(hwndMain);
335:
336: switch (nCode) {
337: case HC_ACTION:
338: cch = wsprintf(szCWPBuf,
339: "CALLWNDPROC - tsk: %ld, msg: %d, %d times %20s",
340: wParam, nCode, c++, "" );
341: TextOut(hdc, 2, 15, szCWPBuf, cch);
342: break;
343:
344: default:
345: break;
346: }
347:
348: ReleaseDC(hwndMain, hdc);
349: return CallNextHookEx(myhookdata[IDM_CALLWNDPROC].hhook, nCode,
350: wParam, lParam);
351: }
352:
353: /****************************************************************
354: WH_GETMESSAGE hook procedure
355: ****************************************************************/
356:
357: LRESULT CALLBACK GetMsgProc(nCode, wParam, lParam)
358: int nCode;
359: WPARAM wParam;
360:
361: LPARAM lParam;
362: {
363: CHAR szMSGBuf[256];
364: CHAR szRem[16];
365: HDC hdc;
366: static int c = 0;
367: int cch;
368:
369: if (nCode < 0) /* do not process message */
370: return CallNextHookEx(myhookdata[IDM_GETMESSAGE].hhook, nCode,
371: wParam, lParam);
372:
373: switch (nCode) {
374: case HC_ACTION:
375: switch (wParam) {
376: case PM_REMOVE:
377: lstrcpy(szRem, "PM_REMOVE");
378: break;
379:
380: case PM_NOREMOVE:
381: lstrcpy(szRem, "PM_NOREMOVE");
382: break;
383:
384: default:
385: lstrcpy(szRem, "Unknown");
386: break;
387: }
388:
389: hdc = GetDC(hwndMain);
390:
391: cch = wsprintf(szMSGBuf,
392: "GETMESSAGE - wParam: %s, msg: %d, %d times %20s",
393: szRem, nCode, c++, "");
394: TextOut(hdc, 2, 35, szMSGBuf, cch);
395: break;
396:
397: default:
398: break;
399: }
400:
401: ReleaseDC(hwndMain, hdc);
402: return CallNextHookEx(myhookdata[IDM_GETMESSAGE].hhook, nCode,
403: wParam, lParam);
404: }
405:
406: /****************************************************************
407: WH_DEBUG hook procedure
408: ****************************************************************/
409:
410: LRESULT CALLBACK DebugProc(nCode, wParam, lParam)
411:
412: int nCode;
413: WPARAM wParam;
414: LPARAM lParam;
415: {
416: CHAR szBuf[128];
417: HDC hdc;
418: static int c = 0;
419: int cch;
420:
421: if (nCode < 0) /* do not process message */
422: return CallNextHookEx(myhookdata[IDM_DEBUG].hhook, nCode,
423: wParam, lParam);
424:
425: hdc = GetDC(hwndMain);
426:
427: switch (nCode) {
428: case HC_ACTION:
429: cch = wsprintf(szBuf,
430: "DEBUG - nCode: %d, tsk: %ld, %d times %20s",
431: nCode,wParam, c++, "");
432: TextOut(hdc, 2, 55, szBuf, cch);
433: break;
434:
435: default:
436: break;
437: }
438:
439: ReleaseDC(hwndMain, hdc);
440: return CallNextHookEx(myhookdata[IDM_DEBUG].hhook, nCode, wParam,
441: lParam);
442: }
443:
444: /****************************************************************
445: WH_CBT hook procedure
446: ****************************************************************/
447:
448: LRESULT CALLBACK CBTProc(nCode, wParam, lParam)
449: int nCode;
450: WPARAM wParam;
451: LPARAM lParam;
452:
453: {
454: CHAR szBuf[128];
455: CHAR szCode[128];
456: HDC hdc;
457: static int c = 0;
458: int cch;
459:
460: if (nCode < 0) /* do not process message */
461: return CallNextHookEx(myhookdata[IDM_CBT].hhook, nCode, wParam,
462: lParam);
463:
464: hdc = GetDC(hwndMain);
465:
466: switch (nCode) {
467: case HCBT_ACTIVATE:
468: lstrcpy(szCode, "HCBT_ACTIVATE");
469: break;
470:
471: case HCBT_CLICKSKIPPED:
472: lstrcpy(szCode, "HCBT_CLICKSKIPPED");
473: break;
474:
475: case HCBT_CREATEWND:
476: lstrcpy(szCode, "HCBT_CREATEWND");
477: break;
478:
479: case HCBT_DESTROYWND:
480: lstrcpy(szCode, "HCBT_DESTROYWND");
481: break;
482:
483: case HCBT_KEYSKIPPED:
484: lstrcpy(szCode, "HCBT_KEYSKIPPED");
485: break;
486:
487: case HCBT_MINMAX:
488: lstrcpy(szCode, "HCBT_MINMAX");
489: break;
490:
491: case HCBT_MOVESIZE:
492: lstrcpy(szCode, "HCBT_MOVESIZE");
493: break;
494:
495: case HCBT_QS:
496: lstrcpy(szCode, "HCBT_QS");
497: break;
498:
499: case HCBT_SETFOCUS:
500: lstrcpy(szCode, "HCBT_SETFOCUS");
501: break;
502:
503: case HCBT_SYSCOMMAND:
504:
505: lstrcpy(szCode, "HCBT_SYSCOMMAND");
506: break;
507:
508: default:
509: lstrcpy(szCode, "Unknown");
510: break;
511: }
512:
513: cch = wsprintf(szBuf, "CBT - nCode: %s, tsk: %ld, %d times %20s",
514: szCode, wParam, c++, "");
515: TextOut(hdc, 2, 75, szBuf, cch);
516: ReleaseDC(hwndMain, hdc);
517: return CallNextHookEx(myhookdata[IDM_CBT].hhook, nCode, wParam,
518: lParam);
519: }
520:
521: /****************************************************************
522: WH_MOUSE hook procedure
523:
524: ****************************************************************/
525:
526: LRESULT CALLBACK MouseProc(nCode, wParam, lParam)
527: int nCode;
528: WPARAM wParam;
529: LPARAM lParam;
530: {
531: CHAR szBuf[128];
532: HDC hdc;
533: static int c = 0;
534: int cch;
535:
536: if (nCode < 0) /* do not process the message */
537: return CallNextHookEx(myhookdata[IDM_MOUSE].hhook, nCode,
538: wParam, lParam);
539:
540:
541: hdc = GetDC(hwndMain);
542: cch = wsprintf(szBuf,
543: "MOUSE - nCode: %d, msg: %d, x: %d, y: %d, %d times %20s",
544: nCode, nCode, LOWORD(lParam), HIWORD(lParam), c++, "" );
545: TextOut(hdc, 2, 95, szBuf, cch);
546: ReleaseDC(hwndMain, hdc);
547: return CallNextHookEx(myhookdata[IDM_MOUSE].hhook, nCode, wParam,
548:
549: lParam);
550: }
551:
552: /****************************************************************
553: WH_KEYBOARD hook procedure
554: ****************************************************************/
555:
556: LRESULT CALLBACK KeyboardProc(nCode, wParam, lParam)
557: int nCode;
558: WPARAM wParam;
559: LPARAM lParam;
560: {
561: CHAR szBuf[128];
562: HDC hdc;
563: static int c = 0;
564: int cch;
565:
566: if (nCode < 0) /* do not process message */
567: return CallNextHookEx(myhookdata[IDM_KEYBOARD].hhook, nCode,
568: wParam, lParam);
569:
570: hdc = GetDC(hwndMain);
571:
572: cch = wsprintf(szBuf, "KEYBOARD - nCode: %d, vk: %d, %d times %20s",
573: nCode, wParam, c++, "" );
574: TextOut(hdc, 2, 115, szBuf, cch);
575: ReleaseDC(hwndMain, hdc);
576: return CallNextHookEx(myhookdata[IDM_KEYBOARD].hhook, nCode, wParam,
577: lParam);
578: }
579:
580: /****************************************************************
581: WH_MSGFILTER hook procedure
582: ****************************************************************/
583:
584: LRESULT CALLBACK MessageProc(nCode, wParam, lParam)
585: int nCode;
586: WPARAM wParam;
587:
588: LPARAM lParam;
589: {
590: CHAR szBuf[128];
591: CHAR szCode[32];
592: HDC hdc;
593: static int c = 0;
594: int cch;
595:
596: if (nCode < 0) /* do not process message */
597: return CallNextHookEx(myhookdata[IDM_MSGFILTER].hhook, nCode,
598: wParam, lParam);
599:
600: switch (nCode) {
601: case MSGF_DIALOGBOX:
602: lstrcpy(szCode, "MSGF_DIALOGBOX");
603: break;
604:
605:
606: case MSGF_MENU:
607: lstrcpy(szCode, "MSGF_MENU");
608: break;
609:
610: case MSGF_SCROLLBAR:
611: lstrcpy(szCode, "MSGF_SCROLLBAR");
612: break;
613:
614: case MSGF_NEXTWINDOW:
615: lstrcpy(szCode, "MSGF_NEXTWINDOW");
616: break;
617:
618: default:
619: wsprintf(szCode, "Unknown: %d", nCode);
620: break;
621: }
622:
623: hdc = GetDC(hwndMain);
624: cch = wsprintf(szBuf,
625: "MSGFILTER nCode: %s, msg: %d, %d times %20s",
626: szCode, nCode, c++, "" );
627: TextOut(hdc, 2, 135, szBuf, cch);
628: ReleaseDC(hwndMain, hdc);
629: return CallNextHookEx(myhookdata[IDM_MSGFILTER].hhook, nCode,
630: wParam, lParam);
631: }
632:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.