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