/[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 1001 - (hide annotations)
Tue Aug 30 09:48:07 2005 UTC (18 years, 10 months ago) by astrand
File size: 14522 byte(s)
Implemented DESTROY1.

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 1000 if ( !( dwStyle & WS_VISIBLE ) )
115     break;
116    
117     if ( !( dwStyle & WS_DLGFRAME ) )
118     break;
119    
120 astrand 998 snprintf( result, sizeof( result ),
121     "POSITION1,0x%x,%d,%d,%d,%d,0x%x",
122     ( int ) details->hwnd,
123     rect->left, rect->top,
124     rect->right - rect->left,
125     rect->bottom - rect->top,
126     0 );
127     result[ sizeof( result ) - 1 ] = '\0';
128 astrand 933 buffer = result;
129     break;
130 astrand 993
131 astrand 1000
132 astrand 998 /* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are
133     strange. Sometimes, for example when bringing up the
134     Notepad About dialog, only an WM_WINDOWPOSCHANGING is
135     sent. In some other cases, for exmaple when opening
136     Format->Text in Notepad, both events are sent. Also, for
137     some reason, when closing the Notepad About dialog, an
138     WM_WINDOWPOSCHANGING event is sent which looks just like
139     the event that was sent when the About dialog was opened... */
140     case WM_WINDOWPOSCHANGING:
141 astrand 994
142 astrand 998 if ( !( dwStyle & WS_VISIBLE ) )
143     break;
144    
145     if ( !( dwStyle & WS_DLGFRAME ) )
146     break;
147    
148     if ( !( wp->flags & SWP_NOZORDER ) ) {
149     snprintf( result, sizeof( result ),
150     "ZCHANGE1,0x%x,0x%x,0x%x\n",
151     details->hwnd,
152     wp->flags & SWP_NOACTIVATE ? wp->hwndInsertAfter : 0,
153     0 );
154     result[ sizeof( result ) - 1 ] = '\0';
155 astrand 1000 buffer = result;
156 astrand 998 }
157 astrand 994 break;
158    
159 astrand 995
160     case WM_CREATE:
161     if ( cs->style & WS_DLGFRAME ) {
162 astrand 1000 snprintf( result, sizeof( result ),
163     "CREATE1,0x%x,0x%x\n",
164     details->hwnd, 0 );
165 astrand 995 buffer = result;
166     }
167     break;
168    
169    
170     case WM_DESTROY:
171     if ( dwStyle & WS_DLGFRAME ) {
172 astrand 1001 snprintf( result, sizeof( result ),
173     "DESTROY1,0x%x,0x%x\n",
174     details->hwnd, 0 );
175 astrand 995 buffer = result;
176     }
177    
178     break;
179    
180 astrand 1000
181 astrand 993 default:
182 astrand 933 break;
183     }
184 astrand 993
185     if ( ChannelIsOpen() ) {
186     if ( buffer != NULL ) {
187     WriteToChannel( buffer );
188 astrand 933 }
189     }
190 astrand 993
191     return CallNextHookEx( hhook3, nCode, wParam, lParam );
192 astrand 930 }
193    
194 astrand 993 LRESULT CALLBACK CbtProc( int nCode, WPARAM wParam, LPARAM lParam )
195 astrand 930 {
196 astrand 993 if ( nCode < 0 ) {
197     return CallNextHookEx( hhook, nCode, wParam, lParam );
198 astrand 933 }
199 astrand 993
200    
201 astrand 933 PCHAR buffer = NULL;
202 astrand 993
203    
204     char windowTitle[ 150 ] = { ""
205     };
206 astrand 933 HWND windowHandle = NULL;
207 astrand 993 char result[ 255 ] = { ""
208     };
209     char strWindowId[ 25 ];
210     char type[ 25 ];
211    
212    
213 astrand 933 LONG b, t, l, r;
214 astrand 994 char strW[ 5 ];
215     char strY[ 5 ];
216     char strX[ 5 ];
217     char strH[ 5 ];
218 astrand 933 RECT rect;
219 astrand 993
220 astrand 994 int i = 0; //tmp
221 astrand 993
222     switch ( nCode ) {
223     case HCBT_MINMAX:
224    
225     windowHandle = ( HWND ) wParam;
226 astrand 933 //get win name
227 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
228    
229 astrand 933 //get an id for it
230 astrand 993 itoa( ( int ) windowHandle, strWindowId, 10 );
231    
232 astrand 933 //get operation type(min,max). if max, do not clip at all,if min use window's previous coords
233     //codes are:
234 astrand 993
235 astrand 937 // SW_HIDE= 0 SW_SHOWNORMAL=1 SW_NORMAL=1 SW_SHOWMINIMIZED=2 SW_SHOWMAXIMIZED=3 SW_MAXIMIZE=3
236     // SW_SHOWNOACTIVATE=4 SW_SHOW=5 SW_MINIMIZE=6 SW_SHOWMINNOACTIVE=7 SW_SHOWNA=8 SW_RESTORE=9
237     // SW_SHOWDEFAULT=10 SW_FORCEMINIMIZE=11 SW_MAX=11
238 astrand 993
239     itoa( ( int ) lParam, type, 10 );
240    
241 astrand 933 //get coords
242 astrand 993 GetWindowRect( windowHandle, &rect );
243 astrand 933 b = rect.bottom;
244     t = rect.top;
245     l = rect.left;
246     r = rect.right;
247 astrand 994 ltoa( b - t, strW, 10 );
248     ltoa( t, strY, 10 );
249     ltoa( r - l, strH, 10 );
250     ltoa( l, strX, 10 );
251 astrand 993
252 astrand 933 //get name
253 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
254    
255 astrand 933 ////setup return string
256 astrand 994 strcat( result, "MSG=HCBT_MINMAX;OP=4;" );
257    
258     // SW_SHOWNOACTIVATE=4 SW_SHOW=5 SW_MINIMIZE=6 SW_SHOWMINNOACTIVE=7 SW_SHOWNA=8 SW_RESTORE=9
259     // SW_SHOWDEFAULT=10 SW_FORCEMINIMIZE=11 SW_MAX=11
260 astrand 993 strcat( result, "ID=" );
261     strcat( result, strWindowId );
262     strcat( result, ";" );
263     strcat( result, "TITLE=" );
264     strcat( result, windowTitle );
265     strcat( result, ";" );
266 astrand 994 strcat( result, "X=" );
267     strcat( result, strX );
268 astrand 993 strcat( result, ";" );
269 astrand 994 strcat( result, "Y=" );
270     strcat( result, strY );
271     strcat( result, ";" );
272     strcat( result, "H=" );
273     strcat( result, strH );
274     strcat( result, ";" );
275     strcat( result, "W=" );
276     strcat( result, strW );
277     strcat( result, ";" );
278 astrand 993 strcat( result, "TYPE=" );
279     strcat( result, type );
280 astrand 994 strcat( result, "." );
281 astrand 993
282 astrand 933 //-------------------------------------------------------------------------------------------------
283     // code to prevent minimising windows (can be removed once minimise has been implemented)
284 astrand 994 //i = (int)lParam;
285 astrand 933 //if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11)
286 astrand 994 //if ( i==2 || i==6 )
287     //{
288     // MessageBox(0,"Minimising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);
289     // return 1;
290     //}
291 astrand 933 //-----------------------------------------------------------------------------------------
292 astrand 993
293 astrand 933 //-------------------------------------------------------------------------------------------------
294     // code to prevent maximising windows (can be removed once maximise has been implemented)
295 astrand 994 //i = (int)lParam;
296 astrand 933 //if (i==3 || i==9 || i==11)
297 astrand 994 //if (i==3 || i==11)
298     //{
299     // MessageBox(0,"Maximising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);
300     // return 1;
301     //}
302 astrand 933 //-----------------------------------------------------------------------------------------
303 astrand 993
304 astrand 933 buffer = result;
305 astrand 993
306 astrand 933 break;
307 astrand 993
308     default:
309 astrand 933 break;
310     }
311 astrand 993
312     if ( ChannelIsOpen() ) {
313     if ( buffer != NULL ) {
314     WriteToChannel( buffer );
315 astrand 933 }
316     }
317 astrand 993
318     return CallNextHookEx( hhook, nCode, wParam, lParam );
319 astrand 930 }
320    
321    
322 astrand 993 LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam )
323 astrand 930 {
324 astrand 993 if ( nCode < 0 ) {
325     return CallNextHookEx( hhook, nCode, wParam, lParam );
326 astrand 933 }
327 astrand 993
328     if ( ChannelIsOpen() ) {
329 astrand 933 PCHAR buffer = NULL;
330 astrand 993
331     char windowTitle[ 150 ] = { ""
332     };
333 astrand 933 HWND windowHandle = NULL;
334 astrand 993 char result[ 255 ] = { ""
335     };
336     char strWindowId[ 25 ];
337 astrand 933 LONG b, t, l, r;
338 astrand 994 char strW[ 5 ];
339     char strY[ 5 ];
340     char strX[ 5 ];
341     char strH[ 5 ];
342 astrand 933 RECT rect;
343 astrand 993
344     switch ( nCode ) {
345     case HSHELL_WINDOWCREATED:
346    
347 astrand 933 //get window id
348 astrand 993 windowHandle = ( HWND ) wParam;
349     itoa( ( int ) windowHandle, strWindowId, 10 );
350    
351 astrand 933 //get coords
352 astrand 993 GetWindowRect( windowHandle, &rect );
353 astrand 933 b = rect.bottom;
354     t = rect.top;
355     l = rect.left;
356     r = rect.right;
357 astrand 994 ltoa( b - t, strH, 10 );
358     ltoa( t, strY, 10 );
359     ltoa( r - l, strW, 10 );
360     ltoa( l, strX, 10 );
361 astrand 993
362 astrand 933 //get name
363 astrand 993 GetWindowText( windowHandle, windowTitle, 150 );
364    
365 astrand 933 ////setup return string
366 astrand 994 strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" );
367 astrand 993 strcat( result, "ID=" );
368     strcat( result, strWindowId );
369     strcat( result, ";" );
370     strcat( result, "TITLE=" );
371     strcat( result, windowTitle );
372     strcat( result, ";" );
373 astrand 994 strcat( result, "X=" );
374     strcat( result, strX );
375 astrand 993 strcat( result, ";" );
376 astrand 994 strcat( result, "Y=" );
377     strcat( result, strY );
378     strcat( result, ";" );
379     strcat( result, "H=" );
380     strcat( result, strH );
381     strcat( result, ";" );
382     strcat( result, "W=" );
383     strcat( result, strW );
384     strcat( result, "." );
385 astrand 993
386 astrand 933 buffer = result;
387 astrand 993
388 astrand 933 break;
389 astrand 993
390     case HSHELL_WINDOWDESTROYED:
391    
392 astrand 933 //get window id
393 astrand 993 windowHandle = ( HWND ) wParam;
394     itoa( ( int ) windowHandle, strWindowId, 10 );
395    
396 astrand 994 //get coords
397     GetWindowRect( windowHandle, &rect );
398     b = rect.bottom;
399     t = rect.top;
400     l = rect.left;
401     r = rect.right;
402     ltoa( b - t, strH, 10 );
403     ltoa( t, strY, 10 );
404     ltoa( r - l, strW, 10 );
405     ltoa( l, strX, 10 );
406    
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_WINDOWDESTROYED;OP=1;" );
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     strcat( result, ";" );
421     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 default:
435 astrand 933 break;
436     }
437 astrand 993
438     if ( buffer != NULL ) {
439     WriteToChannel( buffer );
440 astrand 933 }
441     }
442 astrand 993
443     return CallNextHookEx( hhook, nCode, wParam, lParam );
444 astrand 930 }
445    
446 astrand 993 DLL_EXPORT void SetCbtHook( void )
447 astrand 930 {
448 astrand 993 if ( !bHooked ) {
449 astrand 994 hhook = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );
450 astrand 933 bHooked = true;
451     }
452 astrand 993
453     if ( !bHooked2 ) {
454 astrand 994 hhook2 = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL );
455 astrand 933 bHooked2 = true;
456     }
457 astrand 993
458     if ( !bHooked3 ) {
459 astrand 994 hhook3 = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL );
460 astrand 933 bHooked3 = true;
461     }
462 astrand 930 }
463    
464 astrand 993 DLL_EXPORT void RemoveCbtHook( void )
465 astrand 930 {
466 astrand 993 if ( bHooked ) {
467     UnhookWindowsHookEx( hhook );
468 astrand 933 bHooked = false;
469     }
470 astrand 993
471     if ( bHooked2 ) {
472     UnhookWindowsHookEx( hhook2 );
473 astrand 933 bHooked2 = false;
474     }
475 astrand 993
476     if ( bHooked3 ) {
477     UnhookWindowsHookEx( hhook3 );
478 astrand 933 bHooked3 = false;
479     }
480 astrand 930 }
481    
482     DLL_EXPORT int GetInstanceCount()
483     {
484 astrand 933 return iInstanceCount;
485 astrand 930 }
486    
487     int OpenVirtualChannel()
488     {
489 astrand 994 m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME );
490    
491 astrand 993 if ( m_vcHandle == NULL ) {
492 astrand 933 return 0;
493 astrand 937 } else {
494 astrand 933 return 1;
495     }
496 astrand 930 }
497    
498     int CloseVirtualChannel()
499     {
500 astrand 993 BOOL result = WTSVirtualChannelClose( m_vcHandle );
501    
502 astrand 933 m_vcHandle = NULL;
503 astrand 993
504     if ( result ) {
505 astrand 933 return 1;
506 astrand 937 } else {
507 astrand 933 return 0;
508     }
509 astrand 930 }
510    
511     int ChannelIsOpen()
512     {
513 astrand 993 if ( m_vcHandle == NULL ) {
514 astrand 933 return 0;
515 astrand 937 } else {
516 astrand 933 return 1;
517     }
518 astrand 930 }
519    
520 astrand 993 int WriteToChannel( PCHAR buffer )
521 astrand 930 {
522 astrand 933 PULONG bytesRead = 0;
523     PULONG pBytesWritten = 0;
524 astrand 993
525 astrand 994 BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten );
526    
527 astrand 993 if ( result ) {
528 astrand 933 return 1;
529 astrand 937 } else {
530 astrand 933 return 0;
531     }
532 astrand 930 }

  ViewVC Help
Powered by ViewVC 1.1.26