-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathprocess.cpp
More file actions
151 lines (125 loc) · 3.64 KB
/
Copy pathprocess.cpp
File metadata and controls
151 lines (125 loc) · 3.64 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
#include "stdafx.h"
#include "process.h"
static BOOL CloseHandle_s(HANDLE &h)
{
if (h == NULL)
return TRUE;
const auto result = CloseHandle(h);
h = NULL;
return result;
}
Pipe::Pipe(bool inheritHandle) {
SECURITY_ATTRIBUTES pipeAttr;
AutoZeroMemory(pipeAttr);
pipeAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
pipeAttr.bInheritHandle = inheritHandle;
pipeAttr.lpSecurityDescriptor = NULL;
// We currently don't use the _hWrite handle for anything
// but it is *REQUIRED* for CreatePipe()
if (!CreatePipe(&_hRead, &_hWrite, &pipeAttr, 0)) {
throw std::runtime_error("Unable to create pipe!");
}
// Ensure the read handle to the pipe for STDOUT is not inherited
if (!SetHandleInformation(_hRead, HANDLE_FLAG_INHERIT, 0)) {
throw std::runtime_error("_hRead SetHandleInformation");
}
}
Pipe::~Pipe(void) {
CloseHandle_s(_hRead);
CloseHandle_s(_hWrite);
}
bool Pipe::Read(std::string& dst) {
constexpr size_t bufLen = 256;
//
// _hWrite needs to be closed before reading
// in order for ReadFile to correctly report
// if the child process has been terminated
//
// This must be happen each time you attempt to read apparently
//
CloseHandle_s(_hWrite);
dst.clear();
dst.resize(bufLen);
DWORD dwRead = 0;
bool result = ReadFile(_hRead, (void*)dst.data(), bufLen - 1, &dwRead, NULL);
if (!result || !dwRead)
return false;
dst[dwRead] = 0;
return true;
}
Process::Process(const PROCESS_INFORMATION &processInfo)
{
this->processInfo = processInfo;
}
Process::~Process(void)
{
CloseHandle_s(this->processInfo.hProcess);
CloseHandle_s(this->processInfo.hThread);
if (this->processInfo.hProcess != NULL)
{
CloseHandle(this->processInfo.hProcess);
this->processInfo.hProcess = NULL;
}
CloseHandle(this->processInfo.hProcess);
this->processInfo.hProcess = NULL;
}
Process Process::Create(const LaunchInfo& info, const Pipe& pipe)
{
return Create(info, &pipe);
}
Process Process::Create(const LaunchInfo& info, const Pipe* pipe)
{
STARTUPINFO startInfo;
AutoZeroMemory(startInfo);
startInfo.cb = sizeof(STARTUPINFO);
if (pipe != nullptr) {
startInfo.hStdInput = NULL;
startInfo.hStdOutput = pipe->_hWrite;
startInfo.hStdError = pipe->_hWrite;
startInfo.dwFlags |= STARTF_USESTDHANDLES;
}
PROCESS_INFORMATION processInfo;
AutoZeroMemory(processInfo);
auto cmd = info.parameters == nullptr
? va("\"%s\"", info.filename)
: va("\"%s\" %s", info.filename, info.parameters);
BOOL result = CreateProcess(
NULL, // No module name (use command line)
(LPTSTR)cmd.c_str(),// Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
pipe != nullptr, // handles are inherited if we're piping the output
CREATE_NO_WINDOW, // Don't create the console window
NULL, // Use parent's environment block
info.currentDir, // Use parent's starting directory
&startInfo, // Pointer to STARTUPINFO structure
&processInfo // Pointer to PROCESS_INFORMATION structure
);
if (!result)
{
throw std::runtime_error("Unable to create process");
}
return Process(processInfo);
}
BOOL Process::WaitForProcess(DWORD dwMilliseconds)
{
return WaitForSingleObject(this->processInfo.hProcess, dwMilliseconds);
}
DWORD Process::WaitForExit(void)
{
if (!this->processInfo.hProcess) {
throw std::runtime_error("Invalid process handle");
}
DWORD exitCode = 0;
do {
this->WaitForProcess(INFINITE);
if (!GetExitCodeProcess(this->processInfo.hProcess, &exitCode)) {
if (exitCode == STILL_ACTIVE) {
continue;
}
auto msg = va("GetExitCodeProcess() failure: %lu\n", GetLastError());
throw std::runtime_error(msg.c_str());
}
} while (exitCode == STILL_ACTIVE);
return exitCode;
}