/[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

Contents of /sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 993 - (show 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 //
2 // Copyright (C) 2004-2005 Martin Wickett
3 //
4
5 #include "hookdll.h"
6 #include <windows.h>
7 #include <winuser.h>
8
9 #include "wtsapi32.h"
10 #include "Cchannel.h"
11
12 #define DLL_EXPORT extern "C" __declspec(dllexport)
13
14 // Shared DATA
15 #pragma data_seg ( "SHAREDDATA" )
16
17 // this is the total number of processes this dll is currently attached to
18 int iInstanceCount = 0;
19 HWND hWnd = 0;
20
21 #pragma data_seg ()
22
23 #pragma comment(linker, "/section:SHAREDDATA,rws")
24
25 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
34 BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call,
35 LPVOID lpReserved )
36 {
37 switch ( ul_reason_for_call ) {
38 case DLL_PROCESS_ATTACH: {
39 // remember our instance handle
40 hInst = hinstDLL;
41 ++iInstanceCount;
42 OpenVirtualChannel();
43 break;
44 }
45
46 case DLL_THREAD_ATTACH:
47 break;
48 case DLL_THREAD_DETACH:
49 break;
50 case DLL_PROCESS_DETACH: {
51 --iInstanceCount;
52 CloseVirtualChannel();
53 }
54 break;
55 }
56
57 return TRUE;
58 }
59
60 LRESULT CALLBACK CallWndProc( int nCode, WPARAM wParam, LPARAM lParam )
61 {
62 if ( nCode < 0 ) {
63 return CallNextHookEx( hhook3, nCode, wParam, lParam );
64 }
65
66 PCHAR buffer = NULL;
67 char windowTitle[ 150 ] = { ""
68 };
69 HWND windowHandle = NULL;
70 char result[ 255 ] = { ""
71 };
72 char strWindowId[ 25 ];
73 char type[ 25 ];
74
75 LONG b, t, l, r;
76 char strB[ 5 ];
77 char strT[ 5 ];
78 char strL[ 5 ];
79 char strR[ 5 ];
80 RECT rect;
81
82 CWPSTRUCT *details = ( CWPSTRUCT * ) lParam;
83
84 switch ( details->message ) {
85 case WM_SIZING:
86 case WM_MOVING:
87
88 windowHandle = details->hwnd;
89 //get win name
90 GetWindowText( windowHandle, windowTitle, 150 );
91
92 //get an id for it
93 itoa( ( int ) windowHandle, strWindowId, 10 );
94
95 //get coords
96 GetWindowRect( windowHandle, &rect );
97 b = rect.bottom;
98 t = rect.top;
99 l = rect.left;
100 r = rect.right;
101 ltoa( b, strB, 10 );
102 ltoa( t, strT, 10 );
103 ltoa( r, strR, 10 );
104 ltoa( l, strL, 10 );
105
106 ////setup return string
107 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 buffer = result;
125
126 break;
127
128 default:
129 break;
130 }
131
132 if ( ChannelIsOpen() ) {
133 if ( buffer != NULL ) {
134 WriteToChannel( buffer );
135 }
136 }
137
138 return CallNextHookEx( hhook3, nCode, wParam, lParam );
139 }
140
141 LRESULT CALLBACK CbtProc( int nCode, WPARAM wParam, LPARAM lParam )
142 {
143 if ( nCode < 0 ) {
144 return CallNextHookEx( hhook, nCode, wParam, lParam );
145 }
146
147
148 PCHAR buffer = NULL;
149
150
151 char windowTitle[ 150 ] = { ""
152 };
153 HWND windowHandle = NULL;
154 char result[ 255 ] = { ""
155 };
156 char strWindowId[ 25 ];
157 char type[ 25 ];
158
159
160 LONG b, t, l, r;
161 char strB[ 5 ];
162 char strT[ 5 ];
163 char strL[ 5 ];
164 char strR[ 5 ];
165 RECT rect;
166
167 int i = 0; //tmp
168
169 switch ( nCode ) {
170 case HCBT_MINMAX:
171
172 windowHandle = ( HWND ) wParam;
173 //get win name
174 GetWindowText( windowHandle, windowTitle, 150 );
175
176 //get an id for it
177 itoa( ( int ) windowHandle, strWindowId, 10 );
178
179 //get operation type(min,max). if max, do not clip at all,if min use window's previous coords
180 //codes are:
181
182 // 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
186 itoa( ( int ) lParam, type, 10 );
187
188 //get coords
189 GetWindowRect( windowHandle, &rect );
190 b = rect.bottom;
191 t = rect.top;
192 l = rect.left;
193 r = rect.right;
194 ltoa( b, strB, 10 );
195 ltoa( t, strT, 10 );
196 ltoa( r, strR, 10 );
197 ltoa( l, strL, 10 );
198
199 //get name
200 GetWindowText( windowHandle, windowTitle, 150 );
201
202 ////setup return string
203 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 //-------------------------------------------------------------------------------------------------
224 // code to prevent minimising windows (can be removed once minimise has been implemented)
225 i = ( int ) lParam;
226 //if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11)
227 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 return 1;
232 }
233 //-----------------------------------------------------------------------------------------
234
235 //-------------------------------------------------------------------------------------------------
236 // code to prevent maximising windows (can be removed once maximise has been implemented)
237 i = ( int ) lParam;
238 //if (i==3 || i==9 || i==11)
239 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 return 1;
244 }
245 //-----------------------------------------------------------------------------------------
246
247 buffer = result;
248
249 break;
250
251 case HCBT_MOVESIZE:
252
253 windowHandle = ( HWND ) wParam;
254 //get win name
255 GetWindowText( windowHandle, windowTitle, 150 );
256
257 //get an id for it
258 itoa( ( int ) windowHandle, strWindowId, 10 );
259
260 //get coords
261 GetWindowRect( windowHandle, &rect );
262 b = rect.bottom;
263 t = rect.top;
264 l = rect.left;
265 r = rect.right;
266 ltoa( b, strB, 10 );
267 ltoa( t, strT, 10 );
268 ltoa( r, strR, 10 );
269 ltoa( l, strL, 10 );
270
271 //get name
272 GetWindowText( windowHandle, windowTitle, 150 );
273
274 ////setup return string
275 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 buffer = result;
293
294 break;
295 case HCBT_SETFOCUS:
296 //buffer = "HCBT_SETFOCUS";
297 //not needed yet
298 break;
299 default:
300 break;
301 }
302
303 if ( ChannelIsOpen() ) {
304 if ( buffer != NULL ) {
305 WriteToChannel( buffer );
306 }
307 }
308
309 return CallNextHookEx( hhook, nCode, wParam, lParam );
310 }
311
312
313 LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam )
314 {
315 if ( nCode < 0 ) {
316 return CallNextHookEx( hhook, nCode, wParam, lParam );
317 }
318
319 if ( ChannelIsOpen() ) {
320 PCHAR buffer = NULL;
321
322 char windowTitle[ 150 ] = { ""
323 };
324 HWND windowHandle = NULL;
325 char result[ 255 ] = { ""
326 };
327 char strWindowId[ 25 ];
328 LONG b, t, l, r;
329 char strB[ 5 ];
330 char strT[ 5 ];
331 char strL[ 5 ];
332 char strR[ 5 ];
333 RECT rect;
334
335 switch ( nCode ) {
336 case HSHELL_WINDOWCREATED:
337
338 //get window id
339 windowHandle = ( HWND ) wParam;
340 itoa( ( int ) windowHandle, strWindowId, 10 );
341
342 //get coords
343 GetWindowRect( windowHandle, &rect );
344 b = rect.bottom;
345 t = rect.top;
346 l = rect.left;
347 r = rect.right;
348 ltoa( b, strB, 10 );
349 ltoa( t, strT, 10 );
350 ltoa( r, strR, 10 );
351 ltoa( l, strL, 10 );
352
353 //get name
354 GetWindowText( windowHandle, windowTitle, 150 );
355
356 ////setup return string
357 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 buffer = result;
375
376 break;
377
378 case HSHELL_WINDOWDESTROYED:
379
380 //get window id
381 windowHandle = ( HWND ) wParam;
382 itoa( ( int ) windowHandle, strWindowId, 10 );
383
384 //get name
385 GetWindowText( windowHandle, windowTitle, 150 );
386
387 ////setup return string
388 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 buffer = result;
397
398 break;
399 default:
400 break;
401 }
402
403 if ( buffer != NULL ) {
404 WriteToChannel( buffer );
405 }
406 }
407
408 return CallNextHookEx( hhook, nCode, wParam, lParam );
409 }
410
411 DLL_EXPORT void SetCbtHook( void )
412 {
413 if ( !bHooked ) {
414 hhook =
415 SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );
416 bHooked = true;
417 }
418
419 if ( !bHooked2 ) {
420 hhook2 =
421 SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst,
422 ( DWORD ) NULL );
423 bHooked2 = true;
424 }
425
426 if ( !bHooked3 ) {
427 hhook3 =
428 SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst,
429 ( DWORD ) NULL );
430 bHooked3 = true;
431 }
432 }
433
434 DLL_EXPORT void RemoveCbtHook( void )
435 {
436 if ( bHooked ) {
437 UnhookWindowsHookEx( hhook );
438 bHooked = false;
439 }
440
441 if ( bHooked2 ) {
442 UnhookWindowsHookEx( hhook2 );
443 bHooked2 = false;
444 }
445
446 if ( bHooked3 ) {
447 UnhookWindowsHookEx( hhook3 );
448 bHooked3 = false;
449 }
450 }
451
452 DLL_EXPORT int GetInstanceCount()
453 {
454 return iInstanceCount;
455 }
456
457 int OpenVirtualChannel()
458 {
459 m_vcHandle =
460 WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION,
461 CHANNELNAME );
462
463 if ( m_vcHandle == NULL ) {
464 return 0;
465 } else {
466 return 1;
467 }
468 }
469
470 int CloseVirtualChannel()
471 {
472 BOOL result = WTSVirtualChannelClose( m_vcHandle );
473
474 m_vcHandle = NULL;
475
476 if ( result ) {
477 return 1;
478 } else {
479 return 0;
480 }
481 }
482
483 int ChannelIsOpen()
484 {
485 if ( m_vcHandle == NULL ) {
486 return 0;
487 } else {
488 return 1;
489 }
490 }
491
492 int WriteToChannel( PCHAR buffer )
493 {
494 PULONG bytesRead = 0;
495 PULONG pBytesWritten = 0;
496
497 BOOL result =
498 WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ),
499 pBytesWritten );
500
501 if ( result ) {
502 return 1;
503 } else {
504 return 0;
505 }
506 }

  ViewVC Help
Powered by ViewVC 1.1.26