/[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 938 - (hide annotations)
Fri Jul 1 07:01:00 2005 UTC (18 years, 11 months ago) by astrand
File size: 12253 byte(s)
Added proper copyright headers

1 astrand 930 //
2 astrand 938 // Copyright (C) 2004-2005 Martin Wickett
3 astrand 930 //
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 astrand 937 // this is the total number of processes this dll is currently attached to
18 astrand 933 int iInstanceCount = 0;
19     HWND hWnd = 0;
20 astrand 930
21     #pragma data_seg ()
22    
23     #pragma comment(linker, "/section:SHAREDDATA,rws")
24    
25 astrand 933 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 astrand 930
34 astrand 933 BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call,
35     LPVOID lpReserved)
36 astrand 930 {
37 astrand 933 switch (ul_reason_for_call) {
38 astrand 937 case DLL_PROCESS_ATTACH: {
39 astrand 933 // remember our instance handle
40     hInst = hinstDLL;
41     ++iInstanceCount;
42     OpenVirtualChannel();
43     break;
44     }
45 astrand 930
46 astrand 933 case DLL_THREAD_ATTACH:
47     break;
48     case DLL_THREAD_DETACH:
49     break;
50 astrand 937 case DLL_PROCESS_DETACH: {
51 astrand 933 --iInstanceCount;
52     CloseVirtualChannel();
53     }
54     break;
55 astrand 930 }
56    
57     return TRUE;
58     }
59    
60 astrand 933 LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
61 astrand 930 {
62 astrand 933 if (nCode < 0) {
63     return CallNextHookEx(hhook3, nCode, wParam, lParam);
64     }
65 astrand 930
66 astrand 933 PCHAR buffer = NULL;
67     char windowTitle[150] = { "" };
68     HWND windowHandle = NULL;
69     char result[255] = { "" };
70     char strWindowId[25];
71     char type[25];
72 astrand 930
73 astrand 933 LONG b, t, l, r;
74 astrand 930 char strB[5];
75 astrand 933 char strT[5];
76     char strL[5];
77 astrand 930 char strR[5];
78 astrand 933 RECT rect;
79 astrand 930
80 astrand 933 CWPSTRUCT *details = (CWPSTRUCT *) lParam;
81 astrand 930
82 astrand 933 switch (details->message) {
83     case WM_SIZING:
84     case WM_MOVING:
85 astrand 930
86 astrand 933 windowHandle = details->hwnd;
87     //get win name
88     GetWindowText(windowHandle, windowTitle, 150);
89 astrand 930
90 astrand 933 //get an id for it
91     itoa((int) windowHandle, strWindowId, 10);
92 astrand 930
93 astrand 933 //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 astrand 930
104 astrand 933 ////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 astrand 930 }
138    
139 astrand 933 LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam)
140 astrand 930 {
141 astrand 933 if (nCode < 0) {
142     return CallNextHookEx(hhook, nCode, wParam, lParam);
143     }
144 astrand 930
145    
146 astrand 933 PCHAR buffer = NULL;
147 astrand 930
148    
149 astrand 933 char windowTitle[150] = { "" };
150     HWND windowHandle = NULL;
151     char result[255] = { "" };
152     char strWindowId[25];
153     char type[25];
154 astrand 930
155    
156 astrand 933 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 astrand 930
163 astrand 933 int i = 0; //tmp
164 astrand 930
165 astrand 933 switch (nCode) {
166     case HCBT_MINMAX:
167 astrand 930
168 astrand 933 windowHandle = (HWND) wParam;
169     //get win name
170     GetWindowText(windowHandle, windowTitle, 150);
171 astrand 930
172 astrand 933 //get an id for it
173     itoa((int) windowHandle, strWindowId, 10);
174 astrand 930
175 astrand 933 //get operation type(min,max). if max, do not clip at all,if min use window's previous coords
176     //codes are:
177 astrand 930
178 astrand 937 // 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 astrand 930
182 astrand 933 itoa((int) lParam, type, 10);
183 astrand 930
184 astrand 933 //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 astrand 930
195 astrand 933 //get name
196     GetWindowText(windowHandle, windowTitle, 150);
197 astrand 930
198 astrand 933 ////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 astrand 930
219 astrand 933 //-------------------------------------------------------------------------------------------------
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 astrand 930
231 astrand 933 //-------------------------------------------------------------------------------------------------
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 astrand 930
243 astrand 933 buffer = result;
244 astrand 930
245 astrand 933 break;
246 astrand 930
247 astrand 933 case HCBT_MOVESIZE:
248 astrand 930
249 astrand 933 windowHandle = (HWND) wParam;
250     //get win name
251     GetWindowText(windowHandle, windowTitle, 150);
252 astrand 930
253 astrand 933 //get an id for it
254     itoa((int) windowHandle, strWindowId, 10);
255 astrand 930
256 astrand 933 //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 astrand 930
267 astrand 933 //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 astrand 930 }
307    
308    
309 astrand 933 LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
310 astrand 930 {
311 astrand 933 if (nCode < 0) {
312     return CallNextHookEx(hhook, nCode, wParam, lParam);
313     }
314 astrand 930
315 astrand 933 if (ChannelIsOpen()) {
316     PCHAR buffer = NULL;
317 astrand 930
318 astrand 933 char windowTitle[150] = { "" };
319     HWND windowHandle = NULL;
320     char result[255] = { "" };
321     char strWindowId[25];
322     LONG b, t, l, r;
323 astrand 930 char strB[5];
324 astrand 933 char strT[5];
325     char strL[5];
326 astrand 930 char strR[5];
327 astrand 933 RECT rect;
328 astrand 930
329 astrand 933 switch (nCode) {
330     case HSHELL_WINDOWCREATED:
331 astrand 930
332 astrand 933 //get window id
333     windowHandle = (HWND) wParam;
334     itoa((int) windowHandle, strWindowId, 10);
335 astrand 930
336 astrand 933 //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 astrand 930
347 astrand 933 //get name
348     GetWindowText(windowHandle, windowTitle, 150);
349 astrand 930
350 astrand 933 ////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 astrand 930
368 astrand 933 buffer = result;
369 astrand 930
370 astrand 933 break;
371 astrand 930
372 astrand 933 case HSHELL_WINDOWDESTROYED:
373 astrand 930
374 astrand 933 //get window id
375     windowHandle = (HWND) wParam;
376     itoa((int) windowHandle, strWindowId, 10);
377 astrand 930
378 astrand 933 //get name
379     GetWindowText(windowHandle, windowTitle, 150);
380 astrand 930
381 astrand 933 ////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 astrand 930 }
404    
405     DLL_EXPORT void SetCbtHook(void)
406     {
407 astrand 933 if (!bHooked) {
408     hhook =
409     SetWindowsHookEx(WH_CBT, (HOOKPROC) CbtProc, hInst, (DWORD) NULL);
410     bHooked = true;
411     }
412 astrand 930
413 astrand 933 if (!bHooked2) {
414     hhook2 =
415     SetWindowsHookEx(WH_SHELL, (HOOKPROC) ShellProc, hInst,
416     (DWORD) NULL);
417     bHooked2 = true;
418     }
419 astrand 930
420 astrand 933 if (!bHooked3) {
421     hhook3 =
422     SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) CallWndProc, hInst,
423     (DWORD) NULL);
424     bHooked3 = true;
425     }
426 astrand 930 }
427    
428 astrand 933 DLL_EXPORT void RemoveCbtHook(void)
429 astrand 930 {
430 astrand 933 if (bHooked) {
431     UnhookWindowsHookEx(hhook);
432     bHooked = false;
433     }
434 astrand 930
435 astrand 933 if (bHooked2) {
436     UnhookWindowsHookEx(hhook2);
437     bHooked2 = false;
438     }
439 astrand 930
440 astrand 933 if (bHooked3) {
441     UnhookWindowsHookEx(hhook3);
442     bHooked3 = false;
443     }
444 astrand 930 }
445    
446     DLL_EXPORT int GetInstanceCount()
447     {
448 astrand 933 return iInstanceCount;
449 astrand 930 }
450    
451     int OpenVirtualChannel()
452     {
453 astrand 933 m_vcHandle =
454     WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION,
455     CHANNELNAME);
456    
457     if (m_vcHandle == NULL) {
458     return 0;
459 astrand 937 } else {
460 astrand 933 return 1;
461     }
462 astrand 930 }
463    
464     int CloseVirtualChannel()
465     {
466 astrand 933 BOOL result = WTSVirtualChannelClose(m_vcHandle);
467    
468     m_vcHandle = NULL;
469    
470     if (result) {
471     return 1;
472 astrand 937 } else {
473 astrand 933 return 0;
474     }
475 astrand 930 }
476    
477     int ChannelIsOpen()
478     {
479 astrand 933 if (m_vcHandle == NULL) {
480     return 0;
481 astrand 937 } else {
482 astrand 933 return 1;
483     }
484 astrand 930 }
485    
486     int WriteToChannel(PCHAR buffer)
487     {
488 astrand 933 PULONG bytesRead = 0;
489     PULONG pBytesWritten = 0;
490 astrand 930
491 astrand 933 BOOL result =
492     WTSVirtualChannelWrite(m_vcHandle, buffer, (ULONG) strlen(buffer),
493     pBytesWritten);
494    
495     if (result) {
496     return 1;
497 astrand 937 } else {
498 astrand 933 return 0;
499     }
500 astrand 930 }

  ViewVC Help
Powered by ViewVC 1.1.26