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
|
#include "ProcessUtils.h"
#include <Windows.h>
#include <tchar.h>
#include <vector>
// TODO
// Support .lnk files
namespace {
void Clear(SubProcessData* spd)
{
spd->hOutput = NULL;
spd->hInput = NULL;
spd->pi.hThread = NULL;
spd->pi.hProcess = NULL;
spd->hPC = NULL;
}
SubProcessData CreateSubProcess(LPCTSTR cmd, LPCTSTR wdir, COORD size, HANDLE hInput, HANDLE hOutput, bool bUseConPty)
{
SubProcessData spd = {};
DWORD dwCreationFlags = EXTENDED_STARTUPINFO_PRESENT;
STARTUPINFOEX si = {};
std::vector<BYTE> attrList;
si.StartupInfo.cb = sizeof(si);
BOOL bInheritHandles = FALSE;
if (bUseConPty)
{
spd.hr = CreatePseudoConsole(
size,
hInput,
hOutput,
0,
&spd.hPC);
if (spd.hr != S_OK)
{
CleanupSubProcess(&spd);
Clear(&spd);
return spd;
}
SIZE_T size = 0;
if (InitializeProcThreadAttributeList(NULL, 1, 0, &size))
{
spd.hr = HRESULT_FROM_WIN32(GetLastError());
CleanupSubProcess(&spd);
Clear(&spd);
return spd;
}
attrList.resize(size);
si.lpAttributeList = reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(attrList.data());
if (!InitializeProcThreadAttributeList(si.lpAttributeList, 1, 0, &size))
{
spd.hr = HRESULT_FROM_WIN32(GetLastError());
CleanupSubProcess(&spd);
Clear(&spd);
return spd;
}
if (!UpdateProcThreadAttribute(
si.lpAttributeList,
0,
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
spd.hPC, sizeof(HPCON),
NULL,
NULL))
{
spd.hr = HRESULT_FROM_WIN32(GetLastError());
CleanupSubProcess(&spd);
Clear(&spd);
return spd;
}
}
else
{
si.StartupInfo.dwXSize = size.X;
si.StartupInfo.dwYSize = size.Y;
si.StartupInfo.dwFlags = STARTF_USESIZE | STARTF_USESTDHANDLES;
si.StartupInfo.hStdInput = hInput;
si.StartupInfo.hStdOutput = hOutput;
si.StartupInfo.hStdError = si.StartupInfo.hStdOutput;
dwCreationFlags |= CREATE_NO_WINDOW;
bInheritHandles = TRUE;
}
TCHAR localcmd[MAX_PATH];
TCHAR localwdir[MAX_PATH];
_tcscpy_s(localcmd, cmd);
if(wdir != nullptr)
_tcscpy_s(localwdir, wdir);
if (!CreateProcess(nullptr, localcmd, nullptr, nullptr, bInheritHandles, dwCreationFlags, nullptr, wdir == nullptr ? nullptr : localwdir, &si.StartupInfo, &spd.pi))
{
spd.hr = HRESULT_FROM_WIN32(GetLastError());
CleanupSubProcess(&spd);
Clear(&spd);
return spd;
}
spd.hr = S_OK;
return spd;
}
}
SubProcessData CreateSubProcess(LPCTSTR cmd, LPCTSTR wdir, COORD size, bool bUseConPty)
{
SubProcessData spd = {};
SECURITY_ATTRIBUTES sa = {};
sa.bInheritHandle = !bUseConPty;
HANDLE hReadPipeInput = NULL;
HANDLE hWritePipeInput = NULL;
if (spd.hr == S_OK && !CreatePipe(&hReadPipeInput, &hWritePipeInput, &sa, 0))
spd.hr = HRESULT_FROM_WIN32(GetLastError());
HANDLE hWritePipeOutput = NULL;
HANDLE hReadPipeOutput = NULL;
if (spd.hr == S_OK && !CreatePipe(&hReadPipeOutput, &hWritePipeOutput, &sa, 0))
spd.hr = HRESULT_FROM_WIN32(GetLastError());
if (spd.hr == S_OK)
spd = CreateSubProcess(cmd, wdir, size, hReadPipeInput, hWritePipeOutput, bUseConPty);
if (spd.hr == S_OK)
{
spd.hInput = hWritePipeInput;
spd.hOutput = hReadPipeOutput;
}
else
{
CloseHandle(hWritePipeInput);
CloseHandle(hReadPipeOutput);
}
CloseHandle(hReadPipeInput);
CloseHandle(hWritePipeOutput);
return spd;
}
void CleanupSubProcess(const SubProcessData* spd)
{
CloseHandle(spd->hOutput);
CloseHandle(spd->hInput);
TerminateProcess(spd->pi.hProcess, 0xFF);
CloseHandle(spd->pi.hThread);
CloseHandle(spd->pi.hProcess);
ClosePseudoConsole(spd->hPC);
}
UINT GetIcon(const SubProcessData* spd, HICON *phIconLarge, HICON *phIconSmall)
{
TCHAR exe[MAX_PATH];
DWORD exelen = ARRAYSIZE(exe);
QueryFullProcessImageName(spd->pi.hProcess, 0, exe, &exelen);
WORD iIcon = 0;
return ExtractIconEx(exe, 0, phIconLarge, phIconSmall, 1);
}
|