/[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 998 - (hide annotations)
Tue Aug 30 09:15:44 2005 UTC (18 years, 10 months ago) by astrand
File size: 15985 byte(s)
Added a function for sending debug printouts to the other end.

Re-implemented the code on WM_SIZING/WM_MOVING: Now generating
messages according to the new protocol.

Do not catch WM_WINDOWPOSCHANGED; WM_WINDOWPOSCHANGING is enough. In
response, generate a ZCHANGE1 messages according to the new protocol.

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 astrand 995 #include <stdio.h>
9 astrand 998 #include <stdarg.h>
10 astrand 930
11     #include "wtsapi32.h"
12     #include "Cchannel.h"
13    
14 astrand 993 #define DLL_EXPORT extern "C" __declspec(dllexport)
15 astrand 930
16     // Shared DATA
17     #pragma data_seg ( "SHAREDDATA" )
18    
19 astrand 937 // this is the total number of processes this dll is currently attached to
20 astrand 933 int iInstanceCount = 0;
21     HWND hWnd = 0;
22 astrand 930
23     #pragma data_seg ()
24    
25     #pragma comment(linker, "/section:SHAREDDATA,rws")
26    
27 astrand 998 #define snprintf _snprintf
28    
29 astrand 933 bool bHooked = false;
30     bool bHooked2 = false;
31     bool bHooked3 = false;
32 astrand 994 HHOOK hhook = 0; //cbt
33     HHOOK hhook2 = 0; //shell
34     HHOOK hhook3 = 0; //wnd proc
35 astrand 933 HINSTANCE hInst = 0;
36     HANDLE m_vcHandle = 0;
37 astrand 930
38 astrand 998
39     void SendDebug( char *format, ... )
40     {
41     va_list argp;
42     char buf [ 256 ];
43    
44     va_start( argp, format );
45     vsprintf( buf, format, argp );
46     va_end( argp );
47    
48     if ( ChannelIsOpen() ) {
49     WriteToChannel( "DEBUG1," );
50     WriteToChannel( buf );
51     WriteToChannel( "\n" );
52     }
53     }
54    
55    
56    
57 astrand 994 BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved )
58 astrand 930 {
59 astrand 993 switch ( ul_reason_for_call ) {
60     case DLL_PROCESS_ATTACH: {
61 astrand 933 // remember our instance handle
62     hInst = hinstDLL;
63     ++iInstanceCount;
64     OpenVirtualChannel();
65     break;
66     }
67 astrand 993
68     case DLL_THREAD_ATTACH:
69 astrand 933 break;
70 astrand 993 case DLL_THREAD_DETACH:
71 astrand 933 break;
72 astrand 993 case DLL_PROCESS_DETACH: {
73 astrand 933 --iInstanceCount;
74     CloseVirtualChannel();
75     }
76     break;
77 astrand 930 }
78 astrand 993
79 astrand 930 return TRUE;
80     }
81    
82 astrand 993 LRESULT CALLBACK CallWndProc( int nCode, WPARAM wParam, LPARAM lParam )
83 astrand 930 {
84 astrand 993 if ( nCode < 0 ) {
85     return CallNextHookEx( hhook3, nCode, wParam, lParam );
86 astrand 933 }
87 astrand 993
88 astrand 933 PCHAR buffer = NULL;
89 astrand 993 char windowTitle[ 150 ] = { ""
90     };
91 astrand 933 HWND windowHandle = NULL;
92 astrand 994 HWND windowHandle2 = NULL;
93 astrand 993 char result[ 255 ] = { ""
94     };
95     char strWindowId[ 25 ];
96     char type[ 25 ];
97    
98 astrand 933 LONG b, t, l, r;
99 astrand 994 char strX[ 5 ];
100     char strY[ 5 ];
101     char strW[ 5 ];
102     char strH[ 5 ];
103 astrand 993
104     CWPSTRUCT *details = ( CWPSTRUCT * ) lParam;
105 astrand 995 CREATESTRUCT *cs = ( CREATESTRUCT * ) details->lParam;
106     LONG dwStyle = GetWindowLong( details->hwnd, GWL_STYLE );
107 astrand 998 WINDOWPOS *wp = ( WINDOWPOS * ) details->lParam;
108     RECT *rect = ( RECT * ) details->lParam;
109 astrand 993
110     switch ( details->message ) {
111 astrand 998
112 astrand 993 case WM_SIZING:
113     case WM_MOVING:
114 astrand 998 snprintf( result, sizeof( result ),
115     "POSITION1,0x%x,%d,%d,%d,%d,0x%x",
116     ( int ) details->hwnd,
117     rect->left, rect->top,
118     rect->right - rect->left,
119     rect->bottom - rect->top,
120     0 );
121     result[ sizeof( result ) - 1 ] = '\0';
122 astrand 933 buffer = result;
123 astrand 993
124 astrand 933 break;
125 astrand 993
126 astrand 998 /* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are
127     strange. Sometimes, for example when bringing up the
128     Notepad About dialog, only an WM_WINDOWPOSCHANGING is
129     sent. In some other cases, for exmaple when opening
130     Format->Text in Notepad, both events are sent. Also, for
131     some reason, when closing the Notepad About dialog, an
132     WM_WINDOWPOSCHANGING event is sent which looks just like
133     the event that was sent when the About dialog was opened... */
134     case WM_WINDOWPOSCHANGING:
135 astrand 994
136 astrand 998 if ( !( dwStyle & WS_VISIBLE ) )
137     break;
138    
139     if ( !( dwStyle & WS_DLGFRAME ) )
140     break;
141    
142     if ( !( wp->flags & SWP_NOZORDER ) ) {
143     snprintf( result, sizeof( result ),
144     "ZCHANGE1,0x%x,0x%x,0x%x\n",
145     details->hwnd,
146     wp->flags & SWP_NOACTIVATE ? wp->hwndInsertAfter : 0,
147     0 );
148     result[ sizeof( result ) - 1 ] = '\0';
149     }
150 astrand 994 buffer = result;
151    
152     break;
153    
154 astrand 995
155     case WM_CREATE:
156     if ( cs->style & WS_DLGFRAME ) {
157     sprintf( result, "DEBUG:WM_CREATE:%dx%d at %d,%d, title=%s, menu=%p, window=%p, WS_BORDER=%d, WS_DLGFRAME=%d, WS_POPUP=%d",
158     cs->cx, cs->cy, cs->x, cs->y, cs->lpszName, cs->hMenu, details->hwnd,
159     cs->style & WS_BORDER, cs->style & WS_DLGFRAME,
160     cs->style & WS_POPUP );
161     buffer = result;
162     }
163    
164     break;
165    
166    
167     case WM_DESTROY:
168     if ( dwStyle & WS_DLGFRAME ) {
169     sprintf( result, "WM_DESTROY:%p", details->hwnd );
170     buffer = result;
171     }
172    
173     break;
174    
175 astrand 993 default:
176 astrand 933 break;
177     }
178 astrand 993
179     if ( ChannelIsOpen() ) {
180     if ( buffer != NULL ) {
181     WriteToChannel( buffer );
182 astrand 933 }
183     }
184 astrand 993
185     return CallNextHookEx( hhook3, nCode, wParam, lParam );
186 astrand 930 }
187    
188 astrand 993 LRESULT CALLBACK CbtProc( int nCode, WPARAM wParam, LPARAM lParam )
189 astrand 930 {
190 astrand 993 if ( nCode < 0 ) {
191     return CallNextHookEx( hhook, nCode, wParam, lParam );
192 astrand 933 }
193 astrand 993
194    
195 astrand 933 PCHAR buffer = NULL;
196 astrand 993
197    
198     char windowTitle[ 150 ] = { ""
199     };
200 astrand 933 HWND windowHandle = NULL;
201 astrand 993 char result[ 255 ] = { ""
202     };
203     char strWindowId[ 25 ];
204     char type[ 25 ];
205    
206    
207 astrand 933 LONG b, t, l, r;
208 astrand 994 char strW[ 5 ];
209     char strY[ 5 ];
210     char strX[ 5 ];
211     char strH[ 5 ];
212 astrand 933 RECT rect;
213 astrand 993
214 astrand 994 int i = 0; //tmp
215 astrand 993
216     switch ( nCode ) {
217     case HCBT_MINMAX:
218    
219     windowHandle = ( HWND ) wParam;
220 astrand 933 //get win name
221 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
222    
223 astrand 933 //get an id for it
224 astrand 993 itoa( ( int ) windowHandle, strWindowId, 10 );
225    
226 astrand 933 //get operation type(min,max). if max, do not clip at all,if min use window's previous coords
227     //codes are:
228 astrand 993
229 astrand 937 // SW_HIDE= 0 SW_SHOWNORMAL=1 SW_NORMAL=1 SW_SHOWMINIMIZED=2 SW_SHOWMAXIMIZED=3 SW_MAXIMIZE=3
230     // SW_SHOWNOACTIVATE=4 SW_SHOW=5 SW_MINIMIZE=6 SW_SHOWMINNOACTIVE=7 SW_SHOWNA=8 SW_RESTORE=9
231     // SW_SHOWDEFAULT=10 SW_FORCEMINIMIZE=11 SW_MAX=11
232 astrand 993
233     itoa( ( int ) lParam, type, 10 );
234    
235 astrand 933 //get coords
236 astrand 993 GetWindowRect( windowHandle, &rect );
237 astrand 933 b = rect.bottom;
238     t = rect.top;
239     l = rect.left;
240     r = rect.right;
241 astrand 994 ltoa( b - t, strW, 10 );
242     ltoa( t, strY, 10 );
243     ltoa( r - l, strH, 10 );
244     ltoa( l, strX, 10 );
245 astrand 993
246 astrand 933 //get name
247 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
248    
249 astrand 933 ////setup return string
250 astrand 994 strcat( result, "MSG=HCBT_MINMAX;OP=4;" );
251    
252     // SW_SHOWNOACTIVATE=4 SW_SHOW=5 SW_MINIMIZE=6 SW_SHOWMINNOACTIVE=7 SW_SHOWNA=8 SW_RESTORE=9
253     // SW_SHOWDEFAULT=10 SW_FORCEMINIMIZE=11 SW_MAX=11
254 astrand 993 strcat( result, "ID=" );
255     strcat( result, strWindowId );
256     strcat( result, ";" );
257     strcat( result, "TITLE=" );
258     strcat( result, windowTitle );
259     strcat( result, ";" );
260 astrand 994 strcat( result, "X=" );
261     strcat( result, strX );
262 astrand 993 strcat( result, ";" );
263 astrand 994 strcat( result, "Y=" );
264     strcat( result, strY );
265     strcat( result, ";" );
266     strcat( result, "H=" );
267     strcat( result, strH );
268     strcat( result, ";" );
269     strcat( result, "W=" );
270     strcat( result, strW );
271     strcat( result, ";" );
272 astrand 993 strcat( result, "TYPE=" );
273     strcat( result, type );
274 astrand 994 strcat( result, "." );
275 astrand 993
276 astrand 933 //-------------------------------------------------------------------------------------------------
277     // code to prevent minimising windows (can be removed once minimise has been implemented)
278 astrand 994 //i = (int)lParam;
279 astrand 933 //if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11)
280 astrand 994 //if ( i==2 || i==6 )
281     //{
282     // MessageBox(0,"Minimising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);
283     // return 1;
284     //}
285 astrand 933 //-----------------------------------------------------------------------------------------
286 astrand 993
287 astrand 933 //-------------------------------------------------------------------------------------------------
288     // code to prevent maximising windows (can be removed once maximise has been implemented)
289 astrand 994 //i = (int)lParam;
290 astrand 933 //if (i==3 || i==9 || i==11)
291 astrand 994 //if (i==3 || i==11)
292     //{
293     // MessageBox(0,"Maximising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);
294     // return 1;
295     //}
296 astrand 933 //-----------------------------------------------------------------------------------------
297 astrand 993
298 astrand 933 buffer = result;
299 astrand 993
300 astrand 933 break;
301 astrand 993
302     case HCBT_MOVESIZE:
303    
304     windowHandle = ( HWND ) wParam;
305 astrand 933 //get win name
306 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
307    
308 astrand 933 //get an id for it
309 astrand 993 itoa( ( int ) windowHandle, strWindowId, 10 );
310    
311 astrand 933 //get coords
312 astrand 993 GetWindowRect( windowHandle, &rect );
313 astrand 933 b = rect.bottom;
314     t = rect.top;
315     l = rect.left;
316     r = rect.right;
317 astrand 994 ltoa( b - t, strH, 10 );
318     ltoa( t, strY, 10 );
319     ltoa( r - l, strW, 10 );
320     ltoa( l, strX, 10 );
321 astrand 993
322 astrand 933 //get name
323 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
324    
325 astrand 933 ////setup return string
326 astrand 994 strcat( result, "MSG=HCBT_MOVESIZE;OP=5;" );
327 astrand 993 strcat( result, "ID=" );
328     strcat( result, strWindowId );
329     strcat( result, ";" );
330     strcat( result, "TITLE=" );
331     strcat( result, windowTitle );
332     strcat( result, ";" );
333 astrand 994 strcat( result, "X=" );
334     strcat( result, strX );
335 astrand 993 strcat( result, ";" );
336 astrand 994 strcat( result, "Y=" );
337     strcat( result, strY );
338     strcat( result, ";" );
339     strcat( result, "H=" );
340     strcat( result, strH );
341     strcat( result, ";" );
342     strcat( result, "W=" );
343     strcat( result, strW );
344     strcat( result, "." );
345 astrand 993
346 astrand 933 buffer = result;
347 astrand 993
348 astrand 933 break;
349 astrand 993 case HCBT_SETFOCUS:
350 astrand 933 //buffer = "HCBT_SETFOCUS";
351     //not needed yet
352     break;
353 astrand 993 default:
354 astrand 933 break;
355     }
356 astrand 993
357     if ( ChannelIsOpen() ) {
358     if ( buffer != NULL ) {
359     WriteToChannel( buffer );
360 astrand 933 }
361     }
362 astrand 993
363     return CallNextHookEx( hhook, nCode, wParam, lParam );
364 astrand 930 }
365    
366    
367 astrand 993 LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam )
368 astrand 930 {
369 astrand 993 if ( nCode < 0 ) {
370     return CallNextHookEx( hhook, nCode, wParam, lParam );
371 astrand 933 }
372 astrand 993
373     if ( ChannelIsOpen() ) {
374 astrand 933 PCHAR buffer = NULL;
375 astrand 993
376     char windowTitle[ 150 ] = { ""
377     };
378 astrand 933 HWND windowHandle = NULL;
379 astrand 993 char result[ 255 ] = { ""
380     };
381     char strWindowId[ 25 ];
382 astrand 933 LONG b, t, l, r;
383 astrand 994 char strW[ 5 ];
384     char strY[ 5 ];
385     char strX[ 5 ];
386     char strH[ 5 ];
387 astrand 933 RECT rect;
388 astrand 993
389     switch ( nCode ) {
390     case HSHELL_WINDOWCREATED:
391    
392 astrand 933 //get window id
393 astrand 993 windowHandle = ( HWND ) wParam;
394     itoa( ( int ) windowHandle, strWindowId, 10 );
395    
396 astrand 933 //get coords
397 astrand 993 GetWindowRect( windowHandle, &rect );
398 astrand 933 b = rect.bottom;
399     t = rect.top;
400     l = rect.left;
401     r = rect.right;
402 astrand 994 ltoa( b - t, strH, 10 );
403     ltoa( t, strY, 10 );
404     ltoa( r - l, strW, 10 );
405     ltoa( l, strX, 10 );
406 astrand 993
407 astrand 933 //get name
408 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
409    
410 astrand 933 ////setup return string
411 astrand 994 strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" );
412 astrand 993 strcat( result, "ID=" );
413     strcat( result, strWindowId );
414     strcat( result, ";" );
415     strcat( result, "TITLE=" );
416     strcat( result, windowTitle );
417     strcat( result, ";" );
418 astrand 994 strcat( result, "X=" );
419     strcat( result, strX );
420 astrand 993 strcat( result, ";" );
421 astrand 994 strcat( result, "Y=" );
422     strcat( result, strY );
423     strcat( result, ";" );
424     strcat( result, "H=" );
425     strcat( result, strH );
426     strcat( result, ";" );
427     strcat( result, "W=" );
428     strcat( result, strW );
429     strcat( result, "." );
430 astrand 993
431 astrand 933 buffer = result;
432 astrand 993
433 astrand 933 break;
434 astrand 993
435     case HSHELL_WINDOWDESTROYED:
436    
437 astrand 933 //get window id
438 astrand 993 windowHandle = ( HWND ) wParam;
439     itoa( ( int ) windowHandle, strWindowId, 10 );
440    
441 astrand 994 //get coords
442     GetWindowRect( windowHandle, &rect );
443     b = rect.bottom;
444     t = rect.top;
445     l = rect.left;
446     r = rect.right;
447     ltoa( b - t, strH, 10 );
448     ltoa( t, strY, 10 );
449     ltoa( r - l, strW, 10 );
450     ltoa( l, strX, 10 );
451    
452 astrand 933 //get name
453 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
454    
455 astrand 933 ////setup return string
456 astrand 994 strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" );
457 astrand 993 strcat( result, "ID=" );
458     strcat( result, strWindowId );
459     strcat( result, ";" );
460     strcat( result, "TITLE=" );
461     strcat( result, windowTitle );
462     strcat( result, ";" );
463 astrand 994 strcat( result, "X=" );
464     strcat( result, strX );
465     strcat( result, ";" );
466     strcat( result, "Y=" );
467     strcat( result, strY );
468     strcat( result, ";" );
469     strcat( result, "H=" );
470     strcat( result, strH );
471     strcat( result, ";" );
472     strcat( result, "W=" );
473     strcat( result, strW );
474     strcat( result, "." );
475 astrand 993
476 astrand 933 buffer = result;
477 astrand 993
478 astrand 933 break;
479 astrand 993 default:
480 astrand 933 break;
481     }
482 astrand 993
483     if ( buffer != NULL ) {
484     WriteToChannel( buffer );
485 astrand 933 }
486     }
487 astrand 993
488     return CallNextHookEx( hhook, nCode, wParam, lParam );
489 astrand 930 }
490    
491 astrand 993 DLL_EXPORT void SetCbtHook( void )
492 astrand 930 {
493 astrand 993 if ( !bHooked ) {
494 astrand 994 hhook = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );
495 astrand 933 bHooked = true;
496     }
497 astrand 993
498     if ( !bHooked2 ) {
499 astrand 994 hhook2 = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL );
500 astrand 933 bHooked2 = true;
501     }
502 astrand 993
503     if ( !bHooked3 ) {
504 astrand 994 hhook3 = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL );
505 astrand 933 bHooked3 = true;
506     }
507 astrand 930 }
508    
509 astrand 993 DLL_EXPORT void RemoveCbtHook( void )
510 astrand 930 {
511 astrand 993 if ( bHooked ) {
512     UnhookWindowsHookEx( hhook );
513 astrand 933 bHooked = false;
514     }
515 astrand 993
516     if ( bHooked2 ) {
517     UnhookWindowsHookEx( hhook2 );
518 astrand 933 bHooked2 = false;
519     }
520 astrand 993
521     if ( bHooked3 ) {
522     UnhookWindowsHookEx( hhook3 );
523 astrand 933 bHooked3 = false;
524     }
525 astrand 930 }
526    
527     DLL_EXPORT int GetInstanceCount()
528     {
529 astrand 933 return iInstanceCount;
530 astrand 930 }
531    
532     int OpenVirtualChannel()
533     {
534 astrand 994 m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME );
535    
536 astrand 993 if ( m_vcHandle == NULL ) {
537 astrand 933 return 0;
538 astrand 937 } else {
539 astrand 933 return 1;
540     }
541 astrand 930 }
542    
543     int CloseVirtualChannel()
544     {
545 astrand 993 BOOL result = WTSVirtualChannelClose( m_vcHandle );
546    
547 astrand 933 m_vcHandle = NULL;
548 astrand 993
549     if ( result ) {
550 astrand 933 return 1;
551 astrand 937 } else {
552 astrand 933 return 0;
553     }
554 astrand 930 }
555    
556     int ChannelIsOpen()
557     {
558 astrand 993 if ( m_vcHandle == NULL ) {
559 astrand 933 return 0;
560 astrand 937 } else {
561 astrand 933 return 1;
562     }
563 astrand 930 }
564    
565 astrand 993 int WriteToChannel( PCHAR buffer )
566 astrand 930 {
567 astrand 933 PULONG bytesRead = 0;
568     PULONG pBytesWritten = 0;
569 astrand 993
570 astrand 994 BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten );
571    
572 astrand 993 if ( result ) {
573 astrand 933 return 1;
574 astrand 937 } else {
575 astrand 933 return 0;
576     }
577 astrand 930 }

  ViewVC Help
Powered by ViewVC 1.1.26