Repository navigation
Expand file tree
/
Copy pathProcessRunnerWin.cpp
More file actions
154 lines (134 loc) · 4.54 KB
/
Copy pathProcessRunnerWin.cpp
File metadata and controls
154 lines (134 loc) · 4.54 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
#include "ProcessRunner.hpp"
#include <windows.h>
#include <vector>
#include <iostream>
class ProcessRunnerWin : public IProcessRunner {
private:
HANDLE hProcess = nullptr;
HANDLE hThread = nullptr;
HANDLE hReadPipe = nullptr;
HANDLE hWritePipe = nullptr;
std::string pipeBuffer;
public:
ProcessRunnerWin() = default;
~ProcessRunnerWin() override {
terminate();
}
bool launch(const std::wstring& cmd, std::string& errorMsg) override {
terminate();
SECURITY_ATTRIBUTES saAttr{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE};
if (!CreatePipe(&hReadPipe, &hWritePipe, &saAttr, 0)) {
errorMsg = "Failed to create pipes";
return false;
}
SetHandleInformation(hReadPipe, HANDLE_FLAG_INHERIT, 0);
STARTUPINFOW si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.hStdError = hWritePipe;
si.hStdOutput = hWritePipe;
si.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
std::wstring cmdCopy = cmd;
if (!CreateProcessW(nullptr, cmdCopy.data(), nullptr, nullptr, TRUE,
CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi)) {
errorMsg = "Failed to create process";
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
hReadPipe = nullptr;
hWritePipe = nullptr;
return false;
}
hProcess = pi.hProcess;
hThread = pi.hThread;
CloseHandle(hWritePipe); // Close write end in parent
hWritePipe = nullptr;
return true;
}
bool readLine(std::string& line) override {
if (!hReadPipe) return false;
auto extractLine = [this, &line]() -> bool {
size_t pos = pipeBuffer.find_first_of("\r\n");
if (pos != std::string::npos) {
line = pipeBuffer.substr(0, pos);
char delim = pipeBuffer[pos];
size_t toErase = 1;
if (delim == '\r' && pos + 1 < pipeBuffer.size() && pipeBuffer[pos + 1] == '\n') {
toErase = 2;
}
pipeBuffer.erase(0, pos + toErase);
return true;
}
return false;
};
if (extractLine()) {
return true;
}
while (true) {
char buffer[512];
DWORD bytesRead = 0;
DWORD bytesAvail = 0;
// Non-blocking: Check if there is data in the pipe to avoid blocking
BOOL peekOk = PeekNamedPipe(hReadPipe, nullptr, 0, nullptr, &bytesAvail, nullptr);
if (!peekOk) {
// Pipe might be broken (process exited and closed write handle).
// If so, return any remaining data in pipeBuffer.
if (!pipeBuffer.empty()) {
line = pipeBuffer;
pipeBuffer.clear();
return true;
}
return false;
}
if (bytesAvail == 0) {
return false;
}
DWORD toRead = (bytesAvail < sizeof(buffer) - 1) ? bytesAvail : (sizeof(buffer) - 1);
if (ReadFile(hReadPipe, buffer, toRead, &bytesRead, nullptr) && bytesRead > 0) {
pipeBuffer.append(buffer, bytesRead);
if (extractLine()) {
return true;
}
} else {
return false;
}
}
}
void terminate() override {
if (hProcess) {
TerminateProcess(hProcess, 1);
CloseHandle(hProcess);
hProcess = nullptr;
}
if (hThread) {
CloseHandle(hThread);
hThread = nullptr;
}
if (hReadPipe) {
CloseHandle(hReadPipe);
hReadPipe = nullptr;
}
if (hWritePipe) {
CloseHandle(hWritePipe);
hWritePipe = nullptr;
}
pipeBuffer.clear();
}
int getExitCode() override {
if (!hProcess) return 0;
DWORD exitCode = 0;
WaitForSingleObject(hProcess, INFINITE);
GetExitCodeProcess(hProcess, &exitCode);
return static_cast<int>(exitCode);
}
bool isActive() override {
if (!hProcess) return false;
DWORD exitCode = 0;
GetExitCodeProcess(hProcess, &exitCode);
return exitCode == STILL_ACTIVE;
}
};
std::unique_ptr<IProcessRunner> createProcessRunner() {
return std::make_unique<ProcessRunnerWin>();
}