27 |
|
|
28 |
#include <windows.h> |
#include <windows.h> |
29 |
#include <winuser.h> |
#include <winuser.h> |
|
#include <wtsapi32.h> |
|
|
#include <cchannel.h> |
|
30 |
|
|
31 |
#include "hookdll.h" |
#include "../vchannel.h" |
32 |
|
|
33 |
#define DLL_EXPORT __declspec(dllexport) |
#define DLL_EXPORT __declspec(dllexport) |
34 |
|
|
49 |
|
|
50 |
static HANDLE g_mutex = NULL; |
static HANDLE g_mutex = NULL; |
51 |
|
|
|
static HANDLE g_vchannel = NULL; |
|
|
|
|
52 |
static void |
static void |
53 |
debug(char *format, ...) |
update_position(HWND hwnd) |
54 |
{ |
{ |
55 |
va_list argp; |
RECT rect; |
|
char buf[256]; |
|
|
|
|
|
sprintf(buf, "DEBUG1,"); |
|
56 |
|
|
57 |
va_start(argp, format); |
if (!GetWindowRect(hwnd, &rect)) |
58 |
_vsnprintf(buf + sizeof("DEBUG1,") - 1, sizeof(buf) - sizeof("DEBUG1,") + 1, format, argp); |
{ |
59 |
va_end(argp); |
debug("GetWindowRect failed!\n"); |
60 |
|
return; |
61 |
|
} |
62 |
|
|
63 |
vchannel_write(buf); |
vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x", |
64 |
|
hwnd, |
65 |
|
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); |
66 |
} |
} |
67 |
|
|
68 |
static LRESULT CALLBACK |
static LRESULT CALLBACK |
69 |
wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) |
wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) |
70 |
{ |
{ |
71 |
HWND hwnd = ((CWPSTRUCT *) details)->hwnd; |
HWND hwnd, parent; |
72 |
UINT msg = ((CWPSTRUCT *) details)->message; |
UINT msg; |
73 |
WPARAM wparam = ((CWPSTRUCT *) details)->wParam; |
WPARAM wparam; |
74 |
LPARAM lparam = ((CWPSTRUCT *) details)->lParam; |
LPARAM lparam; |
75 |
|
|
76 |
LONG style = GetWindowLong(hwnd, GWL_STYLE); |
LONG style; |
|
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
|
|
RECT rect; |
|
77 |
|
|
78 |
if (code < 0) |
if (code < 0) |
79 |
goto end; |
goto end; |
80 |
|
|
81 |
|
hwnd = ((CWPSTRUCT *) details)->hwnd; |
82 |
|
msg = ((CWPSTRUCT *) details)->message; |
83 |
|
wparam = ((CWPSTRUCT *) details)->wParam; |
84 |
|
lparam = ((CWPSTRUCT *) details)->lParam; |
85 |
|
|
86 |
|
style = GetWindowLong(hwnd, GWL_STYLE); |
87 |
|
|
88 |
|
/* Docs say that WS_CHILD and WS_POPUP is an illegal combination, |
89 |
|
but they exist nonetheless. */ |
90 |
|
if ((style & WS_CHILD) && !(style & WS_POPUP)) |
91 |
|
goto end; |
92 |
|
|
93 |
|
if (style & WS_POPUP) |
94 |
|
{ |
95 |
|
parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT); |
96 |
|
if (!parent) |
97 |
|
parent = (HWND) - 1; |
98 |
|
} |
99 |
|
else |
100 |
|
parent = NULL; |
101 |
|
|
102 |
switch (msg) |
switch (msg) |
103 |
{ |
{ |
104 |
|
|
105 |
case WM_WINDOWPOSCHANGED: |
case WM_WINDOWPOSCHANGED: |
|
if (style & WS_CHILD) |
|
|
break; |
|
|
|
|
|
|
|
|
if (wp->flags & SWP_SHOWWINDOW) |
|
106 |
{ |
{ |
107 |
// FIXME: Now, just like create! |
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
|
debug("SWP_SHOWWINDOW for %p!", hwnd); |
|
|
vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0); |
|
108 |
|
|
109 |
// FIXME: SETSTATE |
if (wp->flags & SWP_SHOWWINDOW) |
|
|
|
|
if (!GetWindowRect(hwnd, &rect)) |
|
110 |
{ |
{ |
111 |
debug("GetWindowRect failed!\n"); |
char title[150]; |
112 |
break; |
int state; |
|
} |
|
|
vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", |
|
|
hwnd, |
|
|
rect.left, rect.top, |
|
|
rect.right - rect.left, rect.bottom - rect.top, 0); |
|
|
} |
|
113 |
|
|
114 |
|
vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0); |
115 |
|
|
116 |
if (wp->flags & SWP_HIDEWINDOW) |
GetWindowText(hwnd, title, sizeof(title)); |
|
vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); |
|
117 |
|
|
118 |
|
vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, |
119 |
|
vchannel_strfilter(title), 0); |
120 |
|
|
121 |
if (!(style & WS_VISIBLE)) |
if (style & WS_MAXIMIZE) |
122 |
break; |
state = 2; |
123 |
|
else if (style & WS_MINIMIZE) |
124 |
|
state = 1; |
125 |
|
else |
126 |
|
state = 0; |
127 |
|
|
128 |
if (wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE) |
vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0); |
|
break; |
|
129 |
|
|
130 |
if (!GetWindowRect(hwnd, &rect)) |
update_position(hwnd); |
131 |
{ |
|
132 |
debug("GetWindowRect failed!\n"); |
/* FIXME: Figure out z order */ |
133 |
break; |
} |
|
} |
|
134 |
|
|
135 |
vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", |
if (wp->flags & SWP_HIDEWINDOW) |
136 |
hwnd, |
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
|
rect.left, rect.top, |
|
|
rect.right - rect.left, rect.bottom - rect.top, 0); |
|
137 |
|
|
138 |
break; |
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE)) |
139 |
|
break; |
140 |
|
|
141 |
|
if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE)) |
142 |
|
update_position(hwnd); |
143 |
|
|
144 |
/* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are |
if (!(wp->flags & SWP_NOZORDER)) |
145 |
strange. Sometimes, for example when bringing up the |
{ |
146 |
Notepad About dialog, only an WM_WINDOWPOSCHANGING is |
vchannel_write("ZCHANGE,0x%p,0x%p,0x%x", |
147 |
sent. In some other cases, for exmaple when opening |
hwnd, |
148 |
Format->Text in Notepad, both events are sent. Also, for |
wp->flags & SWP_NOACTIVATE ? wp-> |
149 |
some reason, when closing the Notepad About dialog, an |
hwndInsertAfter : 0, 0); |
150 |
WM_WINDOWPOSCHANGING event is sent which looks just like |
} |
|
the event that was sent when the About dialog was opened... */ |
|
|
case WM_WINDOWPOSCHANGING: |
|
|
if (style & WS_CHILD) |
|
|
break; |
|
151 |
|
|
|
if (!(style & WS_VISIBLE)) |
|
152 |
break; |
break; |
153 |
|
} |
154 |
|
|
155 |
if (!(wp->flags & SWP_NOZORDER)) |
case WM_SIZE: |
156 |
vchannel_write("ZCHANGE1,0x%p,0x%p,0x%x", |
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE)) |
157 |
hwnd, |
break; |
158 |
wp->flags & SWP_NOACTIVATE ? wp->hwndInsertAfter : 0, |
update_position(hwnd); |
|
0); |
|
|
|
|
159 |
break; |
break; |
160 |
|
|
161 |
|
case WM_MOVE: |
162 |
|
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE)) |
163 |
|
break; |
164 |
|
update_position(hwnd); |
165 |
|
break; |
166 |
|
|
167 |
|
case WM_SETTEXT: |
168 |
|
{ |
169 |
|
char *title; |
170 |
|
if (!(style & WS_VISIBLE)) |
171 |
|
break; |
172 |
|
title = _strdup((char *) lparam); |
173 |
|
vchannel_write("TITLE,0x%p,%s,0x%x", hwnd, |
174 |
|
vchannel_strfilter(title), 0); |
175 |
|
free(title); |
176 |
|
break; |
177 |
|
} |
178 |
|
|
179 |
case WM_DESTROY: |
case WM_DESTROY: |
180 |
if (style & WS_CHILD) |
if (!(style & WS_VISIBLE)) |
181 |
break; |
break; |
182 |
|
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
|
vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); |
|
|
|
|
183 |
break; |
break; |
184 |
|
|
|
|
|
185 |
default: |
default: |
186 |
break; |
break; |
187 |
} |
} |
193 |
static LRESULT CALLBACK |
static LRESULT CALLBACK |
194 |
cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam) |
cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam) |
195 |
{ |
{ |
|
char title[150]; |
|
|
|
|
196 |
if (code < 0) |
if (code < 0) |
197 |
goto end; |
goto end; |
198 |
|
|
200 |
{ |
{ |
201 |
case HCBT_MINMAX: |
case HCBT_MINMAX: |
202 |
{ |
{ |
203 |
int show; |
int show, state; |
204 |
|
|
205 |
show = LOWORD(lparam); |
show = LOWORD(lparam); |
206 |
|
|
207 |
if ((show == SW_SHOWMINIMIZED) || (show == SW_MINIMIZE)) |
if ((show == SW_NORMAL) || (show == SW_SHOWNORMAL) |
208 |
|
|| (show == SW_RESTORE)) |
209 |
|
state = 0; |
210 |
|
else if ((show == SW_MINIMIZE) || (show == SW_SHOWMINIMIZED)) |
211 |
|
state = 1; |
212 |
|
else if ((show == SW_MAXIMIZE) || (show == SW_SHOWMAXIMIZED)) |
213 |
|
state = 2; |
214 |
|
else |
215 |
{ |
{ |
216 |
MessageBox(0, |
debug("Unexpected show: %d", show); |
217 |
"Minimizing windows is not allowed in this version. Sorry!", |
break; |
|
"SeamlessRDP", MB_OK); |
|
|
return 1; |
|
218 |
} |
} |
219 |
|
vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0); |
|
GetWindowText((HWND) wparam, title, sizeof(title)); |
|
|
|
|
|
/* FIXME: Strip title of dangerous characters */ |
|
|
|
|
|
vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x", |
|
|
(HWND) wparam, title, show, 0); |
|
220 |
break; |
break; |
221 |
} |
} |
222 |
|
|
228 |
return CallNextHookEx(g_cbt_hook, code, wparam, lparam); |
return CallNextHookEx(g_cbt_hook, code, wparam, lparam); |
229 |
} |
} |
230 |
|
|
|
int |
|
|
vchannel_open() |
|
|
{ |
|
|
g_vchannel = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, |
|
|
WTS_CURRENT_SESSION, CHANNELNAME); |
|
|
|
|
|
if (g_vchannel == NULL) |
|
|
return 0; |
|
|
else |
|
|
return 1; |
|
|
} |
|
|
|
|
|
int |
|
|
vchannel_close() |
|
|
{ |
|
|
BOOL result; |
|
|
|
|
|
result = WTSVirtualChannelClose(g_vchannel); |
|
|
|
|
|
g_vchannel = NULL; |
|
|
|
|
|
if (result) |
|
|
return 1; |
|
|
else |
|
|
return 0; |
|
|
} |
|
|
|
|
|
int |
|
|
vchannel_is_open() |
|
|
{ |
|
|
if (g_vchannel == NULL) |
|
|
return 0; |
|
|
else |
|
|
return 1; |
|
|
} |
|
|
|
|
|
int |
|
|
vchannel_write(char *format, ...) |
|
|
{ |
|
|
BOOL result; |
|
|
va_list argp; |
|
|
char buf[1024]; |
|
|
int size; |
|
|
ULONG bytes_written; |
|
|
|
|
|
if (!vchannel_is_open()) |
|
|
return 1; |
|
|
|
|
|
va_start(argp, format); |
|
|
size = _vsnprintf(buf, sizeof(buf), format, argp); |
|
|
va_end(argp); |
|
|
|
|
|
if (size >= sizeof(buf)) |
|
|
return 0; |
|
|
|
|
|
WaitForSingleObject(g_mutex, INFINITE); |
|
|
result = WTSVirtualChannelWrite(g_vchannel, buf, (ULONG) strlen(buf), &bytes_written); |
|
|
result = WTSVirtualChannelWrite(g_vchannel, "\n", (ULONG) 1, &bytes_written); |
|
|
ReleaseMutex(g_mutex); |
|
|
|
|
|
if (result) |
|
|
return 1; |
|
|
else |
|
|
return 0; |
|
|
} |
|
|
|
|
231 |
DLL_EXPORT void |
DLL_EXPORT void |
232 |
SetHooks(void) |
SetHooks(void) |
233 |
{ |
{ |
263 |
// remember our instance handle |
// remember our instance handle |
264 |
g_instance = hinstDLL; |
g_instance = hinstDLL; |
265 |
|
|
266 |
g_mutex = CreateMutex(NULL, FALSE, "Local\\Seamless"); |
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL"); |
267 |
if (!g_mutex) |
if (!g_mutex) |
268 |
return FALSE; |
return FALSE; |
269 |
|
|