1 |
astrand |
930 |
//********************************************************************************* |
2 |
|
|
// |
3 |
|
|
//Title: SeamlessRDP Shell |
4 |
|
|
// |
5 |
|
|
//Author: Martin Wickett |
6 |
|
|
// |
7 |
|
|
//Date: 2004 |
8 |
|
|
// |
9 |
|
|
//********************************************************************************* |
10 |
|
|
|
11 |
|
|
#include <windows.h> |
12 |
|
|
|
13 |
|
|
#include "resource.h" |
14 |
|
|
#include "hookdll/hook.h" |
15 |
|
|
|
16 |
|
|
// |
17 |
|
|
// some global data |
18 |
|
|
// |
19 |
|
|
HWND ghWnd; |
20 |
|
|
NOTIFYICONDATA nid; |
21 |
|
|
HINSTANCE hAppInstance; |
22 |
|
|
|
23 |
|
|
static const UINT WM_TRAY_NOTIFY = (WM_APP + 1000); |
24 |
|
|
static const char szAppName[] = "SeamlessRDP Shell"; |
25 |
|
|
|
26 |
|
|
// |
27 |
|
|
// spawn a message box |
28 |
|
|
// |
29 |
|
|
void Message(const char* message) |
30 |
|
|
{ |
31 |
|
|
MessageBox(GetDesktopWindow(),message,"SeamlessRDP Shell", MB_OK); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
// |
35 |
|
|
// manage the tray icon |
36 |
|
|
// |
37 |
|
|
bool InitTrayIcon() |
38 |
|
|
{ |
39 |
|
|
nid.cbSize = sizeof(NOTIFYICONDATA) ; |
40 |
|
|
nid.hWnd = ghWnd; |
41 |
|
|
nid.uID = 0 ; |
42 |
|
|
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP ; |
43 |
|
|
nid.uCallbackMessage = WM_TRAY_NOTIFY; |
44 |
|
|
strcpy(nid.szTip,szAppName); |
45 |
|
|
nid.hIcon = ::LoadIcon(hAppInstance,MAKEINTRESOURCE(IDI_TRAY)); |
46 |
|
|
|
47 |
|
|
if (Shell_NotifyIcon (NIM_ADD,&nid) != TRUE) |
48 |
|
|
{ |
49 |
|
|
Message("Unable to create tray icon."); |
50 |
|
|
return false; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
return true; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
// |
57 |
|
|
// Remove tray icon |
58 |
|
|
// |
59 |
|
|
bool RemoveTrayIcon() |
60 |
|
|
{ |
61 |
|
|
if (Shell_NotifyIcon (NIM_DELETE,&nid) != TRUE) |
62 |
|
|
{ |
63 |
|
|
Message("Unable to remove tray icon."); |
64 |
|
|
return false; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
return true; |
68 |
|
|
|
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
// |
72 |
|
|
// manage the about dialog box |
73 |
|
|
// |
74 |
|
|
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ) |
75 |
|
|
{ |
76 |
|
|
if (uMsg == WM_COMMAND) |
77 |
|
|
{ |
78 |
|
|
WORD wID = LOWORD(wParam); |
79 |
|
|
if (wID == IDOK) DestroyWindow(hwndDlg); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
return 0; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
void AboutDlg() |
86 |
|
|
{ |
87 |
|
|
DialogBox(hAppInstance,MAKEINTRESOURCE(IDD_ABOUT),NULL,DialogProc); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
// |
91 |
|
|
// manage the context menu |
92 |
|
|
// |
93 |
|
|
void DoContextMenu() |
94 |
|
|
{ |
95 |
|
|
HMENU hMenu =LoadMenu(hAppInstance,MAKEINTRESOURCE(IDR_TRAY)); |
96 |
|
|
if (hMenu == NULL) |
97 |
|
|
{ |
98 |
|
|
Message("Unable to load menu ressource."); |
99 |
|
|
return; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
HMENU hSubMenu = GetSubMenu(hMenu,0); |
103 |
|
|
if (hSubMenu == NULL) |
104 |
|
|
{ |
105 |
|
|
Message("Unable to find popup mennu."); |
106 |
|
|
return; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
// get the cursor position |
110 |
|
|
POINT pt; |
111 |
|
|
GetCursorPos (&pt) ; |
112 |
|
|
|
113 |
|
|
SetForegroundWindow(ghWnd); |
114 |
|
|
int cmd = TrackPopupMenu(hSubMenu,TPM_RETURNCMD|TPM_LEFTALIGN|TPM_RIGHTBUTTON, |
115 |
|
|
pt.x,pt.y,0,ghWnd,NULL); |
116 |
|
|
DeleteObject(hMenu); |
117 |
|
|
|
118 |
|
|
switch (cmd) |
119 |
|
|
{ |
120 |
|
|
case ID_WMEXIT : |
121 |
|
|
{ |
122 |
|
|
PostQuitMessage(0); |
123 |
|
|
break; |
124 |
|
|
} |
125 |
|
|
case ID_WMABOUT : |
126 |
|
|
{ |
127 |
|
|
AboutDlg(); |
128 |
|
|
break; |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
// |
134 |
|
|
// manage the main window |
135 |
|
|
// |
136 |
|
|
LONG WINAPI MainWndProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) |
137 |
|
|
{ |
138 |
|
|
switch (uMsg) |
139 |
|
|
{ |
140 |
|
|
case WM_DESTROY : |
141 |
|
|
{ |
142 |
|
|
PostQuitMessage (0); |
143 |
|
|
return 0; |
144 |
|
|
} |
145 |
|
|
case WM_TRAY_NOTIFY : |
146 |
|
|
{ |
147 |
|
|
if (lParam == WM_RBUTTONDOWN) |
148 |
|
|
DoContextMenu(); |
149 |
|
|
return 0; |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
return DefWindowProc (hWnd, uMsg, wParam, lParam); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
// |
157 |
|
|
//Init window |
158 |
|
|
// |
159 |
|
|
bool InitWindow() |
160 |
|
|
{ |
161 |
|
|
// register the frame class |
162 |
|
|
WNDCLASS wndclass; |
163 |
|
|
wndclass.style = 0; |
164 |
|
|
wndclass.lpfnWndProc = (WNDPROC)MainWndProc; |
165 |
|
|
wndclass.cbClsExtra = 0; |
166 |
|
|
wndclass.cbWndExtra = 0; |
167 |
|
|
wndclass.hInstance = hAppInstance; |
168 |
|
|
wndclass.hIcon = 0; |
169 |
|
|
wndclass.hCursor = LoadCursor (NULL,IDC_ARROW); |
170 |
|
|
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); |
171 |
|
|
wndclass.lpszMenuName = NULL; |
172 |
|
|
wndclass.lpszClassName = szAppName; |
173 |
|
|
|
174 |
|
|
if (!RegisterClass (&wndclass)) |
175 |
|
|
{ |
176 |
|
|
Message("Unable to register the window class."); |
177 |
|
|
return false; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
// create the frame |
181 |
|
|
ghWnd = CreateWindow (szAppName, szAppName, |
182 |
|
|
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, |
183 |
|
|
CW_USEDEFAULT, |
184 |
|
|
CW_USEDEFAULT, |
185 |
|
|
640, |
186 |
|
|
480, |
187 |
|
|
NULL, |
188 |
|
|
NULL, |
189 |
|
|
hAppInstance, |
190 |
|
|
NULL); |
191 |
|
|
|
192 |
|
|
// make sure window was created |
193 |
|
|
if (!ghWnd) |
194 |
|
|
{ |
195 |
|
|
Message("Unable to create the window."); |
196 |
|
|
return false; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
return true; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
// |
203 |
|
|
// init |
204 |
|
|
// |
205 |
|
|
bool Init(LPSTR lpCmdLine) |
206 |
|
|
{ |
207 |
|
|
// try to load WTSWinClipper.dll |
208 |
|
|
if (!WTSWinClipper::Init()) |
209 |
|
|
{ |
210 |
|
|
Message("Application not installed correctly: Unable to init hookdll.dll."); |
211 |
|
|
return false; |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
// check number of instances |
215 |
|
|
if (WTSWinClipper::GetInstanceCount() == 1) |
216 |
|
|
{ |
217 |
|
|
// hook in |
218 |
|
|
WTSWinClipper::SetCbtHook(); |
219 |
|
|
return true; |
220 |
|
|
} else |
221 |
|
|
{ |
222 |
|
|
// already hooked |
223 |
|
|
return false; |
224 |
|
|
} |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
// |
228 |
|
|
// our main loop |
229 |
|
|
// |
230 |
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) |
231 |
|
|
{ |
232 |
|
|
hAppInstance = hInstance; |
233 |
|
|
if (!Init(lpCmdLine)) |
234 |
|
|
{ |
235 |
|
|
return 0; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
// if we have been specified an app to launch, we will wait until the app has closed and use that for |
239 |
|
|
// our cue to exit |
240 |
|
|
if (strlen(lpCmdLine)> 0) |
241 |
|
|
{ |
242 |
|
|
// Because we do not have a explorer.exe we need to make this application the replacement |
243 |
|
|
// shell. We do this by calling SystemParametersInfo. If we don't do this, we won't get the WH_SHELL notifications. |
244 |
|
|
|
245 |
|
|
// From MSDN: |
246 |
|
|
// Note that custom shell applications do not receive WH_SHELL messages. Therefore, any application that |
247 |
|
|
// registers itself as the default shell must call the SystemParametersInfo function with SPI_SETMINIMIZEDMETRICS |
248 |
|
|
// before it (or any other application) can receive WH_SHELL messages. |
249 |
|
|
|
250 |
|
|
MINIMIZEDMETRICS mmm; |
251 |
|
|
mmm.cbSize = sizeof( MINIMIZEDMETRICS); |
252 |
|
|
SystemParametersInfo(SPI_SETMINIMIZEDMETRICS ,sizeof( MINIMIZEDMETRICS),&mmm,0); |
253 |
|
|
|
254 |
|
|
//set the current directory to that of the requested app .exe location |
255 |
|
|
//tokenise lpCmdLine. first is the exe path. second (if exists) is the current directory to set. |
256 |
|
|
//SetCurrentDirectory (); |
257 |
|
|
|
258 |
|
|
//start process specified from command line arg. |
259 |
|
|
PROCESS_INFORMATION procInfo; |
260 |
|
|
STARTUPINFO startupInfo = {0}; |
261 |
|
|
startupInfo.cb = sizeof(STARTUPINFO); |
262 |
|
|
char attr[] = ""; |
263 |
|
|
LPTSTR process= lpCmdLine; |
264 |
|
|
DWORD dwExitCode; |
265 |
|
|
|
266 |
|
|
BOOL m_create = CreateProcess( NULL,process,NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &procInfo); |
267 |
|
|
|
268 |
|
|
if (m_create != false) |
269 |
|
|
{ |
270 |
|
|
// A loop to watch the process. |
271 |
|
|
GetExitCodeProcess(procInfo.hProcess, &dwExitCode); |
272 |
|
|
|
273 |
|
|
while (dwExitCode == STILL_ACTIVE ) |
274 |
|
|
{ |
275 |
|
|
GetExitCodeProcess(procInfo.hProcess, &dwExitCode); |
276 |
|
|
Sleep(1000); |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
// Release handles |
280 |
|
|
CloseHandle(procInfo.hProcess); |
281 |
|
|
CloseHandle(procInfo.hThread); |
282 |
|
|
} |
283 |
|
|
else |
284 |
|
|
{ |
285 |
|
|
// CreateProcess failed. |
286 |
|
|
Message("Unable to launch the requested application"); |
287 |
|
|
} |
288 |
|
|
} |
289 |
|
|
else |
290 |
|
|
// we are launching without an app, therefore we will show the system tray app and wait for the user to close it |
291 |
|
|
{ |
292 |
|
|
// create a dummy window to receive WM_QUIT message |
293 |
|
|
InitWindow(); |
294 |
|
|
|
295 |
|
|
// create the tray icon |
296 |
|
|
InitTrayIcon(); |
297 |
|
|
|
298 |
|
|
// just get and dispatch messages until we're killed |
299 |
|
|
MSG msg; |
300 |
|
|
while (GetMessage(&msg,0,0,0)) |
301 |
|
|
{ |
302 |
|
|
TranslateMessage(&msg); |
303 |
|
|
DispatchMessage(&msg); |
304 |
|
|
}; |
305 |
|
|
|
306 |
|
|
// remove our tray icon |
307 |
|
|
RemoveTrayIcon(); |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
|
311 |
|
|
// remove hook before saying goodbye |
312 |
|
|
WTSWinClipper::RemoveCbtHook(); |
313 |
|
|
|
314 |
|
|
WTSWinClipper::Done(); |
315 |
|
|
|
316 |
|
|
return 1; |
317 |
|
|
} |