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