|
|
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: /*****************************************************************************\
13: * hook.c - Windows message spy application dll
14: *
15: * Functions:
16: *
17: * DllMain()
18: * FindSpyWindow()
19: * HookProc()
20: * SpyGetMsgProc()
21: * SpyCallWndProc()
22: * DbgPrintf()
23: *
24: * Comments:
25: *
26: \*****************************************************************************/
27:
28: #include <windows.h>
29: #include "..\hook.h"
30:
31:
32: PRIVATE HWND ghwndSpyHook = NULL; // the handle back to the spy executable
33: PRIVATE SPYMSGDATA gsmd;
34: PRIVATE COPYDATASTRUCT gcds = { 0, 0, &gsmd };
35:
36:
37: PRIVATE VOID FindSpyWindow(VOID);
38:
39: #ifdef DBG
40: VOID DbgPrintf(LPTSTR fmt, ...);
41: #endif
42:
43:
44:
45: /*****************************************************************************\
46: * DllMain (hModule,cbHeap,lpchCmdLine)
47: *
48: * Called when the libary is loaded
49: *
50: * Arguments:
51: * PVOID hModule - Module handle for the libary.
52: * ULONG ulReason - DLL purpose
53: * PCONTEXT pctx - not used
54: *
55: * Returns:
56: * TRUE - Everything is ok
57: * FALSE- Error.
58: \*****************************************************************************/
59:
60: BOOL
61: APIENTRY DllMain(
62: PVOID hModule,
63: ULONG ulReason,
64: PCONTEXT pctx
65: )
66: {
67: UNREFERENCED_PARAMETER(hModule);
68: UNREFERENCED_PARAMETER(pctx);
69:
70: //
71: // This function is called for every instance of the DLL. We must find
72: // and store the handle to the spy window every time an instance of the
73: // DLL is instantiated.
74: //
75: if ( ulReason == DLL_PROCESS_ATTACH ) {
76: FindSpyWindow();
77: }
78:
79: return TRUE;
80: }
81:
82:
83:
84: /*****************************************************************************\
85: * FindSpyWindow
86: *
87: * Finds the spy window and store a local copy in this instances data.
88: * This must be called everytime that a new instance of the DLL is
89: * created.
90: *
91: * Arguments:
92: * none
93: *
94: * Returns:
95: * VOID
96: \*****************************************************************************/
97:
98: PRIVATE VOID
99: FindSpyWindow(
100: VOID
101: )
102: {
103: ghwndSpyHook = FindWindow(HOOKWINDOWCLASS, HOOKWINDOWNAME);
104: }
105:
106:
107: /*****************************************************************************\
108: * HookProc( hWnd, uiMessage, wParam, lParam )
109: *
110: * The hook proc for the windows hook being spied on
111: *
112: * Arguments:
113: * HWND hWnd - window handle for the parent window
114: * UINT uiMessage - message number
115: * WPARAM wParam - message-dependent
116: * LPARAM lParam - message-dependent
117: *
118: * Returns:
119: * 0 if processed, nonzero if ignored
120: \*****************************************************************************/
121:
122: BOOL WINAPI
123: HookProc(
124: HWND hwnd,
125: UINT uiMessage,
126: WPARAM wParam,
127: LPARAM lParam
128: )
129: {
130: HWND hwndSpyingOn;
131: HWND hwndSpyApp;
132:
133: if (ghwndSpyHook == NULL || !IsWindow(ghwndSpyHook))
134: {
135: //
136: // Spy has terminated. Find the new window.
137: //
138: FindSpyWindow();
139: }
140:
141: if (ghwndSpyHook && hwnd != ghwndSpyHook)
142: {
143: hwndSpyingOn = (HWND)GetWindowLong(ghwndSpyHook, 0);
144: hwndSpyApp = (HWND)GetWindowLong(ghwndSpyHook, sizeof(HWND));
145: //DbgPrintf("H ghwndSpyHook:%8.8x", ghwndSpyHook);
146: //DbgPrintf("H hwndSpyingOn:%8.8x", hwndSpyingOn);
147: //DbgPrintf("H hwndSpyApp:%8.8x", hwndSpyApp);
148:
149: //
150: // Send the message on asynchronously for Spy to deal with if
151: // it is the appropriate hwndSpyingOn window to spy on.
152: //
153:
154: if (hwndSpyingOn == hwnd
155: || (hwndSpyingOn == HWND_ALL && hwnd != hwndSpyApp
156: && !IsChild(hwndSpyApp, hwnd)))
157: {
158: gsmd.wParam = wParam;
159: gsmd.lParam = lParam;
160:
161: gcds.dwData = uiMessage;
162: gcds.cbData = 8;
163:
164: //DbgPrintf("H Sending Message hwnd:%8.8x msg:%d", hwnd, uiMessage);
165: SendMessage(ghwndSpyHook, WM_COPYDATA, (WPARAM)hwnd, (LPARAM)&gcds);
166: //DbgPrintf("H Sent Message hwnd:%8.8x msg:%d", hwnd, uiMessage);
167:
168: //DbgPrintf("");
169: return TRUE;
170: }
171: //DbgPrintf("");
172: }
173:
174: return FALSE;
175: }
176:
177:
178:
179: /*****************************************************************************\
180: * SpyGetMsgProc
181: *
182: * The Get Message hook function.
183: *
184: \*****************************************************************************/
185:
186: LRESULT CALLBACK
187: SpyGetMsgProc(
188: INT hc,
189: WPARAM wParam,
190: LPARAM lParam
191: )
192: {
193: PMSG pmsg;
194:
195: pmsg = (PMSG)lParam;
196:
197: if (hc >= 0 && pmsg && pmsg->hwnd)
198: {
199: return HookProc(pmsg->hwnd, pmsg->message, pmsg->wParam, pmsg->lParam);
200: }
201:
202: //
203: // Note that CallNextHookEx ignores the first parameter (hhook) so
204: // it is acceptable (barely) to pass in a NULL.
205: //
206: return CallNextHookEx(NULL, hc, wParam, lParam);
207: }
208:
209:
210:
211: /*****************************************************************************\
212: * SpyCallWndProc
213: *
214: * The Call Window Proc (Send Message) hook function.
215: *
216: \*****************************************************************************/
217:
218: LRESULT CALLBACK
219: SpyCallWndProc(
220: INT hc,
221: WPARAM wParam,
222: LPARAM lParam
223: )
224: {
225: PCWPSTRUCT pcwps;
226:
227: pcwps = (PCWPSTRUCT)lParam;
228:
229: if (hc >= 0 && pcwps && pcwps->hwnd)
230: {
231: return HookProc(pcwps->hwnd, pcwps->message, pcwps->wParam, pcwps->lParam);
232: }
233:
234: //
235: // Note that CallNextHookEx ignores the first parameter (hhook) so
236: // it is acceptable (barely) to pass in a NULL.
237: //
238: return CallNextHookEx(NULL, hc, wParam, lParam);
239: }
240:
241:
242:
243: #ifdef DBG
244: /****************************************************************************
245: * DBGprintf
246: *
247: * This debugging function prints out a string to the debug output.
248: * An optional set of substitutional parameters can be specified,
249: * and the final output will be the processed result of these combined
250: * with the format string, just like printf. A newline is always
251: * output after every call to this function.
252: *
253: * Arguments:
254: * LPTSTR fmt - Format string (printf style).
255: * ... - Variable number of arguments.
256: * Returns:
257: * VOID
258: \****************************************************************************/
259:
260: VOID DbgPrintf(
261: LPTSTR fmt,
262: ...
263: )
264: {
265: va_list marker;
266: TCHAR szBuf[256];
267:
268: va_start(marker, fmt);
269: wvsprintf(szBuf, fmt, marker);
270: va_end(marker);
271:
272: OutputDebugString(szBuf);
273: OutputDebugString(TEXT("\r\n"));
274: }
275: #endif
276:
277:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.