/[rdesktop]/sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.cpp
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 933 - (show annotations)
Thu Jun 30 14:46:14 2005 UTC (18 years, 10 months ago) by astrand
File size: 12512 byte(s)
Fixed indentation, by running indent-all.

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

  ViewVC Help
Powered by ViewVC 1.1.26