/[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 1006 - (hide annotations)
Tue Aug 30 11:52:38 2005 UTC (18 years, 9 months ago) by astrand
File size: 11638 byte(s)
WM_CREATE now sends SETSTATE1 and POSITION1.

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 astrand 1004 WriteToChannel( "DEBUG1," );
49     WriteToChannel( buf );
50     WriteToChannel( "\n" );
51 astrand 998 }
52    
53    
54    
55 astrand 994 BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved )
56 astrand 930 {
57 astrand 993 switch ( ul_reason_for_call ) {
58     case DLL_PROCESS_ATTACH: {
59 astrand 933 // remember our instance handle
60     hInst = hinstDLL;
61     ++iInstanceCount;
62     OpenVirtualChannel();
63     break;
64     }
65 astrand 993
66     case DLL_THREAD_ATTACH:
67 astrand 933 break;
68 astrand 993 case DLL_THREAD_DETACH:
69 astrand 933 break;
70 astrand 993 case DLL_PROCESS_DETACH: {
71 astrand 933 --iInstanceCount;
72     CloseVirtualChannel();
73     }
74     break;
75 astrand 930 }
76 astrand 993
77 astrand 930 return TRUE;
78     }
79    
80 astrand 993 LRESULT CALLBACK CallWndProc( int nCode, WPARAM wParam, LPARAM lParam )
81 astrand 930 {
82 astrand 993 if ( nCode < 0 ) {
83     return CallNextHookEx( hhook3, nCode, wParam, lParam );
84 astrand 933 }
85 astrand 993
86     char windowTitle[ 150 ] = { ""
87     };
88 astrand 933 HWND windowHandle = NULL;
89 astrand 994 HWND windowHandle2 = NULL;
90 astrand 993 char result[ 255 ] = { ""
91     };
92     CWPSTRUCT *details = ( CWPSTRUCT * ) lParam;
93 astrand 995 CREATESTRUCT *cs = ( CREATESTRUCT * ) details->lParam;
94     LONG dwStyle = GetWindowLong( details->hwnd, GWL_STYLE );
95 astrand 998 WINDOWPOS *wp = ( WINDOWPOS * ) details->lParam;
96     RECT *rect = ( RECT * ) details->lParam;
97 astrand 993
98     switch ( details->message ) {
99 astrand 998
100 astrand 993 case WM_SIZING:
101     case WM_MOVING:
102 astrand 1000 if ( !( dwStyle & WS_VISIBLE ) )
103     break;
104    
105     if ( !( dwStyle & WS_DLGFRAME ) )
106     break;
107    
108 astrand 998 snprintf( result, sizeof( result ),
109 astrand 1002 "POSITION1,0x%p,%d,%d,%d,%d,0x%x",
110     details->hwnd,
111 astrand 998 rect->left, rect->top,
112     rect->right - rect->left,
113     rect->bottom - rect->top,
114     0 );
115     result[ sizeof( result ) - 1 ] = '\0';
116 astrand 1005 WriteToChannel( result );
117 astrand 933 break;
118 astrand 993
119 astrand 1000
120 astrand 998 /* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are
121     strange. Sometimes, for example when bringing up the
122     Notepad About dialog, only an WM_WINDOWPOSCHANGING is
123     sent. In some other cases, for exmaple when opening
124     Format->Text in Notepad, both events are sent. Also, for
125     some reason, when closing the Notepad About dialog, an
126     WM_WINDOWPOSCHANGING event is sent which looks just like
127     the event that was sent when the About dialog was opened... */
128     case WM_WINDOWPOSCHANGING:
129 astrand 994
130 astrand 998 if ( !( dwStyle & WS_VISIBLE ) )
131     break;
132    
133     if ( !( dwStyle & WS_DLGFRAME ) )
134     break;
135    
136     if ( !( wp->flags & SWP_NOZORDER ) ) {
137     snprintf( result, sizeof( result ),
138 astrand 1002 "ZCHANGE1,0x%p,0x%p,0x%x\n",
139 astrand 998 details->hwnd,
140     wp->flags & SWP_NOACTIVATE ? wp->hwndInsertAfter : 0,
141     0 );
142     result[ sizeof( result ) - 1 ] = '\0';
143 astrand 1005 WriteToChannel( result );
144 astrand 998 }
145 astrand 994 break;
146    
147 astrand 995
148     case WM_CREATE:
149     if ( cs->style & WS_DLGFRAME ) {
150 astrand 1006
151 astrand 1000 snprintf( result, sizeof( result ),
152 astrand 1002 "CREATE1,0x%p,0x%x\n",
153 astrand 1000 details->hwnd, 0 );
154 astrand 1005 result[ sizeof( result ) - 1 ] = '\0';
155     WriteToChannel( result );
156 astrand 1006
157     snprintf( result, sizeof( result ),
158     "SETSTATE1,0x%p,%s,0x%x,0x%x\n",
159     details->hwnd,
160     cs->lpszName,
161     1, // FIXME: Check for WS_MAXIMIZE/WS_MINIMIZE
162     0 );
163     result[ sizeof( result ) - 1 ] = '\0';
164     WriteToChannel( result );
165    
166     snprintf( result, sizeof( result ),
167     "POSITION1,0x%p,%d,%d,%d,%d,0x%x",
168     details->hwnd,
169     cs->x,
170     cs->y,
171     cs->cx,
172     cs->cy,
173     0 );
174     result[ sizeof( result ) - 1 ] = '\0';
175     WriteToChannel( result );
176    
177 astrand 995 }
178     break;
179    
180    
181     case WM_DESTROY:
182     if ( dwStyle & WS_DLGFRAME ) {
183 astrand 1001 snprintf( result, sizeof( result ),
184 astrand 1002 "DESTROY1,0x%p,0x%x\n",
185 astrand 1001 details->hwnd, 0 );
186 astrand 1005 result[ sizeof( result ) - 1 ] = '\0';
187     WriteToChannel( result );
188 astrand 995 }
189    
190     break;
191    
192 astrand 1000
193 astrand 993 default:
194 astrand 933 break;
195     }
196 astrand 993
197     return CallNextHookEx( hhook3, nCode, wParam, lParam );
198 astrand 930 }
199    
200 astrand 993 LRESULT CALLBACK CbtProc( int nCode, WPARAM wParam, LPARAM lParam )
201 astrand 930 {
202 astrand 993 if ( nCode < 0 ) {
203     return CallNextHookEx( hhook, nCode, wParam, lParam );
204 astrand 933 }
205 astrand 993
206     char windowTitle[ 150 ] = { ""
207     };
208 astrand 933 HWND windowHandle = NULL;
209 astrand 993 char result[ 255 ] = { ""
210     };
211     switch ( nCode ) {
212     case HCBT_MINMAX:
213    
214 astrand 1002 if ( ( LOWORD( lParam ) == SW_SHOWMINIMIZED )
215     || ( LOWORD( lParam ) == SW_MINIMIZE ) ) {
216     MessageBox( 0, "Minimizing windows is not allowed in this version. Sorry!", "SeamlessRDP", MB_OK );
217     return 1;
218     }
219 astrand 993
220 astrand 1002 GetWindowText( ( HWND ) wParam, windowTitle, 150 );
221 astrand 993
222 astrand 1002 snprintf( result, sizeof( result ),
223     "SETSTATE1,0x%p,%s,0x%x,0x%x\n",
224     ( HWND ) wParam,
225     windowTitle,
226     LOWORD( lParam ),
227     0 );
228 astrand 1005 result[ sizeof( result ) - 1 ] = '\0';
229     WriteToChannel( result );
230 astrand 933 break;
231 astrand 993
232 astrand 1002
233 astrand 993 default:
234 astrand 933 break;
235     }
236 astrand 993
237    
238 astrand 1005
239 astrand 993 return CallNextHookEx( hhook, nCode, wParam, lParam );
240 astrand 930 }
241    
242    
243 astrand 993 LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam )
244 astrand 930 {
245 astrand 993 if ( nCode < 0 ) {
246     return CallNextHookEx( hhook, nCode, wParam, lParam );
247 astrand 933 }
248 astrand 993
249 astrand 1004 char windowTitle[ 150 ] = { ""
250     };
251     HWND windowHandle = NULL;
252     char result[ 255 ] = { ""
253     };
254     char strWindowId[ 25 ];
255     LONG b, t, l, r;
256     char strW[ 5 ];
257     char strY[ 5 ];
258     char strX[ 5 ];
259     char strH[ 5 ];
260     RECT rect;
261    
262     switch ( nCode ) {
263     case HSHELL_WINDOWCREATED:
264 astrand 993
265 astrand 1004 //get window id
266     windowHandle = ( HWND ) wParam;
267     itoa( ( int ) windowHandle, strWindowId, 10 );
268 astrand 993
269 astrand 1004 //get coords
270     GetWindowRect( windowHandle, &rect );
271     b = rect.bottom;
272     t = rect.top;
273     l = rect.left;
274     r = rect.right;
275     ltoa( b - t, strH, 10 );
276     ltoa( t, strY, 10 );
277     ltoa( r - l, strW, 10 );
278     ltoa( l, strX, 10 );
279    
280     //get name
281     GetWindowText( windowHandle, windowTitle, 150 );
282    
283     ////setup return string
284     strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" );
285     strcat( result, "ID=" );
286     strcat( result, strWindowId );
287     strcat( result, ";" );
288     strcat( result, "TITLE=" );
289     strcat( result, windowTitle );
290     strcat( result, ";" );
291     strcat( result, "X=" );
292     strcat( result, strX );
293     strcat( result, ";" );
294     strcat( result, "Y=" );
295     strcat( result, strY );
296     strcat( result, ";" );
297     strcat( result, "H=" );
298     strcat( result, strH );
299     strcat( result, ";" );
300     strcat( result, "W=" );
301     strcat( result, strW );
302     strcat( result, "." );
303 astrand 1005 WriteToChannel( result );
304 astrand 1004 break;
305    
306     case HSHELL_WINDOWDESTROYED:
307    
308     //get window id
309     windowHandle = ( HWND ) wParam;
310     itoa( ( int ) windowHandle, strWindowId, 10 );
311    
312     //get coords
313     GetWindowRect( windowHandle, &rect );
314     b = rect.bottom;
315     t = rect.top;
316     l = rect.left;
317     r = rect.right;
318     ltoa( b - t, strH, 10 );
319     ltoa( t, strY, 10 );
320     ltoa( r - l, strW, 10 );
321     ltoa( l, strX, 10 );
322    
323     //get name
324     GetWindowText( windowHandle, windowTitle, 150 );
325    
326     ////setup return string
327     strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" );
328     strcat( result, "ID=" );
329     strcat( result, strWindowId );
330     strcat( result, ";" );
331     strcat( result, "TITLE=" );
332     strcat( result, windowTitle );
333     strcat( result, ";" );
334     strcat( result, "X=" );
335     strcat( result, strX );
336     strcat( result, ";" );
337     strcat( result, "Y=" );
338     strcat( result, strY );
339     strcat( result, ";" );
340     strcat( result, "H=" );
341     strcat( result, strH );
342     strcat( result, ";" );
343     strcat( result, "W=" );
344     strcat( result, strW );
345     strcat( result, "." );
346 astrand 1005 WriteToChannel( result );
347     break;
348 astrand 1004
349 astrand 1005
350 astrand 1004 default:
351     break;
352 astrand 933 }
353 astrand 993
354 astrand 1004
355 astrand 993 return CallNextHookEx( hhook, nCode, wParam, lParam );
356 astrand 930 }
357    
358 astrand 993 DLL_EXPORT void SetCbtHook( void )
359 astrand 930 {
360 astrand 993 if ( !bHooked ) {
361 astrand 994 hhook = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );
362 astrand 933 bHooked = true;
363     }
364 astrand 993
365     if ( !bHooked2 ) {
366 astrand 994 hhook2 = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL );
367 astrand 933 bHooked2 = true;
368     }
369 astrand 993
370     if ( !bHooked3 ) {
371 astrand 994 hhook3 = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL );
372 astrand 933 bHooked3 = true;
373     }
374 astrand 930 }
375    
376 astrand 993 DLL_EXPORT void RemoveCbtHook( void )
377 astrand 930 {
378 astrand 993 if ( bHooked ) {
379     UnhookWindowsHookEx( hhook );
380 astrand 933 bHooked = false;
381     }
382 astrand 993
383     if ( bHooked2 ) {
384     UnhookWindowsHookEx( hhook2 );
385 astrand 933 bHooked2 = false;
386     }
387 astrand 993
388     if ( bHooked3 ) {
389     UnhookWindowsHookEx( hhook3 );
390 astrand 933 bHooked3 = false;
391     }
392 astrand 930 }
393    
394     DLL_EXPORT int GetInstanceCount()
395     {
396 astrand 933 return iInstanceCount;
397 astrand 930 }
398    
399     int OpenVirtualChannel()
400     {
401 astrand 994 m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME );
402    
403 astrand 993 if ( m_vcHandle == NULL ) {
404 astrand 933 return 0;
405 astrand 937 } else {
406 astrand 933 return 1;
407     }
408 astrand 930 }
409    
410     int CloseVirtualChannel()
411     {
412 astrand 993 BOOL result = WTSVirtualChannelClose( m_vcHandle );
413    
414 astrand 933 m_vcHandle = NULL;
415 astrand 993
416     if ( result ) {
417 astrand 933 return 1;
418 astrand 937 } else {
419 astrand 933 return 0;
420     }
421 astrand 930 }
422    
423     int ChannelIsOpen()
424     {
425 astrand 993 if ( m_vcHandle == NULL ) {
426 astrand 933 return 0;
427 astrand 937 } else {
428 astrand 933 return 1;
429     }
430 astrand 930 }
431    
432 astrand 993 int WriteToChannel( PCHAR buffer )
433 astrand 930 {
434 astrand 933 PULONG bytesRead = 0;
435     PULONG pBytesWritten = 0;
436 astrand 993
437 astrand 1004 if ( !ChannelIsOpen() )
438     return 1;
439    
440 astrand 994 BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten );
441    
442 astrand 993 if ( result ) {
443 astrand 933 return 1;
444 astrand 937 } else {
445 astrand 933 return 0;
446     }
447 astrand 930 }

  ViewVC Help
Powered by ViewVC 1.1.26