/[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 933 - (hide annotations)
Thu Jun 30 14:46:14 2005 UTC (18 years, 11 months ago) by astrand
File size: 12512 byte(s)
Fixed indentation, by running indent-all.

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 933 // this is the total number of processes this dll is currently attached to
24     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     case DLL_PROCESS_ATTACH:
45     {
46     // remember our instance handle
47     hInst = hinstDLL;
48     ++iInstanceCount;
49     OpenVirtualChannel();
50     break;
51     }
52 astrand 930
53 astrand 933 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 astrand 930 }
64    
65     return TRUE;
66     }
67    
68 astrand 933 LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
69 astrand 930 {
70 astrand 933 if (nCode < 0) {
71     return CallNextHookEx(hhook3, nCode, wParam, lParam);
72     }
73 astrand 930
74 astrand 933 PCHAR buffer = NULL;
75     char windowTitle[150] = { "" };
76     HWND windowHandle = NULL;
77     char result[255] = { "" };
78     char strWindowId[25];
79     char type[25];
80 astrand 930
81 astrand 933 LONG b, t, l, r;
82 astrand 930 char strB[5];
83 astrand 933 char strT[5];
84     char strL[5];
85 astrand 930 char strR[5];
86 astrand 933 RECT rect;
87 astrand 930
88 astrand 933 CWPSTRUCT *details = (CWPSTRUCT *) lParam;
89 astrand 930
90 astrand 933 switch (details->message) {
91     case WM_SIZING:
92     case WM_MOVING:
93 astrand 930
94 astrand 933 windowHandle = details->hwnd;
95     //get win name
96     GetWindowText(windowHandle, windowTitle, 150);
97 astrand 930
98 astrand 933 //get an id for it
99     itoa((int) windowHandle, strWindowId, 10);
100 astrand 930
101 astrand 933 //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 astrand 930
112 astrand 933 ////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 astrand 930 }
146    
147 astrand 933 LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam)
148 astrand 930 {
149 astrand 933 if (nCode < 0) {
150     return CallNextHookEx(hhook, nCode, wParam, lParam);
151     }
152 astrand 930
153    
154 astrand 933 PCHAR buffer = NULL;
155 astrand 930
156    
157 astrand 933 char windowTitle[150] = { "" };
158     HWND windowHandle = NULL;
159     char result[255] = { "" };
160     char strWindowId[25];
161     char type[25];
162 astrand 930
163    
164 astrand 933 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 astrand 930
171 astrand 933 int i = 0; //tmp
172 astrand 930
173 astrand 933 switch (nCode) {
174     case HCBT_MINMAX:
175 astrand 930
176 astrand 933 windowHandle = (HWND) wParam;
177     //get win name
178     GetWindowText(windowHandle, windowTitle, 150);
179 astrand 930
180 astrand 933 //get an id for it
181     itoa((int) windowHandle, strWindowId, 10);
182 astrand 930
183 astrand 933 //get operation type(min,max). if max, do not clip at all,if min use window's previous coords
184     //codes are:
185 astrand 930
186 astrand 933 // 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 astrand 930
190 astrand 933 itoa((int) lParam, type, 10);
191 astrand 930
192 astrand 933 //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 astrand 930
203 astrand 933 //get name
204     GetWindowText(windowHandle, windowTitle, 150);
205 astrand 930
206 astrand 933 ////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 astrand 930
227 astrand 933 //-------------------------------------------------------------------------------------------------
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 astrand 930
239 astrand 933 //-------------------------------------------------------------------------------------------------
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 astrand 930
251 astrand 933 buffer = result;
252 astrand 930
253 astrand 933 break;
254 astrand 930
255 astrand 933 case HCBT_MOVESIZE:
256 astrand 930
257 astrand 933 windowHandle = (HWND) wParam;
258     //get win name
259     GetWindowText(windowHandle, windowTitle, 150);
260 astrand 930
261 astrand 933 //get an id for it
262     itoa((int) windowHandle, strWindowId, 10);
263 astrand 930
264 astrand 933 //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 astrand 930
275 astrand 933 //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 astrand 930 }
315    
316    
317 astrand 933 LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
318 astrand 930 {
319 astrand 933 if (nCode < 0) {
320     return CallNextHookEx(hhook, nCode, wParam, lParam);
321     }
322 astrand 930
323 astrand 933 if (ChannelIsOpen()) {
324     PCHAR buffer = NULL;
325 astrand 930
326 astrand 933 char windowTitle[150] = { "" };
327     HWND windowHandle = NULL;
328     char result[255] = { "" };
329     char strWindowId[25];
330     LONG b, t, l, r;
331 astrand 930 char strB[5];
332 astrand 933 char strT[5];
333     char strL[5];
334 astrand 930 char strR[5];
335 astrand 933 RECT rect;
336 astrand 930
337 astrand 933 switch (nCode) {
338     case HSHELL_WINDOWCREATED:
339 astrand 930
340 astrand 933 //get window id
341     windowHandle = (HWND) wParam;
342     itoa((int) windowHandle, strWindowId, 10);
343 astrand 930
344 astrand 933 //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 astrand 930
355 astrand 933 //get name
356     GetWindowText(windowHandle, windowTitle, 150);
357 astrand 930
358 astrand 933 ////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 astrand 930
376 astrand 933 buffer = result;
377 astrand 930
378 astrand 933 break;
379 astrand 930
380 astrand 933 case HSHELL_WINDOWDESTROYED:
381 astrand 930
382 astrand 933 //get window id
383     windowHandle = (HWND) wParam;
384     itoa((int) windowHandle, strWindowId, 10);
385 astrand 930
386 astrand 933 //get name
387     GetWindowText(windowHandle, windowTitle, 150);
388 astrand 930
389 astrand 933 ////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 astrand 930 }
412    
413     DLL_EXPORT void SetCbtHook(void)
414     {
415 astrand 933 if (!bHooked) {
416     hhook =
417     SetWindowsHookEx(WH_CBT, (HOOKPROC) CbtProc, hInst, (DWORD) NULL);
418     bHooked = true;
419     }
420 astrand 930
421 astrand 933 if (!bHooked2) {
422     hhook2 =
423     SetWindowsHookEx(WH_SHELL, (HOOKPROC) ShellProc, hInst,
424     (DWORD) NULL);
425     bHooked2 = true;
426     }
427 astrand 930
428 astrand 933 if (!bHooked3) {
429     hhook3 =
430     SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) CallWndProc, hInst,
431     (DWORD) NULL);
432     bHooked3 = true;
433     }
434 astrand 930 }
435    
436 astrand 933 DLL_EXPORT void RemoveCbtHook(void)
437 astrand 930 {
438 astrand 933 if (bHooked) {
439     UnhookWindowsHookEx(hhook);
440     bHooked = false;
441     }
442 astrand 930
443 astrand 933 if (bHooked2) {
444     UnhookWindowsHookEx(hhook2);
445     bHooked2 = false;
446     }
447 astrand 930
448 astrand 933 if (bHooked3) {
449     UnhookWindowsHookEx(hhook3);
450     bHooked3 = false;
451     }
452 astrand 930 }
453    
454     DLL_EXPORT int GetInstanceCount()
455     {
456 astrand 933 return iInstanceCount;
457 astrand 930 }
458    
459     int OpenVirtualChannel()
460     {
461 astrand 933 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 astrand 930 }
472    
473     int CloseVirtualChannel()
474     {
475 astrand 933 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 astrand 930 }
486    
487     int ChannelIsOpen()
488     {
489 astrand 933 if (m_vcHandle == NULL) {
490     return 0;
491     }
492     else {
493     return 1;
494     }
495 astrand 930 }
496    
497     int WriteToChannel(PCHAR buffer)
498     {
499 astrand 933 PULONG bytesRead = 0;
500     PULONG pBytesWritten = 0;
501 astrand 930
502 astrand 933 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 astrand 930 }

  ViewVC Help
Powered by ViewVC 1.1.26