/[rdesktop]/sourceforge.net/trunk/seamlessrdp/ServerExe/main.cpp
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/seamlessrdp/ServerExe/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 993 - (show annotations)
Sun Aug 28 12:58:39 2005 UTC (18 years, 9 months ago) by astrand
File size: 7777 byte(s)
Indenting with the CVS version of astyle

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

  ViewVC Help
Powered by ViewVC 1.1.26