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