-
Notifications
You must be signed in to change notification settings - Fork 5
/
silentbatch.cpp
423 lines (361 loc) · 11 KB
/
silentbatch.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* silentbatch. Based on public-domain code written by Paul Miner.
* Rewritten in C++ by James D. Lin.
*/
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <vector>
#include <string>
#include <sstream>
#include <windows.h>
#include <winuser.h>
#include <shellapi.h>
#define APP_NAME L"silentbatch"
/** CHandle
*
* A class to manage Windows HANDLEs. Note that some Windows
* functions use NULL as an invalid handle value and some use
* INVALID_HANDLE_VALUE, so we must parameterize on the invalid
* handle value.
*/
template<HANDLE invalidHandleValue>
class CHandle
{
public:
explicit CHandle(HANDLE h = invalidHandleValue)
: mHandle(h)
{
}
~CHandle()
{
close();
}
void close()
{
if (mHandle != invalidHandleValue)
{
CloseHandle(mHandle);
}
release();
}
void release()
{
mHandle = invalidHandleValue;
}
HANDLE get() const
{
return mHandle;
}
HANDLE* getAddress()
{
return &mHandle;
}
CHandle& operator=(HANDLE h)
{
close();
mHandle = h;
return *this;
}
operator bool() const
{
return mHandle != invalidHandleValue;
}
private:
HANDLE mHandle;
private:
// Non-copyable.
CHandle(const CHandle&);
CHandle& operator=(const CHandle&);
};
/** LocalAllocGuard
*
* A class to manage memory to be freed via LocalFree.
*/
class LocalAllocGuard
{
public:
explicit LocalAllocGuard(void* p = NULL)
: mP(p)
{
}
~LocalAllocGuard()
{
LocalFree(mP);
}
private:
void* mP;
private:
// Non-copyable.
LocalAllocGuard(const LocalAllocGuard&);
LocalAllocGuard& operator=(const LocalAllocGuard&);
};
struct PipeHandles
{
CHandle<NULL> hRead;
CHandle<NULL> hWrite;
};
/** GetErrorMessage
*
* Retrieves the system error message associated with a given error
* code.
*
* PARAMETERS:
* err : The error code.
*
* RETURNS:
* The error message.
*/
static std::wstring
GetErrorMessage(DWORD err)
{
std::wstring::value_type* p;
if (FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err, 0, reinterpret_cast<std::wstring::value_type*>(&p), 0, NULL)
!= 0)
{
LocalAllocGuard g(p);
return std::wstring(p);
}
else
{
return std::wstring();
}
}
/** DebugOutput
*
* Generates debugging output.
*
* PARAMETERS:
* IN message : The message.
*/
static void
DebugOutput(const std::wstring& message)
{
std::wstringstream ss;
ss << APP_NAME << ": " << message;
OutputDebugStringW(ss.str().c_str());
}
/** OutputError
*
* Generates an error dialog.
*
* PARAMETERS:
* IN message : The main error message.
* error : The error code.
*/
static void
OutputError(const std::wstring& message, DWORD error)
{
std::wstringstream ss;
ss << message << L" (" << error << L")";
DebugOutput(ss.str());
const std::wstring s = message + L"\n" + GetErrorMessage(error);
MessageBoxW(NULL, s.c_str(), APP_NAME, MB_ICONERROR | MB_OK);
}
/** CreateChildProcess
*
* Creates a specified child process.
*
* PARAMETERS:
* IN cmdLine : The command line to invoke the child process.
* hChildStdOutWr : Pipe handle that the child process should use when
* writing to stdout or to stderr.
* hChildStdinRd : Pipe handle that the child process should use when
* reading from stdin.
*
* RETURNS:
* A handle to the created process.
* Returns NULL on failure.
*/
static HANDLE
CreateChildProcess(const std::wstring& cmdLine,
HANDLE hChildStdOutWr, HANDLE hChildStdInRd)
{
PROCESS_INFORMATION piProcInfo = { 0 };
STARTUPINFOW siStartInfo = { 0 };
siStartInfo.cb = sizeof siStartInfo;
siStartInfo.hStdError = hChildStdOutWr;
siStartInfo.hStdOutput = hChildStdOutWr;
siStartInfo.hStdInput = hChildStdInRd;
siStartInfo.dwFlags = STARTF_USESTDHANDLES;
// Create the child process.
std::vector<wchar_t> cmdLineBuf(cmdLine.begin(), cmdLine.end());
cmdLineBuf.push_back(L'\0');
BOOL success = CreateProcessW(NULL,
&cmdLineBuf[0], // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
CREATE_NO_WINDOW, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
if (success) { CloseHandle(piProcInfo.hThread); }
return success ? piProcInfo.hProcess : NULL;
}
/** ReadFromPipe
*
* Reads output from the child process and writes it to the parent's
* stdout.
*
* PARAMETERS:
* hProc : Handle the child process.
* hChildStdOutRd : Pipe handle used to read the stdout output from
* the child process.
* hParentStdOut : Pipe handle to use to write to the parent
* process's stdout. May be INVALID_HANDLE_VALUE.
*/
static void
ReadFromPipe(HANDLE hProc, HANDLE hChildStdOutRd, HANDLE hParentStdOut)
{
std::vector<char> buf(4096);
DWORD waitResult;
do
{
// Don't bother using WaitForMultipleObjects with hChildStdOutRd.
// Even if nothing's being actively written to it, it apparently
// gets signalled so frequently that it causes large CPU spikes.
// Poll on a 100 ms timeout instead. Alas.
waitResult = WaitForSingleObject(hProc, 100);
DWORD available;
if (!PeekNamedPipe(hChildStdOutRd, NULL, 0, NULL, &available, NULL))
{
break;
}
if (available > 0)
{
if (available > buf.size()) { buf.resize(available); }
DWORD dwRead;
if ( !ReadFile(hChildStdOutRd, &buf[0], available, &dwRead, NULL)
|| dwRead == 0)
{
break;
}
if (hParentStdOut != INVALID_HANDLE_VALUE)
{
DWORD dwWritten;
(void) WriteFile(hParentStdOut, &buf[0], dwRead, &dwWritten, NULL);
FlushFileBuffers(hParentStdOut);
}
}
} while (waitResult == WAIT_TIMEOUT);
}
/** ParseCommandLine
*
* Parses the command-line.
*
* RETURNS:
* A std::vector containing the command-line arguments.
*/
static std::vector<std::wstring>
ParseCommandLine()
{
int argc;
wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
LocalAllocGuard g(argv);
return std::vector<std::wstring>(argv, argv + argc);
}
int APIENTRY
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
CHandle<INVALID_HANDLE_VALUE> hStdOut;
std::vector<std::wstring> args = ParseCommandLine();
if (args.size() < 2)
{
MessageBoxW(NULL,
APP_NAME L" v1.1. By Paul Miner and James D. Lin.\n"
L"http://www.taenarum.com/software/\n"
L"\n"
L"Usage: " APP_NAME L" BATCHFILE [LOGFILE]\n",
APP_NAME,
MB_ICONINFORMATION | MB_OK);
return EXIT_SUCCESS;
}
if (args.size() > 2)
{
hStdOut = CreateFileW(args[2].c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// Always append to an existing file.
if ( hStdOut
&& SetFilePointer(hStdOut.get(), 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR)
{
hStdOut.close();
}
if (!hStdOut)
{
OutputError(L"Failed to open log file " + args[2] + L".", GetLastError());
return EXIT_FAILURE;
}
}
SECURITY_ATTRIBUTES saAttr = { 0 };
saAttr.nLength = sizeof saAttr;
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
PipeHandles childStdOut;
PipeHandles childStdIn;
CHandle<NULL> hChildStdOutRdCopy;
CHandle<NULL> hChildStdInWrCopy;
// Create a pipe for the child process's stdout.
if (!CreatePipe(childStdOut.hRead.getAddress(),
childStdOut.hWrite.getAddress(),
&saAttr, 0))
{
OutputError(L"Failed to create stdout pipe for child process.", GetLastError());
return EXIT_FAILURE;
}
// Create a pipe for the child process's stdin.
if (!CreatePipe(childStdIn.hRead.getAddress(),
childStdIn.hWrite.getAddress(),
&saAttr, 0))
{
OutputError(L"Failed to create stdin pipe for child process.", GetLastError());
return EXIT_FAILURE;
}
// Create a non-inheritable version of the stdout pipe's read handle.
if (!DuplicateHandle(GetCurrentProcess(), childStdOut.hRead.get(),
GetCurrentProcess(), hChildStdOutRdCopy.getAddress(),
0, FALSE, // Not inherited.
DUPLICATE_SAME_ACCESS))
{
OutputError(L"Failed to duplicate stdout handle.", GetLastError());
return EXIT_FAILURE;
}
childStdOut.hRead.close();
// Create a non-inheritable version of the stdin pipe's write handle.
if (!DuplicateHandle(GetCurrentProcess(), childStdIn.hWrite.get(),
GetCurrentProcess(), hChildStdInWrCopy.getAddress(),
0, FALSE, // Not inherited.
DUPLICATE_SAME_ACCESS))
{
OutputError(L"Failed to duplicate stdin handle.", GetLastError());
return EXIT_FAILURE;
}
childStdIn.hWrite.close();
// Now create the child process.
const std::wstring cmdLine = L"\"" + args[1] + L"\"";
CHandle<NULL> hProc(CreateChildProcess(cmdLine,
childStdOut.hWrite.get(),
childStdIn.hRead.get()));
if (!hProc)
{
OutputError(L"Failed to create process " + cmdLine + L".", GetLastError());
return EXIT_FAILURE;
}
else
{
DebugOutput(L"Spawned: " + cmdLine);
}
// Read from the stdout pipe of the child process.
//
// Close the write end of the pipe before reading from the read end of
// the pipe.
childStdOut.hWrite.close();
ReadFromPipe(hProc.get(), hChildStdOutRdCopy.get(), hStdOut.get());
DebugOutput(L"Completed.");
return EXIT_SUCCESS;
}