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 |
|
|
|
9 |
|
|
#include "wtsapi32.h" |
10 |
|
|
#include "Cchannel.h" |
11 |
|
|
|
12 |
astrand |
993 |
#define DLL_EXPORT extern "C" __declspec(dllexport) |
13 |
astrand |
930 |
|
14 |
|
|
// Shared DATA |
15 |
|
|
#pragma data_seg ( "SHAREDDATA" ) |
16 |
|
|
|
17 |
astrand |
937 |
// this is the total number of processes this dll is currently attached to |
18 |
astrand |
933 |
int iInstanceCount = 0; |
19 |
|
|
HWND hWnd = 0; |
20 |
astrand |
930 |
|
21 |
|
|
#pragma data_seg () |
22 |
|
|
|
23 |
|
|
#pragma comment(linker, "/section:SHAREDDATA,rws") |
24 |
|
|
|
25 |
astrand |
933 |
bool bHooked = false; |
26 |
|
|
bool bHooked2 = false; |
27 |
|
|
bool bHooked3 = false; |
28 |
astrand |
994 |
HHOOK hhook = 0; //cbt |
29 |
|
|
HHOOK hhook2 = 0; //shell |
30 |
|
|
HHOOK hhook3 = 0; //wnd proc |
31 |
astrand |
933 |
HINSTANCE hInst = 0; |
32 |
|
|
HANDLE m_vcHandle = 0; |
33 |
astrand |
930 |
|
34 |
astrand |
994 |
BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved ) |
35 |
astrand |
930 |
{ |
36 |
astrand |
993 |
switch ( ul_reason_for_call ) { |
37 |
|
|
case DLL_PROCESS_ATTACH: { |
38 |
astrand |
933 |
// remember our instance handle |
39 |
|
|
hInst = hinstDLL; |
40 |
|
|
++iInstanceCount; |
41 |
|
|
OpenVirtualChannel(); |
42 |
|
|
break; |
43 |
|
|
} |
44 |
astrand |
993 |
|
45 |
|
|
case DLL_THREAD_ATTACH: |
46 |
astrand |
933 |
break; |
47 |
astrand |
993 |
case DLL_THREAD_DETACH: |
48 |
astrand |
933 |
break; |
49 |
astrand |
993 |
case DLL_PROCESS_DETACH: { |
50 |
astrand |
933 |
--iInstanceCount; |
51 |
|
|
CloseVirtualChannel(); |
52 |
|
|
} |
53 |
|
|
break; |
54 |
astrand |
930 |
} |
55 |
astrand |
993 |
|
56 |
astrand |
930 |
return TRUE; |
57 |
|
|
} |
58 |
|
|
|
59 |
astrand |
993 |
LRESULT CALLBACK CallWndProc( int nCode, WPARAM wParam, LPARAM lParam ) |
60 |
astrand |
930 |
{ |
61 |
astrand |
993 |
if ( nCode < 0 ) { |
62 |
|
|
return CallNextHookEx( hhook3, nCode, wParam, lParam ); |
63 |
astrand |
933 |
} |
64 |
astrand |
993 |
|
65 |
astrand |
933 |
PCHAR buffer = NULL; |
66 |
astrand |
993 |
char windowTitle[ 150 ] = { "" |
67 |
|
|
}; |
68 |
astrand |
933 |
HWND windowHandle = NULL; |
69 |
astrand |
994 |
HWND windowHandle2 = NULL; |
70 |
astrand |
993 |
char result[ 255 ] = { "" |
71 |
|
|
}; |
72 |
|
|
char strWindowId[ 25 ]; |
73 |
|
|
char type[ 25 ]; |
74 |
|
|
|
75 |
astrand |
933 |
LONG b, t, l, r; |
76 |
astrand |
994 |
char strX[ 5 ]; |
77 |
|
|
char strY[ 5 ]; |
78 |
|
|
char strW[ 5 ]; |
79 |
|
|
char strH[ 5 ]; |
80 |
astrand |
933 |
RECT rect; |
81 |
astrand |
993 |
|
82 |
|
|
CWPSTRUCT *details = ( CWPSTRUCT * ) lParam; |
83 |
|
|
|
84 |
|
|
switch ( details->message ) { |
85 |
|
|
case WM_SIZING: |
86 |
astrand |
994 |
windowHandle = details->hwnd; |
87 |
|
|
//get win name |
88 |
|
|
GetWindowText( windowHandle, windowTitle, 150 ); |
89 |
|
|
|
90 |
|
|
//get an id for it |
91 |
|
|
itoa( ( int ) windowHandle, strWindowId, 10 ); |
92 |
|
|
|
93 |
|
|
//get coords |
94 |
|
|
GetWindowRect( windowHandle, &rect ); |
95 |
|
|
b = rect.bottom; |
96 |
|
|
t = rect.top; |
97 |
|
|
l = rect.left; |
98 |
|
|
r = rect.right; |
99 |
|
|
ltoa( b - t, strH, 10 ); |
100 |
|
|
ltoa( t, strY, 10 ); |
101 |
|
|
ltoa( r - l, strW, 10 ); |
102 |
|
|
ltoa( l, strX, 10 ); |
103 |
|
|
|
104 |
|
|
////setup return string |
105 |
|
|
strcat( result, "MSG=CALLWNDPROC_WM_SIZING;OP=6;" ); |
106 |
|
|
strcat( result, "ID=" ); |
107 |
|
|
strcat( result, strWindowId ); |
108 |
|
|
strcat( result, ";" ); |
109 |
|
|
strcat( result, "TITLE=" ); |
110 |
|
|
strcat( result, windowTitle ); |
111 |
|
|
strcat( result, ";" ); |
112 |
|
|
strcat( result, "X=" ); |
113 |
|
|
strcat( result, strX ); |
114 |
|
|
strcat( result, ";" ); |
115 |
|
|
strcat( result, "Y=" ); |
116 |
|
|
strcat( result, strY ); |
117 |
|
|
strcat( result, ";" ); |
118 |
|
|
strcat( result, "H=" ); |
119 |
|
|
strcat( result, strH ); |
120 |
|
|
strcat( result, ";" ); |
121 |
|
|
strcat( result, "W=" ); |
122 |
|
|
strcat( result, strW ); |
123 |
|
|
strcat( result, "." ); |
124 |
|
|
|
125 |
|
|
buffer = result; |
126 |
|
|
|
127 |
|
|
break; |
128 |
astrand |
993 |
case WM_MOVING: |
129 |
|
|
|
130 |
astrand |
933 |
windowHandle = details->hwnd; |
131 |
|
|
//get win name |
132 |
astrand |
993 |
GetWindowText( windowHandle, windowTitle, 150 ); |
133 |
|
|
|
134 |
astrand |
933 |
//get an id for it |
135 |
astrand |
993 |
itoa( ( int ) windowHandle, strWindowId, 10 ); |
136 |
|
|
|
137 |
astrand |
933 |
//get coords |
138 |
astrand |
993 |
GetWindowRect( windowHandle, &rect ); |
139 |
astrand |
933 |
b = rect.bottom; |
140 |
|
|
t = rect.top; |
141 |
|
|
l = rect.left; |
142 |
|
|
r = rect.right; |
143 |
astrand |
994 |
ltoa( b - t, strH, 10 ); |
144 |
|
|
ltoa( t, strY, 10 ); |
145 |
|
|
ltoa( r - l, strW, 10 ); |
146 |
|
|
ltoa( l, strX, 10 ); |
147 |
astrand |
993 |
|
148 |
astrand |
933 |
////setup return string |
149 |
astrand |
994 |
strcat( result, "MSG=CALLWNDPROC_WM_MOVING;OP=2;" ); |
150 |
astrand |
993 |
strcat( result, "ID=" ); |
151 |
|
|
strcat( result, strWindowId ); |
152 |
|
|
strcat( result, ";" ); |
153 |
|
|
strcat( result, "TITLE=" ); |
154 |
|
|
strcat( result, windowTitle ); |
155 |
|
|
strcat( result, ";" ); |
156 |
astrand |
994 |
strcat( result, "X=" ); |
157 |
|
|
strcat( result, strX ); |
158 |
astrand |
993 |
strcat( result, ";" ); |
159 |
astrand |
994 |
strcat( result, "Y=" ); |
160 |
|
|
strcat( result, strY ); |
161 |
|
|
strcat( result, ";" ); |
162 |
|
|
strcat( result, "H=" ); |
163 |
|
|
strcat( result, strH ); |
164 |
|
|
strcat( result, ";" ); |
165 |
|
|
strcat( result, "W=" ); |
166 |
|
|
strcat( result, strW ); |
167 |
|
|
strcat( result, "." ); |
168 |
astrand |
993 |
|
169 |
astrand |
933 |
buffer = result; |
170 |
astrand |
993 |
|
171 |
astrand |
933 |
break; |
172 |
astrand |
993 |
|
173 |
astrand |
994 |
|
174 |
|
|
case WM_WINDOWPOSCHANGING: |
175 |
|
|
|
176 |
|
|
windowHandle = details->hwnd; |
177 |
|
|
//windowHandle2 = details->hwndInsertAfter; |
178 |
|
|
//get win name |
179 |
|
|
GetWindowText( windowHandle, windowTitle, 150 ); |
180 |
|
|
|
181 |
|
|
//get an id for it |
182 |
|
|
itoa( ( int ) windowHandle, strWindowId, 10 ); |
183 |
|
|
|
184 |
|
|
//get coords |
185 |
|
|
GetWindowRect( windowHandle, &rect ); |
186 |
|
|
b = rect.bottom; |
187 |
|
|
t = rect.top; |
188 |
|
|
l = rect.left; |
189 |
|
|
r = rect.right; |
190 |
|
|
ltoa( b - t, strH, 10 ); |
191 |
|
|
ltoa( t, strY, 10 ); |
192 |
|
|
ltoa( r - l, strW, 10 ); |
193 |
|
|
ltoa( l, strX, 10 ); |
194 |
|
|
|
195 |
|
|
////setup return string |
196 |
|
|
strcat( result, "MSG=CALLWNDPROC_WM_WINDOWPOSCHANGING;OP=7;" ); |
197 |
|
|
strcat( result, "ID=" ); |
198 |
|
|
strcat( result, strWindowId ); |
199 |
|
|
strcat( result, ";" ); |
200 |
|
|
strcat( result, "TITLE=" ); |
201 |
|
|
strcat( result, windowTitle ); |
202 |
|
|
strcat( result, ";" ); |
203 |
|
|
strcat( result, "X=" ); |
204 |
|
|
strcat( result, strX ); |
205 |
|
|
strcat( result, ";" ); |
206 |
|
|
strcat( result, "Y=" ); |
207 |
|
|
strcat( result, strY ); |
208 |
|
|
strcat( result, ";" ); |
209 |
|
|
strcat( result, "H=" ); |
210 |
|
|
strcat( result, strH ); |
211 |
|
|
strcat( result, ";" ); |
212 |
|
|
strcat( result, "W=" ); |
213 |
|
|
strcat( result, strW ); |
214 |
|
|
strcat( result, "." ); |
215 |
|
|
|
216 |
|
|
buffer = result; |
217 |
|
|
|
218 |
|
|
break; |
219 |
|
|
case WM_WINDOWPOSCHANGED: |
220 |
|
|
|
221 |
|
|
windowHandle = details->hwnd; |
222 |
|
|
//windowHandle2 = details->hwndInsertAfter; |
223 |
|
|
//get win name |
224 |
|
|
GetWindowText( windowHandle, windowTitle, 150 ); |
225 |
|
|
|
226 |
|
|
//get an id for it |
227 |
|
|
itoa( ( int ) windowHandle, strWindowId, 10 ); |
228 |
|
|
|
229 |
|
|
//get coords |
230 |
|
|
GetWindowRect( windowHandle, &rect ); |
231 |
|
|
b = rect.bottom; |
232 |
|
|
t = rect.top; |
233 |
|
|
l = rect.left; |
234 |
|
|
r = rect.right; |
235 |
|
|
ltoa( b - t, strH, 10 ); |
236 |
|
|
ltoa( t, strY, 10 ); |
237 |
|
|
ltoa( r - l, strW, 10 ); |
238 |
|
|
ltoa( l, strX, 10 ); |
239 |
|
|
|
240 |
|
|
////setup return string |
241 |
|
|
strcat( result, "MSG=CALLWNDPROC_WM_WINDOWPOSCHANGED;OP=8;" ); |
242 |
|
|
strcat( result, "ID=" ); |
243 |
|
|
strcat( result, strWindowId ); |
244 |
|
|
strcat( result, ";" ); |
245 |
|
|
strcat( result, "TITLE=" ); |
246 |
|
|
strcat( result, windowTitle ); |
247 |
|
|
strcat( result, ";" ); |
248 |
|
|
strcat( result, "X=" ); |
249 |
|
|
strcat( result, strX ); |
250 |
|
|
strcat( result, ";" ); |
251 |
|
|
strcat( result, "Y=" ); |
252 |
|
|
strcat( result, strY ); |
253 |
|
|
strcat( result, ";" ); |
254 |
|
|
strcat( result, "H=" ); |
255 |
|
|
strcat( result, strH ); |
256 |
|
|
strcat( result, ";" ); |
257 |
|
|
strcat( result, "W=" ); |
258 |
|
|
strcat( result, strW ); |
259 |
|
|
strcat( result, "." ); |
260 |
|
|
|
261 |
|
|
buffer = result; |
262 |
|
|
|
263 |
|
|
break; |
264 |
|
|
|
265 |
astrand |
993 |
default: |
266 |
astrand |
933 |
break; |
267 |
|
|
} |
268 |
astrand |
993 |
|
269 |
|
|
if ( ChannelIsOpen() ) { |
270 |
|
|
if ( buffer != NULL ) { |
271 |
|
|
WriteToChannel( buffer ); |
272 |
astrand |
933 |
} |
273 |
|
|
} |
274 |
astrand |
993 |
|
275 |
|
|
return CallNextHookEx( hhook3, nCode, wParam, lParam ); |
276 |
astrand |
930 |
} |
277 |
|
|
|
278 |
astrand |
993 |
LRESULT CALLBACK CbtProc( int nCode, WPARAM wParam, LPARAM lParam ) |
279 |
astrand |
930 |
{ |
280 |
astrand |
993 |
if ( nCode < 0 ) { |
281 |
|
|
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
282 |
astrand |
933 |
} |
283 |
astrand |
993 |
|
284 |
|
|
|
285 |
astrand |
933 |
PCHAR buffer = NULL; |
286 |
astrand |
993 |
|
287 |
|
|
|
288 |
|
|
char windowTitle[ 150 ] = { "" |
289 |
|
|
}; |
290 |
astrand |
933 |
HWND windowHandle = NULL; |
291 |
astrand |
993 |
char result[ 255 ] = { "" |
292 |
|
|
}; |
293 |
|
|
char strWindowId[ 25 ]; |
294 |
|
|
char type[ 25 ]; |
295 |
|
|
|
296 |
|
|
|
297 |
astrand |
933 |
LONG b, t, l, r; |
298 |
astrand |
994 |
char strW[ 5 ]; |
299 |
|
|
char strY[ 5 ]; |
300 |
|
|
char strX[ 5 ]; |
301 |
|
|
char strH[ 5 ]; |
302 |
astrand |
933 |
RECT rect; |
303 |
astrand |
993 |
|
304 |
astrand |
994 |
int i = 0; //tmp |
305 |
astrand |
993 |
|
306 |
|
|
switch ( nCode ) { |
307 |
|
|
case HCBT_MINMAX: |
308 |
|
|
|
309 |
|
|
windowHandle = ( HWND ) wParam; |
310 |
astrand |
933 |
//get win name |
311 |
astrand |
993 |
GetWindowText( windowHandle, windowTitle, 150 ); |
312 |
|
|
|
313 |
astrand |
933 |
//get an id for it |
314 |
astrand |
993 |
itoa( ( int ) windowHandle, strWindowId, 10 ); |
315 |
|
|
|
316 |
astrand |
933 |
//get operation type(min,max). if max, do not clip at all,if min use window's previous coords |
317 |
|
|
//codes are: |
318 |
astrand |
993 |
|
319 |
astrand |
937 |
// SW_HIDE= 0 SW_SHOWNORMAL=1 SW_NORMAL=1 SW_SHOWMINIMIZED=2 SW_SHOWMAXIMIZED=3 SW_MAXIMIZE=3 |
320 |
|
|
// SW_SHOWNOACTIVATE=4 SW_SHOW=5 SW_MINIMIZE=6 SW_SHOWMINNOACTIVE=7 SW_SHOWNA=8 SW_RESTORE=9 |
321 |
|
|
// SW_SHOWDEFAULT=10 SW_FORCEMINIMIZE=11 SW_MAX=11 |
322 |
astrand |
993 |
|
323 |
|
|
itoa( ( int ) lParam, type, 10 ); |
324 |
|
|
|
325 |
astrand |
933 |
//get coords |
326 |
astrand |
993 |
GetWindowRect( windowHandle, &rect ); |
327 |
astrand |
933 |
b = rect.bottom; |
328 |
|
|
t = rect.top; |
329 |
|
|
l = rect.left; |
330 |
|
|
r = rect.right; |
331 |
astrand |
994 |
ltoa( b - t, strW, 10 ); |
332 |
|
|
ltoa( t, strY, 10 ); |
333 |
|
|
ltoa( r - l, strH, 10 ); |
334 |
|
|
ltoa( l, strX, 10 ); |
335 |
astrand |
993 |
|
336 |
astrand |
933 |
//get name |
337 |
astrand |
993 |
GetWindowText( windowHandle, windowTitle, 150 ); |
338 |
|
|
|
339 |
astrand |
933 |
////setup return string |
340 |
astrand |
994 |
strcat( result, "MSG=HCBT_MINMAX;OP=4;" ); |
341 |
|
|
|
342 |
|
|
// SW_SHOWNOACTIVATE=4 SW_SHOW=5 SW_MINIMIZE=6 SW_SHOWMINNOACTIVE=7 SW_SHOWNA=8 SW_RESTORE=9 |
343 |
|
|
// SW_SHOWDEFAULT=10 SW_FORCEMINIMIZE=11 SW_MAX=11 |
344 |
astrand |
993 |
strcat( result, "ID=" ); |
345 |
|
|
strcat( result, strWindowId ); |
346 |
|
|
strcat( result, ";" ); |
347 |
|
|
strcat( result, "TITLE=" ); |
348 |
|
|
strcat( result, windowTitle ); |
349 |
|
|
strcat( result, ";" ); |
350 |
astrand |
994 |
strcat( result, "X=" ); |
351 |
|
|
strcat( result, strX ); |
352 |
astrand |
993 |
strcat( result, ";" ); |
353 |
astrand |
994 |
strcat( result, "Y=" ); |
354 |
|
|
strcat( result, strY ); |
355 |
|
|
strcat( result, ";" ); |
356 |
|
|
strcat( result, "H=" ); |
357 |
|
|
strcat( result, strH ); |
358 |
|
|
strcat( result, ";" ); |
359 |
|
|
strcat( result, "W=" ); |
360 |
|
|
strcat( result, strW ); |
361 |
|
|
strcat( result, ";" ); |
362 |
astrand |
993 |
strcat( result, "TYPE=" ); |
363 |
|
|
strcat( result, type ); |
364 |
astrand |
994 |
strcat( result, "." ); |
365 |
astrand |
993 |
|
366 |
astrand |
933 |
//------------------------------------------------------------------------------------------------- |
367 |
|
|
// code to prevent minimising windows (can be removed once minimise has been implemented) |
368 |
astrand |
994 |
//i = (int)lParam; |
369 |
astrand |
933 |
//if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11) |
370 |
astrand |
994 |
//if ( i==2 || i==6 ) |
371 |
|
|
//{ |
372 |
|
|
// MessageBox(0,"Minimising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK); |
373 |
|
|
// return 1; |
374 |
|
|
//} |
375 |
astrand |
933 |
//----------------------------------------------------------------------------------------- |
376 |
astrand |
993 |
|
377 |
astrand |
933 |
//------------------------------------------------------------------------------------------------- |
378 |
|
|
// code to prevent maximising windows (can be removed once maximise has been implemented) |
379 |
astrand |
994 |
//i = (int)lParam; |
380 |
astrand |
933 |
//if (i==3 || i==9 || i==11) |
381 |
astrand |
994 |
//if (i==3 || i==11) |
382 |
|
|
//{ |
383 |
|
|
// MessageBox(0,"Maximising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK); |
384 |
|
|
// return 1; |
385 |
|
|
//} |
386 |
astrand |
933 |
//----------------------------------------------------------------------------------------- |
387 |
astrand |
993 |
|
388 |
astrand |
933 |
buffer = result; |
389 |
astrand |
993 |
|
390 |
astrand |
933 |
break; |
391 |
astrand |
993 |
|
392 |
|
|
case HCBT_MOVESIZE: |
393 |
|
|
|
394 |
|
|
windowHandle = ( HWND ) wParam; |
395 |
astrand |
933 |
//get win name |
396 |
astrand |
993 |
GetWindowText( windowHandle, windowTitle, 150 ); |
397 |
|
|
|
398 |
astrand |
933 |
//get an id for it |
399 |
astrand |
993 |
itoa( ( int ) windowHandle, strWindowId, 10 ); |
400 |
|
|
|
401 |
astrand |
933 |
//get coords |
402 |
astrand |
993 |
GetWindowRect( windowHandle, &rect ); |
403 |
astrand |
933 |
b = rect.bottom; |
404 |
|
|
t = rect.top; |
405 |
|
|
l = rect.left; |
406 |
|
|
r = rect.right; |
407 |
astrand |
994 |
ltoa( b - t, strH, 10 ); |
408 |
|
|
ltoa( t, strY, 10 ); |
409 |
|
|
ltoa( r - l, strW, 10 ); |
410 |
|
|
ltoa( l, strX, 10 ); |
411 |
astrand |
993 |
|
412 |
astrand |
933 |
//get name |
413 |
astrand |
993 |
GetWindowText( windowHandle, windowTitle, 150 ); |
414 |
|
|
|
415 |
astrand |
933 |
////setup return string |
416 |
astrand |
994 |
strcat( result, "MSG=HCBT_MOVESIZE;OP=5;" ); |
417 |
astrand |
993 |
strcat( result, "ID=" ); |
418 |
|
|
strcat( result, strWindowId ); |
419 |
|
|
strcat( result, ";" ); |
420 |
|
|
strcat( result, "TITLE=" ); |
421 |
|
|
strcat( result, windowTitle ); |
422 |
|
|
strcat( result, ";" ); |
423 |
astrand |
994 |
strcat( result, "X=" ); |
424 |
|
|
strcat( result, strX ); |
425 |
astrand |
993 |
strcat( result, ";" ); |
426 |
astrand |
994 |
strcat( result, "Y=" ); |
427 |
|
|
strcat( result, strY ); |
428 |
|
|
strcat( result, ";" ); |
429 |
|
|
strcat( result, "H=" ); |
430 |
|
|
strcat( result, strH ); |
431 |
|
|
strcat( result, ";" ); |
432 |
|
|
strcat( result, "W=" ); |
433 |
|
|
strcat( result, strW ); |
434 |
|
|
strcat( result, "." ); |
435 |
astrand |
993 |
|
436 |
astrand |
933 |
buffer = result; |
437 |
astrand |
993 |
|
438 |
astrand |
933 |
break; |
439 |
astrand |
993 |
case HCBT_SETFOCUS: |
440 |
astrand |
933 |
//buffer = "HCBT_SETFOCUS"; |
441 |
|
|
//not needed yet |
442 |
|
|
break; |
443 |
astrand |
993 |
default: |
444 |
astrand |
933 |
break; |
445 |
|
|
} |
446 |
astrand |
993 |
|
447 |
|
|
if ( ChannelIsOpen() ) { |
448 |
|
|
if ( buffer != NULL ) { |
449 |
|
|
WriteToChannel( buffer ); |
450 |
astrand |
933 |
} |
451 |
|
|
} |
452 |
astrand |
993 |
|
453 |
|
|
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
454 |
astrand |
930 |
} |
455 |
|
|
|
456 |
|
|
|
457 |
astrand |
993 |
LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam ) |
458 |
astrand |
930 |
{ |
459 |
astrand |
993 |
if ( nCode < 0 ) { |
460 |
|
|
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
461 |
astrand |
933 |
} |
462 |
astrand |
993 |
|
463 |
|
|
if ( ChannelIsOpen() ) { |
464 |
astrand |
933 |
PCHAR buffer = NULL; |
465 |
astrand |
993 |
|
466 |
|
|
char windowTitle[ 150 ] = { "" |
467 |
|
|
}; |
468 |
astrand |
933 |
HWND windowHandle = NULL; |
469 |
astrand |
993 |
char result[ 255 ] = { "" |
470 |
|
|
}; |
471 |
|
|
char strWindowId[ 25 ]; |
472 |
astrand |
933 |
LONG b, t, l, r; |
473 |
astrand |
994 |
char strW[ 5 ]; |
474 |
|
|
char strY[ 5 ]; |
475 |
|
|
char strX[ 5 ]; |
476 |
|
|
char strH[ 5 ]; |
477 |
astrand |
933 |
RECT rect; |
478 |
astrand |
993 |
|
479 |
|
|
switch ( nCode ) { |
480 |
|
|
case HSHELL_WINDOWCREATED: |
481 |
|
|
|
482 |
astrand |
933 |
//get window id |
483 |
astrand |
993 |
windowHandle = ( HWND ) wParam; |
484 |
|
|
itoa( ( int ) windowHandle, strWindowId, 10 ); |
485 |
|
|
|
486 |
astrand |
933 |
//get coords |
487 |
astrand |
993 |
GetWindowRect( windowHandle, &rect ); |
488 |
astrand |
933 |
b = rect.bottom; |
489 |
|
|
t = rect.top; |
490 |
|
|
l = rect.left; |
491 |
|
|
r = rect.right; |
492 |
astrand |
994 |
ltoa( b - t, strH, 10 ); |
493 |
|
|
ltoa( t, strY, 10 ); |
494 |
|
|
ltoa( r - l, strW, 10 ); |
495 |
|
|
ltoa( l, strX, 10 ); |
496 |
astrand |
993 |
|
497 |
astrand |
933 |
//get name |
498 |
astrand |
993 |
GetWindowText( windowHandle, windowTitle, 150 ); |
499 |
|
|
|
500 |
astrand |
933 |
////setup return string |
501 |
astrand |
994 |
strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" ); |
502 |
astrand |
993 |
strcat( result, "ID=" ); |
503 |
|
|
strcat( result, strWindowId ); |
504 |
|
|
strcat( result, ";" ); |
505 |
|
|
strcat( result, "TITLE=" ); |
506 |
|
|
strcat( result, windowTitle ); |
507 |
|
|
strcat( result, ";" ); |
508 |
astrand |
994 |
strcat( result, "X=" ); |
509 |
|
|
strcat( result, strX ); |
510 |
astrand |
993 |
strcat( result, ";" ); |
511 |
astrand |
994 |
strcat( result, "Y=" ); |
512 |
|
|
strcat( result, strY ); |
513 |
|
|
strcat( result, ";" ); |
514 |
|
|
strcat( result, "H=" ); |
515 |
|
|
strcat( result, strH ); |
516 |
|
|
strcat( result, ";" ); |
517 |
|
|
strcat( result, "W=" ); |
518 |
|
|
strcat( result, strW ); |
519 |
|
|
strcat( result, "." ); |
520 |
astrand |
993 |
|
521 |
astrand |
933 |
buffer = result; |
522 |
astrand |
993 |
|
523 |
astrand |
933 |
break; |
524 |
astrand |
993 |
|
525 |
|
|
case HSHELL_WINDOWDESTROYED: |
526 |
|
|
|
527 |
astrand |
933 |
//get window id |
528 |
astrand |
993 |
windowHandle = ( HWND ) wParam; |
529 |
|
|
itoa( ( int ) windowHandle, strWindowId, 10 ); |
530 |
|
|
|
531 |
astrand |
994 |
//get coords |
532 |
|
|
GetWindowRect( windowHandle, &rect ); |
533 |
|
|
b = rect.bottom; |
534 |
|
|
t = rect.top; |
535 |
|
|
l = rect.left; |
536 |
|
|
r = rect.right; |
537 |
|
|
ltoa( b - t, strH, 10 ); |
538 |
|
|
ltoa( t, strY, 10 ); |
539 |
|
|
ltoa( r - l, strW, 10 ); |
540 |
|
|
ltoa( l, strX, 10 ); |
541 |
|
|
|
542 |
astrand |
933 |
//get name |
543 |
astrand |
993 |
GetWindowText( windowHandle, windowTitle, 150 ); |
544 |
|
|
|
545 |
astrand |
933 |
////setup return string |
546 |
astrand |
994 |
strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" ); |
547 |
astrand |
993 |
strcat( result, "ID=" ); |
548 |
|
|
strcat( result, strWindowId ); |
549 |
|
|
strcat( result, ";" ); |
550 |
|
|
strcat( result, "TITLE=" ); |
551 |
|
|
strcat( result, windowTitle ); |
552 |
|
|
strcat( result, ";" ); |
553 |
astrand |
994 |
strcat( result, "X=" ); |
554 |
|
|
strcat( result, strX ); |
555 |
|
|
strcat( result, ";" ); |
556 |
|
|
strcat( result, "Y=" ); |
557 |
|
|
strcat( result, strY ); |
558 |
|
|
strcat( result, ";" ); |
559 |
|
|
strcat( result, "H=" ); |
560 |
|
|
strcat( result, strH ); |
561 |
|
|
strcat( result, ";" ); |
562 |
|
|
strcat( result, "W=" ); |
563 |
|
|
strcat( result, strW ); |
564 |
|
|
strcat( result, "." ); |
565 |
astrand |
993 |
|
566 |
astrand |
933 |
buffer = result; |
567 |
astrand |
993 |
|
568 |
astrand |
933 |
break; |
569 |
astrand |
993 |
default: |
570 |
astrand |
933 |
break; |
571 |
|
|
} |
572 |
astrand |
993 |
|
573 |
|
|
if ( buffer != NULL ) { |
574 |
|
|
WriteToChannel( buffer ); |
575 |
astrand |
933 |
} |
576 |
|
|
} |
577 |
astrand |
993 |
|
578 |
|
|
return CallNextHookEx( hhook, nCode, wParam, lParam ); |
579 |
astrand |
930 |
} |
580 |
|
|
|
581 |
astrand |
993 |
DLL_EXPORT void SetCbtHook( void ) |
582 |
astrand |
930 |
{ |
583 |
astrand |
993 |
if ( !bHooked ) { |
584 |
astrand |
994 |
hhook = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL ); |
585 |
astrand |
933 |
bHooked = true; |
586 |
|
|
} |
587 |
astrand |
993 |
|
588 |
|
|
if ( !bHooked2 ) { |
589 |
astrand |
994 |
hhook2 = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL ); |
590 |
astrand |
933 |
bHooked2 = true; |
591 |
|
|
} |
592 |
astrand |
993 |
|
593 |
|
|
if ( !bHooked3 ) { |
594 |
astrand |
994 |
hhook3 = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL ); |
595 |
astrand |
933 |
bHooked3 = true; |
596 |
|
|
} |
597 |
astrand |
930 |
} |
598 |
|
|
|
599 |
astrand |
993 |
DLL_EXPORT void RemoveCbtHook( void ) |
600 |
astrand |
930 |
{ |
601 |
astrand |
993 |
if ( bHooked ) { |
602 |
|
|
UnhookWindowsHookEx( hhook ); |
603 |
astrand |
933 |
bHooked = false; |
604 |
|
|
} |
605 |
astrand |
993 |
|
606 |
|
|
if ( bHooked2 ) { |
607 |
|
|
UnhookWindowsHookEx( hhook2 ); |
608 |
astrand |
933 |
bHooked2 = false; |
609 |
|
|
} |
610 |
astrand |
993 |
|
611 |
|
|
if ( bHooked3 ) { |
612 |
|
|
UnhookWindowsHookEx( hhook3 ); |
613 |
astrand |
933 |
bHooked3 = false; |
614 |
|
|
} |
615 |
astrand |
930 |
} |
616 |
|
|
|
617 |
|
|
DLL_EXPORT int GetInstanceCount() |
618 |
|
|
{ |
619 |
astrand |
933 |
return iInstanceCount; |
620 |
astrand |
930 |
} |
621 |
|
|
|
622 |
|
|
int OpenVirtualChannel() |
623 |
|
|
{ |
624 |
astrand |
994 |
m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME ); |
625 |
|
|
|
626 |
astrand |
993 |
if ( m_vcHandle == NULL ) { |
627 |
astrand |
933 |
return 0; |
628 |
astrand |
937 |
} else { |
629 |
astrand |
933 |
return 1; |
630 |
|
|
} |
631 |
astrand |
930 |
} |
632 |
|
|
|
633 |
|
|
int CloseVirtualChannel() |
634 |
|
|
{ |
635 |
astrand |
993 |
BOOL result = WTSVirtualChannelClose( m_vcHandle ); |
636 |
|
|
|
637 |
astrand |
933 |
m_vcHandle = NULL; |
638 |
astrand |
993 |
|
639 |
|
|
if ( result ) { |
640 |
astrand |
933 |
return 1; |
641 |
astrand |
937 |
} else { |
642 |
astrand |
933 |
return 0; |
643 |
|
|
} |
644 |
astrand |
930 |
} |
645 |
|
|
|
646 |
|
|
int ChannelIsOpen() |
647 |
|
|
{ |
648 |
astrand |
993 |
if ( m_vcHandle == NULL ) { |
649 |
astrand |
933 |
return 0; |
650 |
astrand |
937 |
} else { |
651 |
astrand |
933 |
return 1; |
652 |
|
|
} |
653 |
astrand |
930 |
} |
654 |
|
|
|
655 |
astrand |
993 |
int WriteToChannel( PCHAR buffer ) |
656 |
astrand |
930 |
{ |
657 |
astrand |
933 |
PULONG bytesRead = 0; |
658 |
|
|
PULONG pBytesWritten = 0; |
659 |
astrand |
993 |
|
660 |
astrand |
994 |
BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten ); |
661 |
|
|
|
662 |
astrand |
993 |
if ( result ) { |
663 |
astrand |
933 |
return 1; |
664 |
astrand |
937 |
} else { |
665 |
astrand |
933 |
return 0; |
666 |
|
|
} |
667 |
astrand |
930 |
} |