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 |
|
|
52 |
static HANDLE g_vchannel = NULL; |
static LRESULT CALLBACK |
53 |
|
wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) |
|
static void |
|
|
debug(char *format, ...) |
|
54 |
{ |
{ |
55 |
va_list argp; |
HWND hwnd; |
56 |
char buf[256]; |
UINT msg; |
57 |
|
WPARAM wparam; |
58 |
|
LPARAM lparam; |
59 |
|
|
60 |
sprintf(buf, "DEBUG1,"); |
LONG style; |
61 |
|
|
62 |
va_start(argp, format); |
if (code < 0) |
63 |
_vsnprintf(buf + sizeof("DEBUG1,") - 1, sizeof(buf) - sizeof("DEBUG1,") + 1, format, argp); |
goto end; |
|
va_end(argp); |
|
64 |
|
|
65 |
vchannel_write(buf); |
hwnd = ((CWPSTRUCT *) details)->hwnd; |
66 |
} |
msg = ((CWPSTRUCT *) details)->message; |
67 |
|
wparam = ((CWPSTRUCT *) details)->wParam; |
68 |
|
lparam = ((CWPSTRUCT *) details)->lParam; |
69 |
|
|
70 |
static LRESULT CALLBACK |
style = GetWindowLong(hwnd, GWL_STYLE); |
|
wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) |
|
|
{ |
|
|
HWND hwnd = ((CWPSTRUCT *) details)->hwnd; |
|
|
UINT msg = ((CWPSTRUCT *) details)->message; |
|
|
WPARAM wparam = ((CWPSTRUCT *) details)->wParam; |
|
|
LPARAM lparam = ((CWPSTRUCT *) details)->lParam; |
|
|
|
|
|
LONG style = GetWindowLong(hwnd, GWL_STYLE); |
|
|
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
|
|
RECT rect; |
|
71 |
|
|
72 |
if (code < 0) |
if (style & WS_CHILD) |
73 |
goto end; |
goto end; |
74 |
|
|
75 |
switch (msg) |
switch (msg) |
76 |
{ |
{ |
77 |
|
|
78 |
case WM_WINDOWPOSCHANGED: |
case WM_WINDOWPOSCHANGED: |
79 |
if (style & WS_CHILD) |
{ |
80 |
break; |
RECT rect; |
81 |
|
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
82 |
|
|
83 |
|
if (wp->flags & SWP_SHOWWINDOW) |
84 |
|
{ |
85 |
|
// FIXME: Now, just like create! |
86 |
|
debug("SWP_SHOWWINDOW for %p!", hwnd); |
87 |
|
vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0); |
88 |
|
|
89 |
|
// FIXME: SETSTATE |
90 |
|
|
91 |
|
if (!GetWindowRect(hwnd, &rect)) |
92 |
|
{ |
93 |
|
debug("GetWindowRect failed!\n"); |
94 |
|
break; |
95 |
|
} |
96 |
|
vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", |
97 |
|
hwnd, |
98 |
|
rect.left, rect.top, |
99 |
|
rect.right - rect.left, |
100 |
|
rect.bottom - rect.top, 0); |
101 |
|
} |
102 |
|
|
103 |
if (wp->flags & SWP_SHOWWINDOW) |
if (wp->flags & SWP_HIDEWINDOW) |
104 |
{ |
vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); |
105 |
// FIXME: Now, just like create! |
|
106 |
debug("SWP_SHOWWINDOW for %p!", hwnd); |
if (!(style & WS_VISIBLE)) |
107 |
vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0); |
break; |
108 |
|
|
109 |
// FIXME: SETSTATE |
if (wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE) |
110 |
|
break; |
111 |
|
|
112 |
if (!GetWindowRect(hwnd, &rect)) |
if (!GetWindowRect(hwnd, &rect)) |
113 |
{ |
{ |
114 |
debug("GetWindowRect failed!\n"); |
debug("GetWindowRect failed!\n"); |
115 |
break; |
break; |
116 |
} |
} |
117 |
|
|
118 |
vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", |
vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", |
119 |
hwnd, |
hwnd, |
120 |
rect.left, rect.top, |
rect.left, rect.top, |
121 |
rect.right - rect.left, rect.bottom - rect.top, 0); |
rect.right - rect.left, rect.bottom - rect.top, 0); |
|
} |
|
|
|
|
|
|
|
|
if (wp->flags & SWP_HIDEWINDOW) |
|
|
vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); |
|
|
|
|
|
|
|
|
if (!(style & WS_VISIBLE)) |
|
|
break; |
|
|
|
|
|
if (wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE) |
|
|
break; |
|
122 |
|
|
|
if (!GetWindowRect(hwnd, &rect)) |
|
|
{ |
|
|
debug("GetWindowRect failed!\n"); |
|
123 |
break; |
break; |
124 |
} |
} |
125 |
|
|
|
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); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
/* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are |
|
|
strange. Sometimes, for example when bringing up the |
|
|
Notepad About dialog, only an WM_WINDOWPOSCHANGING is |
|
|
sent. In some other cases, for exmaple when opening |
|
|
Format->Text in Notepad, both events are sent. Also, for |
|
|
some reason, when closing the Notepad About dialog, an |
|
|
WM_WINDOWPOSCHANGING event is sent which looks just like |
|
|
the event that was sent when the About dialog was opened... */ |
|
126 |
case WM_WINDOWPOSCHANGING: |
case WM_WINDOWPOSCHANGING: |
127 |
if (style & WS_CHILD) |
{ |
128 |
break; |
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
|
|
|
|
if (!(style & WS_VISIBLE)) |
|
|
break; |
|
|
|
|
|
if (!(wp->flags & SWP_NOZORDER)) |
|
|
vchannel_write("ZCHANGE1,0x%p,0x%p,0x%x", |
|
|
hwnd, |
|
|
wp->flags & SWP_NOACTIVATE ? wp->hwndInsertAfter : 0, |
|
|
0); |
|
|
|
|
|
break; |
|
|
|
|
129 |
|
|
130 |
|
if (!(style & WS_VISIBLE)) |
131 |
|
break; |
132 |
|
|
133 |
|
if (!(wp->flags & SWP_NOZORDER)) |
134 |
|
vchannel_write("ZCHANGE1,0x%p,0x%p,0x%x", |
135 |
|
hwnd, |
136 |
|
wp->flags & SWP_NOACTIVATE ? wp-> |
137 |
|
hwndInsertAfter : 0, 0); |
138 |
|
|
|
case WM_DESTROY: |
|
|
if (style & WS_CHILD) |
|
139 |
break; |
break; |
140 |
|
} |
141 |
|
|
142 |
|
case WM_DESTROY: |
143 |
vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); |
vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); |
|
|
|
144 |
break; |
break; |
145 |
|
|
|
|
|
146 |
default: |
default: |
147 |
break; |
break; |
148 |
} |
} |
163 |
{ |
{ |
164 |
case HCBT_MINMAX: |
case HCBT_MINMAX: |
165 |
{ |
{ |
166 |
int show; |
int show, state; |
167 |
|
|
168 |
show = LOWORD(lparam); |
show = LOWORD(lparam); |
169 |
|
|
179 |
|
|
180 |
/* FIXME: Strip title of dangerous characters */ |
/* FIXME: Strip title of dangerous characters */ |
181 |
|
|
182 |
|
if (show == SW_SHOWNORMAL) |
183 |
|
state = 0; |
184 |
|
else if (show == SW_SHOWMINIMIZED) |
185 |
|
state = 1; |
186 |
|
else if (show == SW_SHOWMAXIMIZED) |
187 |
|
state = 2; |
188 |
vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x", |
vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x", |
189 |
(HWND) wparam, title, show, 0); |
(HWND) wparam, title, state, 0); |
190 |
break; |
break; |
191 |
} |
} |
192 |
|
|
198 |
return CallNextHookEx(g_cbt_hook, code, wparam, lparam); |
return CallNextHookEx(g_cbt_hook, code, wparam, lparam); |
199 |
} |
} |
200 |
|
|
|
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; |
|
|
} |
|
|
|
|
201 |
DLL_EXPORT void |
DLL_EXPORT void |
202 |
SetHooks(void) |
SetHooks(void) |
203 |
{ |
{ |
233 |
// remember our instance handle |
// remember our instance handle |
234 |
g_instance = hinstDLL; |
g_instance = hinstDLL; |
235 |
|
|
236 |
g_mutex = CreateMutex(NULL, FALSE, "Local\\Seamless"); |
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL"); |
237 |
if (!g_mutex) |
if (!g_mutex) |
238 |
return FALSE; |
return FALSE; |
239 |
|
|