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