1 |
// |
2 |
// Copyright (C) 2004-2005 Martin Wickett |
3 |
// |
4 |
|
5 |
#include "hookdll.h" |
6 |
#include <windows.h> |
7 |
#include <winuser.h> |
8 |
|
9 |
#include "wtsapi32.h" |
10 |
#include "Cchannel.h" |
11 |
|
12 |
#define DLL_EXPORT extern "C" __declspec(dllexport) |
13 |
|
14 |
// Shared DATA |
15 |
#pragma data_seg ( "SHAREDDATA" ) |
16 |
|
17 |
// this is the total number of processes this dll is currently attached to |
18 |
int iInstanceCount = 0; |
19 |
HWND hWnd = 0; |
20 |
|
21 |
#pragma data_seg () |
22 |
|
23 |
#pragma comment(linker, "/section:SHAREDDATA,rws") |
24 |
|
25 |
bool bHooked = false; |
26 |
bool bHooked2 = false; |
27 |
bool bHooked3 = false; |
28 |
HHOOK hhook = 0; //cbt |
29 |
HHOOK hhook2 = 0; //shell |
30 |
HHOOK hhook3 = 0; //wnd proc |
31 |
HINSTANCE hInst = 0; |
32 |
HANDLE m_vcHandle = 0; |
33 |
|
34 |
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, |
35 |
LPVOID lpReserved) |
36 |
{ |
37 |
switch (ul_reason_for_call) { |
38 |
case DLL_PROCESS_ATTACH: { |
39 |
// remember our instance handle |
40 |
hInst = hinstDLL; |
41 |
++iInstanceCount; |
42 |
OpenVirtualChannel(); |
43 |
break; |
44 |
} |
45 |
|
46 |
case DLL_THREAD_ATTACH: |
47 |
break; |
48 |
case DLL_THREAD_DETACH: |
49 |
break; |
50 |
case DLL_PROCESS_DETACH: { |
51 |
--iInstanceCount; |
52 |
CloseVirtualChannel(); |
53 |
} |
54 |
break; |
55 |
} |
56 |
|
57 |
return TRUE; |
58 |
} |
59 |
|
60 |
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam) |
61 |
{ |
62 |
if (nCode < 0) { |
63 |
return CallNextHookEx(hhook3, nCode, wParam, lParam); |
64 |
} |
65 |
|
66 |
PCHAR buffer = NULL; |
67 |
char windowTitle[150] = { "" }; |
68 |
HWND windowHandle = NULL; |
69 |
char result[255] = { "" }; |
70 |
char strWindowId[25]; |
71 |
char type[25]; |
72 |
|
73 |
LONG b, t, l, r; |
74 |
char strB[5]; |
75 |
char strT[5]; |
76 |
char strL[5]; |
77 |
char strR[5]; |
78 |
RECT rect; |
79 |
|
80 |
CWPSTRUCT *details = (CWPSTRUCT *) lParam; |
81 |
|
82 |
switch (details->message) { |
83 |
case WM_SIZING: |
84 |
case WM_MOVING: |
85 |
|
86 |
windowHandle = details->hwnd; |
87 |
//get win name |
88 |
GetWindowText(windowHandle, windowTitle, 150); |
89 |
|
90 |
//get an id for it |
91 |
itoa((int) windowHandle, strWindowId, 10); |
92 |
|
93 |
//get coords |
94 |
GetWindowRect(windowHandle, &rect); |
95 |
b = rect.bottom; |
96 |
t = rect.top; |
97 |
l = rect.left; |
98 |
r = rect.right; |
99 |
ltoa(b, strB, 10); |
100 |
ltoa(t, strT, 10); |
101 |
ltoa(r, strR, 10); |
102 |
ltoa(l, strL, 10); |
103 |
|
104 |
////setup return string |
105 |
strcat(result, "MSG=CALLWNDPROC_WM_MOVING;"); |
106 |
strcat(result, "ID="); |
107 |
strcat(result, strWindowId); |
108 |
strcat(result, ";"); |
109 |
strcat(result, "TITLE="); |
110 |
strcat(result, windowTitle); |
111 |
strcat(result, ";"); |
112 |
strcat(result, "POS="); |
113 |
strcat(result, strL); |
114 |
strcat(result, "~"); |
115 |
strcat(result, strT); |
116 |
strcat(result, "~"); |
117 |
strcat(result, strR); |
118 |
strcat(result, "~"); |
119 |
strcat(result, strB); |
120 |
strcat(result, ";"); |
121 |
|
122 |
buffer = result; |
123 |
|
124 |
break; |
125 |
|
126 |
default: |
127 |
break; |
128 |
} |
129 |
|
130 |
if (ChannelIsOpen()) { |
131 |
if (buffer != NULL) { |
132 |
WriteToChannel(buffer); |
133 |
} |
134 |
} |
135 |
|
136 |
return CallNextHookEx(hhook3, nCode, wParam, lParam); |
137 |
} |
138 |
|
139 |
LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam) |
140 |
{ |
141 |
if (nCode < 0) { |
142 |
return CallNextHookEx(hhook, nCode, wParam, lParam); |
143 |
} |
144 |
|
145 |
|
146 |
PCHAR buffer = NULL; |
147 |
|
148 |
|
149 |
char windowTitle[150] = { "" }; |
150 |
HWND windowHandle = NULL; |
151 |
char result[255] = { "" }; |
152 |
char strWindowId[25]; |
153 |
char type[25]; |
154 |
|
155 |
|
156 |
LONG b, t, l, r; |
157 |
char strB[5]; |
158 |
char strT[5]; |
159 |
char strL[5]; |
160 |
char strR[5]; |
161 |
RECT rect; |
162 |
|
163 |
int i = 0; //tmp |
164 |
|
165 |
switch (nCode) { |
166 |
case HCBT_MINMAX: |
167 |
|
168 |
windowHandle = (HWND) wParam; |
169 |
//get win name |
170 |
GetWindowText(windowHandle, windowTitle, 150); |
171 |
|
172 |
//get an id for it |
173 |
itoa((int) windowHandle, strWindowId, 10); |
174 |
|
175 |
//get operation type(min,max). if max, do not clip at all,if min use window's previous coords |
176 |
//codes are: |
177 |
|
178 |
// SW_HIDE= 0 SW_SHOWNORMAL=1 SW_NORMAL=1 SW_SHOWMINIMIZED=2 SW_SHOWMAXIMIZED=3 SW_MAXIMIZE=3 |
179 |
// SW_SHOWNOACTIVATE=4 SW_SHOW=5 SW_MINIMIZE=6 SW_SHOWMINNOACTIVE=7 SW_SHOWNA=8 SW_RESTORE=9 |
180 |
// SW_SHOWDEFAULT=10 SW_FORCEMINIMIZE=11 SW_MAX=11 |
181 |
|
182 |
itoa((int) lParam, type, 10); |
183 |
|
184 |
//get coords |
185 |
GetWindowRect(windowHandle, &rect); |
186 |
b = rect.bottom; |
187 |
t = rect.top; |
188 |
l = rect.left; |
189 |
r = rect.right; |
190 |
ltoa(b, strB, 10); |
191 |
ltoa(t, strT, 10); |
192 |
ltoa(r, strR, 10); |
193 |
ltoa(l, strL, 10); |
194 |
|
195 |
//get name |
196 |
GetWindowText(windowHandle, windowTitle, 150); |
197 |
|
198 |
////setup return string |
199 |
strcat(result, "MSG=HCBT_MINMAX;"); |
200 |
strcat(result, "ID="); |
201 |
strcat(result, strWindowId); |
202 |
strcat(result, ";"); |
203 |
strcat(result, "TITLE="); |
204 |
strcat(result, windowTitle); |
205 |
strcat(result, ";"); |
206 |
strcat(result, "POS="); |
207 |
strcat(result, strL); |
208 |
strcat(result, "~"); |
209 |
strcat(result, strT); |
210 |
strcat(result, "~"); |
211 |
strcat(result, strR); |
212 |
strcat(result, "~"); |
213 |
strcat(result, strB); |
214 |
strcat(result, ";"); |
215 |
strcat(result, "TYPE="); |
216 |
strcat(result, type); |
217 |
strcat(result, ";"); |
218 |
|
219 |
//------------------------------------------------------------------------------------------------- |
220 |
// code to prevent minimising windows (can be removed once minimise has been implemented) |
221 |
i = (int) lParam; |
222 |
//if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11) |
223 |
if (i == 2 || i == 6) { |
224 |
MessageBox(0, |
225 |
"Minimising windows is not allowed in this version. Sorry!", |
226 |
"TS Window Clipper", MB_OK); |
227 |
return 1; |
228 |
} |
229 |
//----------------------------------------------------------------------------------------- |
230 |
|
231 |
//------------------------------------------------------------------------------------------------- |
232 |
// code to prevent maximising windows (can be removed once maximise has been implemented) |
233 |
i = (int) lParam; |
234 |
//if (i==3 || i==9 || i==11) |
235 |
if (i == 3 || i == 11) { |
236 |
MessageBox(0, |
237 |
"Maximising windows is not allowed in this version. Sorry!", |
238 |
"TS Window Clipper", MB_OK); |
239 |
return 1; |
240 |
} |
241 |
//----------------------------------------------------------------------------------------- |
242 |
|
243 |
buffer = result; |
244 |
|
245 |
break; |
246 |
|
247 |
case HCBT_MOVESIZE: |
248 |
|
249 |
windowHandle = (HWND) wParam; |
250 |
//get win name |
251 |
GetWindowText(windowHandle, windowTitle, 150); |
252 |
|
253 |
//get an id for it |
254 |
itoa((int) windowHandle, strWindowId, 10); |
255 |
|
256 |
//get coords |
257 |
GetWindowRect(windowHandle, &rect); |
258 |
b = rect.bottom; |
259 |
t = rect.top; |
260 |
l = rect.left; |
261 |
r = rect.right; |
262 |
ltoa(b, strB, 10); |
263 |
ltoa(t, strT, 10); |
264 |
ltoa(r, strR, 10); |
265 |
ltoa(l, strL, 10); |
266 |
|
267 |
//get name |
268 |
GetWindowText(windowHandle, windowTitle, 150); |
269 |
|
270 |
////setup return string |
271 |
strcat(result, "MSG=HCBT_MOVESIZE;"); |
272 |
strcat(result, "ID="); |
273 |
strcat(result, strWindowId); |
274 |
strcat(result, ";"); |
275 |
strcat(result, "TITLE="); |
276 |
strcat(result, windowTitle); |
277 |
strcat(result, ";"); |
278 |
strcat(result, "POS="); |
279 |
strcat(result, strL); |
280 |
strcat(result, "~"); |
281 |
strcat(result, strT); |
282 |
strcat(result, "~"); |
283 |
strcat(result, strR); |
284 |
strcat(result, "~"); |
285 |
strcat(result, strB); |
286 |
strcat(result, ";"); |
287 |
|
288 |
buffer = result; |
289 |
|
290 |
break; |
291 |
case HCBT_SETFOCUS: |
292 |
//buffer = "HCBT_SETFOCUS"; |
293 |
//not needed yet |
294 |
break; |
295 |
default: |
296 |
break; |
297 |
} |
298 |
|
299 |
if (ChannelIsOpen()) { |
300 |
if (buffer != NULL) { |
301 |
WriteToChannel(buffer); |
302 |
} |
303 |
} |
304 |
|
305 |
return CallNextHookEx(hhook, nCode, wParam, lParam); |
306 |
} |
307 |
|
308 |
|
309 |
LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam) |
310 |
{ |
311 |
if (nCode < 0) { |
312 |
return CallNextHookEx(hhook, nCode, wParam, lParam); |
313 |
} |
314 |
|
315 |
if (ChannelIsOpen()) { |
316 |
PCHAR buffer = NULL; |
317 |
|
318 |
char windowTitle[150] = { "" }; |
319 |
HWND windowHandle = NULL; |
320 |
char result[255] = { "" }; |
321 |
char strWindowId[25]; |
322 |
LONG b, t, l, r; |
323 |
char strB[5]; |
324 |
char strT[5]; |
325 |
char strL[5]; |
326 |
char strR[5]; |
327 |
RECT rect; |
328 |
|
329 |
switch (nCode) { |
330 |
case HSHELL_WINDOWCREATED: |
331 |
|
332 |
//get window id |
333 |
windowHandle = (HWND) wParam; |
334 |
itoa((int) windowHandle, strWindowId, 10); |
335 |
|
336 |
//get coords |
337 |
GetWindowRect(windowHandle, &rect); |
338 |
b = rect.bottom; |
339 |
t = rect.top; |
340 |
l = rect.left; |
341 |
r = rect.right; |
342 |
ltoa(b, strB, 10); |
343 |
ltoa(t, strT, 10); |
344 |
ltoa(r, strR, 10); |
345 |
ltoa(l, strL, 10); |
346 |
|
347 |
//get name |
348 |
GetWindowText(windowHandle, windowTitle, 150); |
349 |
|
350 |
////setup return string |
351 |
strcat(result, "MSG=HSHELL_WINDOWCREATED;"); |
352 |
strcat(result, "ID="); |
353 |
strcat(result, strWindowId); |
354 |
strcat(result, ";"); |
355 |
strcat(result, "TITLE="); |
356 |
strcat(result, windowTitle); |
357 |
strcat(result, ";"); |
358 |
strcat(result, "POS="); |
359 |
strcat(result, strL); |
360 |
strcat(result, "~"); |
361 |
strcat(result, strT); |
362 |
strcat(result, "~"); |
363 |
strcat(result, strR); |
364 |
strcat(result, "~"); |
365 |
strcat(result, strB); |
366 |
strcat(result, ";"); |
367 |
|
368 |
buffer = result; |
369 |
|
370 |
break; |
371 |
|
372 |
case HSHELL_WINDOWDESTROYED: |
373 |
|
374 |
//get window id |
375 |
windowHandle = (HWND) wParam; |
376 |
itoa((int) windowHandle, strWindowId, 10); |
377 |
|
378 |
//get name |
379 |
GetWindowText(windowHandle, windowTitle, 150); |
380 |
|
381 |
////setup return string |
382 |
strcat(result, "MSG=HSHELL_WINDOWDESTROYED;"); |
383 |
strcat(result, "ID="); |
384 |
strcat(result, strWindowId); |
385 |
strcat(result, ";"); |
386 |
strcat(result, "TITLE="); |
387 |
strcat(result, windowTitle); |
388 |
strcat(result, ";"); |
389 |
|
390 |
buffer = result; |
391 |
|
392 |
break; |
393 |
default: |
394 |
break; |
395 |
} |
396 |
|
397 |
if (buffer != NULL) { |
398 |
WriteToChannel(buffer); |
399 |
} |
400 |
} |
401 |
|
402 |
return CallNextHookEx(hhook, nCode, wParam, lParam); |
403 |
} |
404 |
|
405 |
DLL_EXPORT void SetCbtHook(void) |
406 |
{ |
407 |
if (!bHooked) { |
408 |
hhook = |
409 |
SetWindowsHookEx(WH_CBT, (HOOKPROC) CbtProc, hInst, (DWORD) NULL); |
410 |
bHooked = true; |
411 |
} |
412 |
|
413 |
if (!bHooked2) { |
414 |
hhook2 = |
415 |
SetWindowsHookEx(WH_SHELL, (HOOKPROC) ShellProc, hInst, |
416 |
(DWORD) NULL); |
417 |
bHooked2 = true; |
418 |
} |
419 |
|
420 |
if (!bHooked3) { |
421 |
hhook3 = |
422 |
SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) CallWndProc, hInst, |
423 |
(DWORD) NULL); |
424 |
bHooked3 = true; |
425 |
} |
426 |
} |
427 |
|
428 |
DLL_EXPORT void RemoveCbtHook(void) |
429 |
{ |
430 |
if (bHooked) { |
431 |
UnhookWindowsHookEx(hhook); |
432 |
bHooked = false; |
433 |
} |
434 |
|
435 |
if (bHooked2) { |
436 |
UnhookWindowsHookEx(hhook2); |
437 |
bHooked2 = false; |
438 |
} |
439 |
|
440 |
if (bHooked3) { |
441 |
UnhookWindowsHookEx(hhook3); |
442 |
bHooked3 = false; |
443 |
} |
444 |
} |
445 |
|
446 |
DLL_EXPORT int GetInstanceCount() |
447 |
{ |
448 |
return iInstanceCount; |
449 |
} |
450 |
|
451 |
int OpenVirtualChannel() |
452 |
{ |
453 |
m_vcHandle = |
454 |
WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, |
455 |
CHANNELNAME); |
456 |
|
457 |
if (m_vcHandle == NULL) { |
458 |
return 0; |
459 |
} else { |
460 |
return 1; |
461 |
} |
462 |
} |
463 |
|
464 |
int CloseVirtualChannel() |
465 |
{ |
466 |
BOOL result = WTSVirtualChannelClose(m_vcHandle); |
467 |
|
468 |
m_vcHandle = NULL; |
469 |
|
470 |
if (result) { |
471 |
return 1; |
472 |
} else { |
473 |
return 0; |
474 |
} |
475 |
} |
476 |
|
477 |
int ChannelIsOpen() |
478 |
{ |
479 |
if (m_vcHandle == NULL) { |
480 |
return 0; |
481 |
} else { |
482 |
return 1; |
483 |
} |
484 |
} |
485 |
|
486 |
int WriteToChannel(PCHAR buffer) |
487 |
{ |
488 |
PULONG bytesRead = 0; |
489 |
PULONG pBytesWritten = 0; |
490 |
|
491 |
BOOL result = |
492 |
WTSVirtualChannelWrite(m_vcHandle, buffer, (ULONG) strlen(buffer), |
493 |
pBytesWritten); |
494 |
|
495 |
if (result) { |
496 |
return 1; |
497 |
} else { |
498 |
return 0; |
499 |
} |
500 |
} |