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