/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1082 - (hide annotations)
Thu Mar 9 16:27:59 2006 UTC (18 years, 3 months ago) by ossman_
File MIME type: text/plain
File size: 5484 byte(s)
Microsoft's docs say that WS_CHILD and WS_POPUP is an illegal combination,
but they exist nonetheless. The resulting window is as if WS_CHILD wasn't
set.

1 ossman_ 1071 /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.
3     Seamless windows - Remote server hook DLL
4 ossman_ 1069
5 ossman_ 1071 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 ossman_ 1069 #include <stdio.h>
26     #include <stdarg.h>
27    
28 ossman_ 1071 #include <windows.h>
29     #include <winuser.h>
30 ossman_ 1069
31 ossman_ 1073 #include "../vchannel.h"
32 ossman_ 1071
33 ossman_ 1069 #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 ossman_ 1071 int g_instance_count = 0;
40 ossman_ 1069
41     #pragma data_seg ()
42    
43     #pragma comment(linker, "/section:SHAREDDATA,rws")
44    
45 ossman_ 1071 static HHOOK g_cbt_hook = NULL;
46     static HHOOK g_wndproc_hook = NULL;
47 ossman_ 1069
48 ossman_ 1071 static HINSTANCE g_instance = NULL;
49 ossman_ 1069
50 ossman_ 1071 static HANDLE g_mutex = NULL;
51 ossman_ 1069
52 ossman_ 1081 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("POSITION1,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 ossman_ 1071 static LRESULT CALLBACK
69     wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details)
70 ossman_ 1069 {
71 ossman_ 1079 HWND hwnd;
72     UINT msg;
73     WPARAM wparam;
74     LPARAM lparam;
75 ossman_ 1069
76 ossman_ 1079 LONG style;
77 ossman_ 1069
78 ossman_ 1071 if (code < 0)
79     goto end;
80 ossman_ 1069
81 ossman_ 1079 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 ossman_ 1082 /* 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 ossman_ 1079 goto end;
92    
93 ossman_ 1071 switch (msg)
94     {
95 ossman_ 1069
96 ossman_ 1071 case WM_WINDOWPOSCHANGED:
97 ossman_ 1079 {
98     WINDOWPOS *wp = (WINDOWPOS *) lparam;
99 ossman_ 1069
100 ossman_ 1079 if (wp->flags & SWP_SHOWWINDOW)
101     {
102     // FIXME: Now, just like create!
103     debug("SWP_SHOWWINDOW for %p!", hwnd);
104     vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0);
105 ossman_ 1069
106 ossman_ 1079 // FIXME: SETSTATE
107 ossman_ 1069
108 ossman_ 1081 update_position(hwnd);
109 ossman_ 1079 }
110 ossman_ 1069
111 ossman_ 1079 if (wp->flags & SWP_HIDEWINDOW)
112     vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0);
113    
114     if (!(style & WS_VISIBLE))
115     break;
116    
117 ossman_ 1080 if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))
118 ossman_ 1081 update_position(hwnd);
119 ossman_ 1079
120     if (!(wp->flags & SWP_NOZORDER))
121 ossman_ 1080 {
122 ossman_ 1079 vchannel_write("ZCHANGE1,0x%p,0x%p,0x%x",
123     hwnd,
124     wp->flags & SWP_NOACTIVATE ? wp->
125     hwndInsertAfter : 0, 0);
126 ossman_ 1080 }
127 ossman_ 1069
128 ossman_ 1071 break;
129 ossman_ 1079 }
130 ossman_ 1069
131 ossman_ 1081 case WM_SIZE:
132     update_position(hwnd);
133     break;
134    
135     case WM_MOVE:
136     update_position(hwnd);
137     break;
138    
139 ossman_ 1071 case WM_DESTROY:
140     vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0);
141     break;
142 ossman_ 1069
143 ossman_ 1071 default:
144     break;
145     }
146    
147     end:
148     return CallNextHookEx(g_wndproc_hook, code, cur_thread, details);
149 ossman_ 1069 }
150    
151 ossman_ 1071 static LRESULT CALLBACK
152     cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam)
153 ossman_ 1069 {
154 ossman_ 1071 char title[150];
155 ossman_ 1069
156 ossman_ 1071 if (code < 0)
157     goto end;
158 ossman_ 1069
159 ossman_ 1071 switch (code)
160     {
161     case HCBT_MINMAX:
162     {
163 ossman_ 1079 int show, state;
164 ossman_ 1069
165 ossman_ 1071 show = LOWORD(lparam);
166 ossman_ 1069
167 ossman_ 1071 if ((show == SW_SHOWMINIMIZED) || (show == SW_MINIMIZE))
168     {
169     MessageBox(0,
170     "Minimizing windows is not allowed in this version. Sorry!",
171     "SeamlessRDP", MB_OK);
172     return 1;
173     }
174 ossman_ 1069
175 ossman_ 1071 GetWindowText((HWND) wparam, title, sizeof(title));
176 ossman_ 1069
177 ossman_ 1071 /* FIXME: Strip title of dangerous characters */
178 ossman_ 1069
179 ossman_ 1079 if (show == SW_SHOWNORMAL)
180     state = 0;
181     else if (show == SW_SHOWMINIMIZED)
182     state = 1;
183     else if (show == SW_SHOWMAXIMIZED)
184     state = 2;
185 ossman_ 1071 vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x",
186 ossman_ 1079 (HWND) wparam, title, state, 0);
187 ossman_ 1071 break;
188     }
189 ossman_ 1069
190 ossman_ 1071 default:
191     break;
192     }
193 ossman_ 1069
194 ossman_ 1071 end:
195     return CallNextHookEx(g_cbt_hook, code, wparam, lparam);
196 ossman_ 1069 }
197    
198 ossman_ 1071 DLL_EXPORT void
199     SetHooks(void)
200 ossman_ 1069 {
201 ossman_ 1071 if (!g_cbt_hook)
202     g_cbt_hook = SetWindowsHookEx(WH_CBT, cbt_hook_proc, g_instance, 0);
203 ossman_ 1069
204 ossman_ 1071 if (!g_wndproc_hook)
205     g_wndproc_hook = SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0);
206 ossman_ 1069 }
207    
208 ossman_ 1071 DLL_EXPORT void
209     RemoveHooks(void)
210 ossman_ 1069 {
211 ossman_ 1071 if (g_cbt_hook)
212     UnhookWindowsHookEx(g_cbt_hook);
213 ossman_ 1069
214 ossman_ 1071 if (g_wndproc_hook)
215     UnhookWindowsHookEx(g_wndproc_hook);
216 ossman_ 1069 }
217    
218 ossman_ 1071 DLL_EXPORT int
219     GetInstanceCount()
220 ossman_ 1069 {
221 ossman_ 1071 return g_instance_count;
222 ossman_ 1069 }
223    
224 ossman_ 1071 BOOL APIENTRY
225     DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved)
226 ossman_ 1069 {
227 ossman_ 1071 switch (ul_reason_for_call)
228     {
229     case DLL_PROCESS_ATTACH:
230     // remember our instance handle
231     g_instance = hinstDLL;
232 ossman_ 1069
233 ossman_ 1073 g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL");
234 ossman_ 1071 if (!g_mutex)
235     return FALSE;
236 ossman_ 1069
237 ossman_ 1071 WaitForSingleObject(g_mutex, INFINITE);
238     ++g_instance_count;
239     ReleaseMutex(g_mutex);
240 ossman_ 1069
241 ossman_ 1071 vchannel_open();
242 ossman_ 1069
243 ossman_ 1071 break;
244 ossman_ 1069
245 ossman_ 1071 case DLL_THREAD_ATTACH:
246     break;
247 ossman_ 1069
248 ossman_ 1071 case DLL_THREAD_DETACH:
249     break;
250 ossman_ 1069
251 ossman_ 1071 case DLL_PROCESS_DETACH:
252     WaitForSingleObject(g_mutex, INFINITE);
253     --g_instance_count;
254     ReleaseMutex(g_mutex);
255 ossman_ 1069
256 ossman_ 1071 vchannel_close();
257 ossman_ 1069
258 ossman_ 1071 CloseHandle(g_mutex);
259 ossman_ 1069
260 ossman_ 1071 break;
261     }
262 ossman_ 1069
263 ossman_ 1071 return TRUE;
264 ossman_ 1069 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26