/[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 1002 - (hide annotations)
Tue Aug 30 11:17:52 2005 UTC (18 years, 9 months ago) by astrand
File size: 11526 byte(s)
Fixed compiler warnings: Removed unused variables. Using %p for
converting HWND to hex.

Implemented SETSTATE1.

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

  ViewVC Help
Powered by ViewVC 1.1.26