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 |
1000 |
snprintf( result, sizeof( result ), |
151 |
astrand |
1002 |
"CREATE1,0x%p,0x%x\n", |
152 |
astrand |
1000 |
details->hwnd, 0 ); |
153 |
astrand |
1005 |
result[ sizeof( result ) - 1 ] = '\0'; |
154 |
|
|
WriteToChannel( result ); |
155 |
astrand |
995 |
} |
156 |
|
|
break; |
157 |
|
|
|
158 |
|
|
|
159 |
|
|
case WM_DESTROY: |
160 |
|
|
if ( dwStyle & WS_DLGFRAME ) { |
161 |
astrand |
1001 |
snprintf( result, sizeof( result ), |
162 |
astrand |
1002 |
"DESTROY1,0x%p,0x%x\n", |
163 |
astrand |
1001 |
details->hwnd, 0 ); |
164 |
astrand |
1005 |
result[ sizeof( result ) - 1 ] = '\0'; |
165 |
|
|
WriteToChannel( result ); |
166 |
astrand |
995 |
} |
167 |
|
|
|
168 |
|
|
break; |
169 |
|
|
|
170 |
astrand |
1000 |
|
171 |
astrand |
993 |
default: |
172 |
astrand |
933 |
break; |
173 |
|
|
} |
174 |
astrand |
993 |
|
175 |
|
|
return CallNextHookEx( hhook3, nCode, wParam, lParam ); |
176 |
astrand |
930 |
} |
177 |
|
|
|
178 |
astrand |
993 |
LRESULT CALLBACK CbtProc( int nCode, WPARAM wParam, LPARAM lParam ) |
179 |
astrand |
930 |
{ |
180 |
astrand |
993 |
if ( nCode < 0 ) { |
181 |
|
|
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
182 |
astrand |
933 |
} |
183 |
astrand |
993 |
|
184 |
|
|
char windowTitle[ 150 ] = { "" |
185 |
|
|
}; |
186 |
astrand |
933 |
HWND windowHandle = NULL; |
187 |
astrand |
993 |
char result[ 255 ] = { "" |
188 |
|
|
}; |
189 |
|
|
switch ( nCode ) { |
190 |
|
|
case HCBT_MINMAX: |
191 |
|
|
|
192 |
astrand |
1002 |
if ( ( LOWORD( lParam ) == SW_SHOWMINIMIZED ) |
193 |
|
|
|| ( LOWORD( lParam ) == SW_MINIMIZE ) ) { |
194 |
|
|
MessageBox( 0, "Minimizing windows is not allowed in this version. Sorry!", "SeamlessRDP", MB_OK ); |
195 |
|
|
return 1; |
196 |
|
|
} |
197 |
astrand |
993 |
|
198 |
astrand |
1002 |
GetWindowText( ( HWND ) wParam, windowTitle, 150 ); |
199 |
astrand |
993 |
|
200 |
astrand |
1002 |
snprintf( result, sizeof( result ), |
201 |
|
|
"SETSTATE1,0x%p,%s,0x%x,0x%x\n", |
202 |
|
|
( HWND ) wParam, |
203 |
|
|
windowTitle, |
204 |
|
|
LOWORD( lParam ), |
205 |
|
|
0 ); |
206 |
astrand |
1005 |
result[ sizeof( result ) - 1 ] = '\0'; |
207 |
|
|
WriteToChannel( result ); |
208 |
astrand |
933 |
break; |
209 |
astrand |
993 |
|
210 |
astrand |
1002 |
|
211 |
astrand |
993 |
default: |
212 |
astrand |
933 |
break; |
213 |
|
|
} |
214 |
astrand |
993 |
|
215 |
|
|
|
216 |
astrand |
1005 |
|
217 |
astrand |
993 |
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
218 |
astrand |
930 |
} |
219 |
|
|
|
220 |
|
|
|
221 |
astrand |
993 |
LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam ) |
222 |
astrand |
930 |
{ |
223 |
astrand |
993 |
if ( nCode < 0 ) { |
224 |
|
|
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
225 |
astrand |
933 |
} |
226 |
astrand |
993 |
|
227 |
astrand |
1004 |
char windowTitle[ 150 ] = { "" |
228 |
|
|
}; |
229 |
|
|
HWND windowHandle = NULL; |
230 |
|
|
char result[ 255 ] = { "" |
231 |
|
|
}; |
232 |
|
|
char strWindowId[ 25 ]; |
233 |
|
|
LONG b, t, l, r; |
234 |
|
|
char strW[ 5 ]; |
235 |
|
|
char strY[ 5 ]; |
236 |
|
|
char strX[ 5 ]; |
237 |
|
|
char strH[ 5 ]; |
238 |
|
|
RECT rect; |
239 |
|
|
|
240 |
|
|
switch ( nCode ) { |
241 |
|
|
case HSHELL_WINDOWCREATED: |
242 |
astrand |
993 |
|
243 |
astrand |
1004 |
//get window id |
244 |
|
|
windowHandle = ( HWND ) wParam; |
245 |
|
|
itoa( ( int ) windowHandle, strWindowId, 10 ); |
246 |
astrand |
993 |
|
247 |
astrand |
1004 |
//get coords |
248 |
|
|
GetWindowRect( windowHandle, &rect ); |
249 |
|
|
b = rect.bottom; |
250 |
|
|
t = rect.top; |
251 |
|
|
l = rect.left; |
252 |
|
|
r = rect.right; |
253 |
|
|
ltoa( b - t, strH, 10 ); |
254 |
|
|
ltoa( t, strY, 10 ); |
255 |
|
|
ltoa( r - l, strW, 10 ); |
256 |
|
|
ltoa( l, strX, 10 ); |
257 |
|
|
|
258 |
|
|
//get name |
259 |
|
|
GetWindowText( windowHandle, windowTitle, 150 ); |
260 |
|
|
|
261 |
|
|
////setup return string |
262 |
|
|
strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" ); |
263 |
|
|
strcat( result, "ID=" ); |
264 |
|
|
strcat( result, strWindowId ); |
265 |
|
|
strcat( result, ";" ); |
266 |
|
|
strcat( result, "TITLE=" ); |
267 |
|
|
strcat( result, windowTitle ); |
268 |
|
|
strcat( result, ";" ); |
269 |
|
|
strcat( result, "X=" ); |
270 |
|
|
strcat( result, strX ); |
271 |
|
|
strcat( result, ";" ); |
272 |
|
|
strcat( result, "Y=" ); |
273 |
|
|
strcat( result, strY ); |
274 |
|
|
strcat( result, ";" ); |
275 |
|
|
strcat( result, "H=" ); |
276 |
|
|
strcat( result, strH ); |
277 |
|
|
strcat( result, ";" ); |
278 |
|
|
strcat( result, "W=" ); |
279 |
|
|
strcat( result, strW ); |
280 |
|
|
strcat( result, "." ); |
281 |
astrand |
1005 |
WriteToChannel( result ); |
282 |
astrand |
1004 |
break; |
283 |
|
|
|
284 |
|
|
case HSHELL_WINDOWDESTROYED: |
285 |
|
|
|
286 |
|
|
//get window id |
287 |
|
|
windowHandle = ( HWND ) wParam; |
288 |
|
|
itoa( ( int ) windowHandle, strWindowId, 10 ); |
289 |
|
|
|
290 |
|
|
//get coords |
291 |
|
|
GetWindowRect( windowHandle, &rect ); |
292 |
|
|
b = rect.bottom; |
293 |
|
|
t = rect.top; |
294 |
|
|
l = rect.left; |
295 |
|
|
r = rect.right; |
296 |
|
|
ltoa( b - t, strH, 10 ); |
297 |
|
|
ltoa( t, strY, 10 ); |
298 |
|
|
ltoa( r - l, strW, 10 ); |
299 |
|
|
ltoa( l, strX, 10 ); |
300 |
|
|
|
301 |
|
|
//get name |
302 |
|
|
GetWindowText( windowHandle, windowTitle, 150 ); |
303 |
|
|
|
304 |
|
|
////setup return string |
305 |
|
|
strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" ); |
306 |
|
|
strcat( result, "ID=" ); |
307 |
|
|
strcat( result, strWindowId ); |
308 |
|
|
strcat( result, ";" ); |
309 |
|
|
strcat( result, "TITLE=" ); |
310 |
|
|
strcat( result, windowTitle ); |
311 |
|
|
strcat( result, ";" ); |
312 |
|
|
strcat( result, "X=" ); |
313 |
|
|
strcat( result, strX ); |
314 |
|
|
strcat( result, ";" ); |
315 |
|
|
strcat( result, "Y=" ); |
316 |
|
|
strcat( result, strY ); |
317 |
|
|
strcat( result, ";" ); |
318 |
|
|
strcat( result, "H=" ); |
319 |
|
|
strcat( result, strH ); |
320 |
|
|
strcat( result, ";" ); |
321 |
|
|
strcat( result, "W=" ); |
322 |
|
|
strcat( result, strW ); |
323 |
|
|
strcat( result, "." ); |
324 |
astrand |
1005 |
WriteToChannel( result ); |
325 |
|
|
break; |
326 |
astrand |
1004 |
|
327 |
astrand |
1005 |
|
328 |
astrand |
1004 |
default: |
329 |
|
|
break; |
330 |
astrand |
933 |
} |
331 |
astrand |
993 |
|
332 |
astrand |
1004 |
|
333 |
astrand |
993 |
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
334 |
astrand |
930 |
} |
335 |
|
|
|
336 |
astrand |
993 |
DLL_EXPORT void SetCbtHook( void ) |
337 |
astrand |
930 |
{ |
338 |
astrand |
993 |
if ( !bHooked ) { |
339 |
astrand |
994 |
hhook = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL ); |
340 |
astrand |
933 |
bHooked = true; |
341 |
|
|
} |
342 |
astrand |
993 |
|
343 |
|
|
if ( !bHooked2 ) { |
344 |
astrand |
994 |
hhook2 = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL ); |
345 |
astrand |
933 |
bHooked2 = true; |
346 |
|
|
} |
347 |
astrand |
993 |
|
348 |
|
|
if ( !bHooked3 ) { |
349 |
astrand |
994 |
hhook3 = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL ); |
350 |
astrand |
933 |
bHooked3 = true; |
351 |
|
|
} |
352 |
astrand |
930 |
} |
353 |
|
|
|
354 |
astrand |
993 |
DLL_EXPORT void RemoveCbtHook( void ) |
355 |
astrand |
930 |
{ |
356 |
astrand |
993 |
if ( bHooked ) { |
357 |
|
|
UnhookWindowsHookEx( hhook ); |
358 |
astrand |
933 |
bHooked = false; |
359 |
|
|
} |
360 |
astrand |
993 |
|
361 |
|
|
if ( bHooked2 ) { |
362 |
|
|
UnhookWindowsHookEx( hhook2 ); |
363 |
astrand |
933 |
bHooked2 = false; |
364 |
|
|
} |
365 |
astrand |
993 |
|
366 |
|
|
if ( bHooked3 ) { |
367 |
|
|
UnhookWindowsHookEx( hhook3 ); |
368 |
astrand |
933 |
bHooked3 = false; |
369 |
|
|
} |
370 |
astrand |
930 |
} |
371 |
|
|
|
372 |
|
|
DLL_EXPORT int GetInstanceCount() |
373 |
|
|
{ |
374 |
astrand |
933 |
return iInstanceCount; |
375 |
astrand |
930 |
} |
376 |
|
|
|
377 |
|
|
int OpenVirtualChannel() |
378 |
|
|
{ |
379 |
astrand |
994 |
m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME ); |
380 |
|
|
|
381 |
astrand |
993 |
if ( m_vcHandle == NULL ) { |
382 |
astrand |
933 |
return 0; |
383 |
astrand |
937 |
} else { |
384 |
astrand |
933 |
return 1; |
385 |
|
|
} |
386 |
astrand |
930 |
} |
387 |
|
|
|
388 |
|
|
int CloseVirtualChannel() |
389 |
|
|
{ |
390 |
astrand |
993 |
BOOL result = WTSVirtualChannelClose( m_vcHandle ); |
391 |
|
|
|
392 |
astrand |
933 |
m_vcHandle = NULL; |
393 |
astrand |
993 |
|
394 |
|
|
if ( result ) { |
395 |
astrand |
933 |
return 1; |
396 |
astrand |
937 |
} else { |
397 |
astrand |
933 |
return 0; |
398 |
|
|
} |
399 |
astrand |
930 |
} |
400 |
|
|
|
401 |
|
|
int ChannelIsOpen() |
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 |
astrand |
993 |
int WriteToChannel( PCHAR buffer ) |
411 |
astrand |
930 |
{ |
412 |
astrand |
933 |
PULONG bytesRead = 0; |
413 |
|
|
PULONG pBytesWritten = 0; |
414 |
astrand |
993 |
|
415 |
astrand |
1004 |
if ( !ChannelIsOpen() ) |
416 |
|
|
return 1; |
417 |
|
|
|
418 |
astrand |
994 |
BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten ); |
419 |
|
|
|
420 |
astrand |
993 |
if ( result ) { |
421 |
astrand |
933 |
return 1; |
422 |
astrand |
937 |
} else { |
423 |
astrand |
933 |
return 0; |
424 |
|
|
} |
425 |
astrand |
930 |
} |