/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 937 - (hide annotations)
Fri Jul 1 06:50:52 2005 UTC (18 years, 11 months ago) by astrand
File size: 12465 byte(s)
Indenting with astyle instead of indent

1 astrand 930 //*********************************************************************************
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 astrand 937 // this is the total number of processes this dll is currently attached to
24 astrand 933 int iInstanceCount = 0;
25     HWND hWnd = 0;
26 astrand 930
27     #pragma data_seg ()
28    
29     #pragma comment(linker, "/section:SHAREDDATA,rws")
30    
31 astrand 933 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 astrand 930
40 astrand 933 BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call,
41     LPVOID lpReserved)
42 astrand 930 {
43 astrand 933 switch (ul_reason_for_call) {
44 astrand 937 case DLL_PROCESS_ATTACH: {
45 astrand 933 // remember our instance handle
46     hInst = hinstDLL;
47     ++iInstanceCount;
48     OpenVirtualChannel();
49     break;
50     }
51 astrand 930
52 astrand 933 case DLL_THREAD_ATTACH:
53     break;
54     case DLL_THREAD_DETACH:
55     break;
56 astrand 937 case DLL_PROCESS_DETACH: {
57 astrand 933 --iInstanceCount;
58     CloseVirtualChannel();
59     }
60     break;
61 astrand 930 }
62    
63     return TRUE;
64     }
65    
66 astrand 933 LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
67 astrand 930 {
68 astrand 933 if (nCode < 0) {
69     return CallNextHookEx(hhook3, nCode, wParam, lParam);
70     }
71 astrand 930
72 astrand 933 PCHAR buffer = NULL;
73     char windowTitle[150] = { "" };
74     HWND windowHandle = NULL;
75     char result[255] = { "" };
76     char strWindowId[25];
77     char type[25];
78 astrand 930
79 astrand 933 LONG b, t, l, r;
80 astrand 930 char strB[5];
81 astrand 933 char strT[5];
82     char strL[5];
83 astrand 930 char strR[5];
84 astrand 933 RECT rect;
85 astrand 930
86 astrand 933 CWPSTRUCT *details = (CWPSTRUCT *) lParam;
87 astrand 930
88 astrand 933 switch (details->message) {
89     case WM_SIZING:
90     case WM_MOVING:
91 astrand 930
92 astrand 933 windowHandle = details->hwnd;
93     //get win name
94     GetWindowText(windowHandle, windowTitle, 150);
95 astrand 930
96 astrand 933 //get an id for it
97     itoa((int) windowHandle, strWindowId, 10);
98 astrand 930
99 astrand 933 //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 astrand 930
110 astrand 933 ////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 astrand 930 }
144    
145 astrand 933 LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam)
146 astrand 930 {
147 astrand 933 if (nCode < 0) {
148     return CallNextHookEx(hhook, nCode, wParam, lParam);
149     }
150 astrand 930
151    
152 astrand 933 PCHAR buffer = NULL;
153 astrand 930
154    
155 astrand 933 char windowTitle[150] = { "" };
156     HWND windowHandle = NULL;
157     char result[255] = { "" };
158     char strWindowId[25];
159     char type[25];
160 astrand 930
161    
162 astrand 933 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 astrand 930
169 astrand 933 int i = 0; //tmp
170 astrand 930
171 astrand 933 switch (nCode) {
172     case HCBT_MINMAX:
173 astrand 930
174 astrand 933 windowHandle = (HWND) wParam;
175     //get win name
176     GetWindowText(windowHandle, windowTitle, 150);
177 astrand 930
178 astrand 933 //get an id for it
179     itoa((int) windowHandle, strWindowId, 10);
180 astrand 930
181 astrand 933 //get operation type(min,max). if max, do not clip at all,if min use window's previous coords
182     //codes are:
183 astrand 930
184 astrand 937 // 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 astrand 930
188 astrand 933 itoa((int) lParam, type, 10);
189 astrand 930
190 astrand 933 //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 astrand 930
201 astrand 933 //get name
202     GetWindowText(windowHandle, windowTitle, 150);
203 astrand 930
204 astrand 933 ////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 astrand 930
225 astrand 933 //-------------------------------------------------------------------------------------------------
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 astrand 930
237 astrand 933 //-------------------------------------------------------------------------------------------------
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 astrand 930
249 astrand 933 buffer = result;
250 astrand 930
251 astrand 933 break;
252 astrand 930
253 astrand 933 case HCBT_MOVESIZE:
254 astrand 930
255 astrand 933 windowHandle = (HWND) wParam;
256     //get win name
257     GetWindowText(windowHandle, windowTitle, 150);
258 astrand 930
259 astrand 933 //get an id for it
260     itoa((int) windowHandle, strWindowId, 10);
261 astrand 930
262 astrand 933 //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 astrand 930
273 astrand 933 //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 astrand 930 }
313    
314    
315 astrand 933 LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
316 astrand 930 {
317 astrand 933 if (nCode < 0) {
318     return CallNextHookEx(hhook, nCode, wParam, lParam);
319     }
320 astrand 930
321 astrand 933 if (ChannelIsOpen()) {
322     PCHAR buffer = NULL;
323 astrand 930
324 astrand 933 char windowTitle[150] = { "" };
325     HWND windowHandle = NULL;
326     char result[255] = { "" };
327     char strWindowId[25];
328     LONG b, t, l, r;
329 astrand 930 char strB[5];
330 astrand 933 char strT[5];
331     char strL[5];
332 astrand 930 char strR[5];
333 astrand 933 RECT rect;
334 astrand 930
335 astrand 933 switch (nCode) {
336     case HSHELL_WINDOWCREATED:
337 astrand 930
338 astrand 933 //get window id
339     windowHandle = (HWND) wParam;
340     itoa((int) windowHandle, strWindowId, 10);
341 astrand 930
342 astrand 933 //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 astrand 930
353 astrand 933 //get name
354     GetWindowText(windowHandle, windowTitle, 150);
355 astrand 930
356 astrand 933 ////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 astrand 930
374 astrand 933 buffer = result;
375 astrand 930
376 astrand 933 break;
377 astrand 930
378 astrand 933 case HSHELL_WINDOWDESTROYED:
379 astrand 930
380 astrand 933 //get window id
381     windowHandle = (HWND) wParam;
382     itoa((int) windowHandle, strWindowId, 10);
383 astrand 930
384 astrand 933 //get name
385     GetWindowText(windowHandle, windowTitle, 150);
386 astrand 930
387 astrand 933 ////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 astrand 930 }
410    
411     DLL_EXPORT void SetCbtHook(void)
412     {
413 astrand 933 if (!bHooked) {
414     hhook =
415     SetWindowsHookEx(WH_CBT, (HOOKPROC) CbtProc, hInst, (DWORD) NULL);
416     bHooked = true;
417     }
418 astrand 930
419 astrand 933 if (!bHooked2) {
420     hhook2 =
421     SetWindowsHookEx(WH_SHELL, (HOOKPROC) ShellProc, hInst,
422     (DWORD) NULL);
423     bHooked2 = true;
424     }
425 astrand 930
426 astrand 933 if (!bHooked3) {
427     hhook3 =
428     SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) CallWndProc, hInst,
429     (DWORD) NULL);
430     bHooked3 = true;
431     }
432 astrand 930 }
433    
434 astrand 933 DLL_EXPORT void RemoveCbtHook(void)
435 astrand 930 {
436 astrand 933 if (bHooked) {
437     UnhookWindowsHookEx(hhook);
438     bHooked = false;
439     }
440 astrand 930
441 astrand 933 if (bHooked2) {
442     UnhookWindowsHookEx(hhook2);
443     bHooked2 = false;
444     }
445 astrand 930
446 astrand 933 if (bHooked3) {
447     UnhookWindowsHookEx(hhook3);
448     bHooked3 = false;
449     }
450 astrand 930 }
451    
452     DLL_EXPORT int GetInstanceCount()
453     {
454 astrand 933 return iInstanceCount;
455 astrand 930 }
456    
457     int OpenVirtualChannel()
458     {
459 astrand 933 m_vcHandle =
460     WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION,
461     CHANNELNAME);
462    
463     if (m_vcHandle == NULL) {
464     return 0;
465 astrand 937 } else {
466 astrand 933 return 1;
467     }
468 astrand 930 }
469    
470     int CloseVirtualChannel()
471     {
472 astrand 933 BOOL result = WTSVirtualChannelClose(m_vcHandle);
473    
474     m_vcHandle = NULL;
475    
476     if (result) {
477     return 1;
478 astrand 937 } else {
479 astrand 933 return 0;
480     }
481 astrand 930 }
482    
483     int ChannelIsOpen()
484     {
485 astrand 933 if (m_vcHandle == NULL) {
486     return 0;
487 astrand 937 } else {
488 astrand 933 return 1;
489     }
490 astrand 930 }
491    
492     int WriteToChannel(PCHAR buffer)
493     {
494 astrand 933 PULONG bytesRead = 0;
495     PULONG pBytesWritten = 0;
496 astrand 930
497 astrand 933 BOOL result =
498     WTSVirtualChannelWrite(m_vcHandle, buffer, (ULONG) strlen(buffer),
499     pBytesWritten);
500    
501     if (result) {
502     return 1;
503 astrand 937 } else {
504 astrand 933 return 0;
505     }
506 astrand 930 }

  ViewVC Help
Powered by ViewVC 1.1.26