/[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 993 - (hide annotations)
Sun Aug 28 12:58:39 2005 UTC (18 years, 9 months ago) by astrand
File size: 13456 byte(s)
Indenting with the CVS version of astyle

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

  ViewVC Help
Powered by ViewVC 1.1.26