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