/[rdesktop]/sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1091 - (show annotations)
Fri Mar 10 09:12:36 2006 UTC (18 years, 2 months ago) by astrand
File MIME type: text/plain
File size: 5601 byte(s)
Removed the version numbers from SeamlessRDP commands

1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Seamless windows - Remote server hook DLL
4
5 Based on code copyright (C) 2004-2005 Martin Wickett
6
7 Copyright (C) Peter Åstrand <astrand@cendio.se> 2005-2006
8 Copyright (C) Pierre Ossman <ossman@cendio.se> 2006
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #include <windows.h>
29 #include <winuser.h>
30
31 #include "../vchannel.h"
32
33 #define DLL_EXPORT __declspec(dllexport)
34
35 // Shared DATA
36 #pragma data_seg ( "SHAREDDATA" )
37
38 // this is the total number of processes this dll is currently attached to
39 int g_instance_count = 0;
40
41 #pragma data_seg ()
42
43 #pragma comment(linker, "/section:SHAREDDATA,rws")
44
45 static HHOOK g_cbt_hook = NULL;
46 static HHOOK g_wndproc_hook = NULL;
47
48 static HINSTANCE g_instance = NULL;
49
50 static HANDLE g_mutex = NULL;
51
52 static void
53 update_position(HWND hwnd)
54 {
55 RECT rect;
56
57 if (!GetWindowRect(hwnd, &rect))
58 {
59 debug("GetWindowRect failed!\n");
60 return;
61 }
62
63 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
69 wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details)
70 {
71 HWND hwnd;
72 UINT msg;
73 WPARAM wparam;
74 LPARAM lparam;
75
76 LONG style;
77
78 if (code < 0)
79 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 switch (msg)
94 {
95
96 case WM_WINDOWPOSCHANGED:
97 {
98 WINDOWPOS *wp = (WINDOWPOS *) lparam;
99
100 if (wp->flags & SWP_SHOWWINDOW)
101 {
102 // FIXME: Now, just like create!
103 debug("SWP_SHOWWINDOW for %p!", hwnd);
104 vchannel_write("CREATE,0x%p,0x%x", hwnd, 0);
105
106 // FIXME: SETSTATE
107
108 update_position(hwnd);
109 }
110
111 if (wp->flags & SWP_HIDEWINDOW)
112 vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);
113
114 if (!(style & WS_VISIBLE))
115 break;
116
117 if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))
118 update_position(hwnd);
119
120 if (!(wp->flags & SWP_NOZORDER))
121 {
122 vchannel_write("ZCHANGE,0x%p,0x%p,0x%x",
123 hwnd,
124 wp->flags & SWP_NOACTIVATE ? wp->
125 hwndInsertAfter : 0, 0);
126 }
127
128 break;
129 }
130
131 case WM_SIZE:
132 if (!(style & WS_VISIBLE))
133 break;
134 update_position(hwnd);
135 break;
136
137 case WM_MOVE:
138 if (!(style & WS_VISIBLE))
139 break;
140 update_position(hwnd);
141 break;
142
143 case WM_DESTROY:
144 if (!(style & WS_VISIBLE))
145 break;
146 vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);
147 break;
148
149 default:
150 break;
151 }
152
153 end:
154 return CallNextHookEx(g_wndproc_hook, code, cur_thread, details);
155 }
156
157 static LRESULT CALLBACK
158 cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam)
159 {
160 char title[150];
161
162 if (code < 0)
163 goto end;
164
165 switch (code)
166 {
167 case HCBT_MINMAX:
168 {
169 int show, state;
170
171 show = LOWORD(lparam);
172
173 if ((show == SW_SHOWMINIMIZED) || (show == SW_MINIMIZE))
174 {
175 MessageBox(0,
176 "Minimizing windows is not allowed in this version. Sorry!",
177 "SeamlessRDP", MB_OK);
178 return 1;
179 }
180
181 GetWindowText((HWND) wparam, title, sizeof(title));
182
183 /* FIXME: Strip title of dangerous characters */
184
185 if (show == SW_SHOWNORMAL)
186 state = 0;
187 else if (show == SW_SHOWMINIMIZED)
188 state = 1;
189 else if (show == SW_SHOWMAXIMIZED)
190 state = 2;
191 vchannel_write("SETSTATE,0x%p,%s,0x%x,0x%x",
192 (HWND) wparam, title, state, 0);
193 break;
194 }
195
196 default:
197 break;
198 }
199
200 end:
201 return CallNextHookEx(g_cbt_hook, code, wparam, lparam);
202 }
203
204 DLL_EXPORT void
205 SetHooks(void)
206 {
207 if (!g_cbt_hook)
208 g_cbt_hook = SetWindowsHookEx(WH_CBT, cbt_hook_proc, g_instance, 0);
209
210 if (!g_wndproc_hook)
211 g_wndproc_hook = SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0);
212 }
213
214 DLL_EXPORT void
215 RemoveHooks(void)
216 {
217 if (g_cbt_hook)
218 UnhookWindowsHookEx(g_cbt_hook);
219
220 if (g_wndproc_hook)
221 UnhookWindowsHookEx(g_wndproc_hook);
222 }
223
224 DLL_EXPORT int
225 GetInstanceCount()
226 {
227 return g_instance_count;
228 }
229
230 BOOL APIENTRY
231 DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved)
232 {
233 switch (ul_reason_for_call)
234 {
235 case DLL_PROCESS_ATTACH:
236 // remember our instance handle
237 g_instance = hinstDLL;
238
239 g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL");
240 if (!g_mutex)
241 return FALSE;
242
243 WaitForSingleObject(g_mutex, INFINITE);
244 ++g_instance_count;
245 ReleaseMutex(g_mutex);
246
247 vchannel_open();
248
249 break;
250
251 case DLL_THREAD_ATTACH:
252 break;
253
254 case DLL_THREAD_DETACH:
255 break;
256
257 case DLL_PROCESS_DETACH:
258 WaitForSingleObject(g_mutex, INFINITE);
259 --g_instance_count;
260 ReleaseMutex(g_mutex);
261
262 vchannel_close();
263
264 CloseHandle(g_mutex);
265
266 break;
267 }
268
269 return TRUE;
270 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26