-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWinMain.cpp
More file actions
351 lines (294 loc) · 10.6 KB
/
WinMain.cpp
File metadata and controls
351 lines (294 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This is a part of the Litestep Shell source code.
//
// Copyright (C) 1997-2011 LiteStep Development Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "litestep.h"
#include "../utility/macros.h"
#include "../utility/core.hpp"
// How long to wait for Explorer to start up as shell, in milliseconds
// Shouldn't use INFINITE since we may block forever in safe mode
#define EXPLORER_WAIT_TIMEOUT 20000
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// IsOtherInstanceRunning
// Checks if another LiteStep instance is running already
//
bool IsOtherInstanceRunning(LPHANDLE phMutex)
{
ASSERT(phMutex);
bool bIsOther = false;
*phMutex = CreateMutex(NULL, FALSE, _T("LiteStep"));
if (*phMutex && (GetLastError() == ERROR_ALREADY_EXISTS))
{
bIsOther = true;
}
return bIsOther;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// ParseCommandLine
// Converts command line parameters to start flags
//
WORD ParseCommandLine(LPCTSTR pszCommandLine, LPTSTR pszFile, DWORD cchFile)
{
ASSERT(pszCommandLine);
// By default, run LiteStep and startup apps
WORD wStartFlags = LSF_RUN_LITESTEP | LSF_RUN_STARTUPAPPS;
char szToken[MAX_LINE_LENGTH] = { 0 };
LPCSTR pszNextToken = pszCommandLine;
while (GetToken(pszNextToken, szToken, &pszNextToken, FALSE))
{
if (szToken[0] == '-')
{
if (!_stricmp(szToken, "-nostartup"))
{
wStartFlags &= ~LSF_RUN_STARTUPAPPS;
}
else if (!_stricmp(szToken, "-startup"))
{
wStartFlags |= LSF_FORCE_STARTUPAPPS;
}
else if (!_stricmp(szToken, "-explorer"))
{
wStartFlags &= ~LSF_RUN_LITESTEP;
wStartFlags |= LSF_RUN_EXPLORER;
}
}
else
{
ASSERT(szToken[0] != '!');
DWORD dwCopied = GetFullPathName(szToken, cchFile, pszFile, NULL);
if (dwCopied == 0 || dwCopied > cchFile)
{
pszFile[0] = _T('\0');
}
wStartFlags |= LSF_ALTERNATE_CONFIG;
}
}
return wStartFlags;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// SendCommandLineBang
// Sends given !bang to a running litestep.exe instance
//
bool SendCommandLineBang(LPCTSTR pszCommand, LPCTSTR pszArgs)
{
ASSERT(pszCommand);
bool bSuccess = false;
HWND hWnd = FindWindow(szMainWindowClass, szMainWindowTitle);
if (IsWindow(hWnd))
{
LMBANGCOMMAND bangCommand;
bangCommand.cbSize = sizeof(LMBANGCOMMAND);
bangCommand.hWnd = NULL;
HRESULT hr = StringCchCopy(
bangCommand.szCommand, MAX_BANGCOMMAND, pszCommand);
if (SUCCEEDED(hr))
{
if (pszArgs)
{
hr = StringCchCopy(bangCommand.szArgs, MAX_BANGARGS, pszArgs);
}
else
{
bangCommand.szArgs[0] = '\0';
}
}
if (SUCCEEDED(hr))
{
// Since we're a new, different litestep.exe process here, give the
// other, "real" instance the right to set the foreground window
TryAllowSetForegroundWindow(hWnd);
COPYDATASTRUCT cds = { 0 };
cds.cbData = sizeof(LMBANGCOMMAND);
cds.dwData = LM_BANGCOMMAND;
cds.lpData = &bangCommand;
if (SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&cds))
{
bSuccess = true;
}
}
}
return bSuccess;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// HandleCommandLineBang
// Grabs !bang command and any arguments from a command line and
// sends them to a running litestep.exe instance
//
int HandleCommandLineBang(LPCTSTR pszCommandLine)
{
ASSERT(pszCommandLine);
int nReturn = -1;
// Can't just use MAX_BANGCOMMAND + MAX_BANGARGS since
// there may be lots of whitespace on the command line
TCHAR szBuffer[MAX_LINE_LENGTH] = { 0 };
if (SUCCEEDED(StringCchCopy(szBuffer, COUNTOF(szBuffer), pszCommandLine)))
{
LPCTSTR pszArgs = NULL;
LPTSTR pszBangEnd = _tcschr(szBuffer, _T(' '));
if (pszBangEnd)
{
pszArgs = pszBangEnd + _tcsspn(pszBangEnd, _T(" "));
// Cut off !bang arguments in szBuffer
*pszBangEnd = _T('\0');
}
if (SendCommandLineBang(szBuffer, pszArgs))
{
nReturn = 0;
}
}
return nReturn;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// StartExplorerShell
// Try to start Explorer in shell mode
//
bool StartExplorerShell(DWORD dwWaitTimeout)
{
bool bStarted = false;
TCHAR szOldShell[MAX_PATH] = { 0 };
DWORD dwCopied = GetPrivateProfileString(_T("boot"), _T("shell"), NULL,
szOldShell, COUNTOF(szOldShell), _T("system.ini"));
// If this user account has limited access rights and
// the shell is in HKLM, GetPrivateProfileString returns 0
if (dwCopied > 0 && dwCopied < (COUNTOF(szOldShell)-1))
{
if (WritePrivateProfileString(
_T("boot"), _T("shell"), _T("explorer.exe"), _T("system.ini")))
{
// We have successfully set Explorer as shell, now launch it...
SHELLEXECUTEINFO sei = { 0 };
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_DOENVSUBST | SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = _T("open");
sei.lpFile = _T("%windir%\\explorer.exe");
if (LSShellExecuteEx(&sei))
{
// If we don't wait here, there'll be a race condition:
// We may reset the 'shell' setting before Explorer reads it.
if (WaitForInputIdle(sei.hProcess, dwWaitTimeout) == 0)
{
bStarted = true;
}
CloseHandle(sei.hProcess);
}
WritePrivateProfileString(
_T("boot"), _T("shell"), szOldShell, _T("system.ini"));
}
}
return bStarted;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// _tWinMain
// Main entry point. Chooses appropriate startup mode.
//
int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE, LPTSTR lpCmdLine, int)
{
int nReturn = 0;
// This is safe since lpCmdLine has no leading spaces and is never NULL
if (lpCmdLine[0] == _T('!'))
{
//
// Mode 1: Command line !bang handling
//
nReturn = HandleCommandLineBang(lpCmdLine);
}
else
{
TCHAR szAltConfigFile[MAX_PATH] = { 0 };
WORD wStartFlags = ParseCommandLine(
lpCmdLine, szAltConfigFile, COUNTOF(szAltConfigFile));
if (GetSystemMetrics(SM_CLEANBOOT))
{
// We're in safe mode. We really want Explorer to run now, in case
// LiteStep is the reason we're here.
// But if we can't, make sure we at least ignore startup apps
// (see docs for GetSystemMetrics(SM_CLEANBOOT))
wStartFlags |= LSF_RUN_EXPLORER;
wStartFlags &= ~LSF_RUN_STARTUPAPPS;
}
bool bLoop = true;
while (bLoop)
{
// Loop back here if explorer is requested as the shell
// while LiteStep is already running
bLoop = false;
if (wStartFlags & LSF_RUN_EXPLORER)
{
//
// Mode 2: (Try to) start Explorer
//
if (StartExplorerShell(EXPLORER_WAIT_TIMEOUT))
{
// Explorer started as shell, no need try LiteStep as well
wStartFlags &= ~LSF_RUN_LITESTEP;
}
else
{
wStartFlags &= ~LSF_RUN_EXPLORER;
}
}
if (wStartFlags & LSF_RUN_LITESTEP)
{
HANDLE hMutex = NULL;
if (IsOtherInstanceRunning(&hMutex))
{
//
// Mode 3a: Other LiteStep instance already running
//
RESOURCE_STR(hInst, IDS_LITESTEP_ERROR1,
"A previous instance of LiteStep was detected.\n"
"Are you sure you want to continue?");
// Can show a MessageBox here since the other instance
// should have closed the welcome screen already
INT idConfirm = RESOURCE_MSGBOX_F(
"LiteStep", MB_ICONINFORMATION | MB_YESNO | MB_DEFBUTTON2);
if (idConfirm == IDNO)
{
wStartFlags &= ~LSF_RUN_LITESTEP;
}
}
if (wStartFlags & LSF_RUN_LITESTEP)
{
//
// Mode 3b: Start the shell!
//
nReturn = StartLitestep(hInst, wStartFlags, szAltConfigFile);
}
if (hMutex)
{
CloseHandle(hMutex);
}
}
if ((nReturn == 4) && !(wStartFlags & LSF_RUN_EXPLORER))
{
// User wants Explorer as the shell anyway
wStartFlags |= LSF_RUN_EXPLORER;
bLoop = true;
}
}
}
return nReturn;
}