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