forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.cpp
More file actions
212 lines (182 loc) · 5.09 KB
/
Copy pathProcess.cpp
File metadata and controls
212 lines (182 loc) · 5.09 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
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <string>
#include <vector>
#include "Win32/Win32Lib.h"
#include "Win32/Process.h"
#include <filesystem>
namespace fusion {
namespace Win32 {
std::wstring GetModuleFilename()
{
std::vector<wchar_t> data(260);
::GetModuleFileName(nullptr, data.data(), static_cast<DWORD>(data.size()));
return std::filesystem::canonical(data.data());
}
std::wstring GetExecutionPath()
{
return std::filesystem::absolute(Win32::GetModuleFilename()).remove_filename();
}
// unspoofable, but in \Device\HarddiskVolume4\project\DebugViewPP\Debug\DebugView++.exe form
std::wstring GetModuleFilenameUnspoofable()
{
std::vector<wchar_t> data(260);
auto filename = GetModuleFilename();
GetMappedFileNameW(GetCurrentProcess(), filename.data(), data.data(), static_cast<DWORD>(data.size()));
return data.data();
}
Process::Process(const std::wstring& pathName, const std::vector<std::wstring>& args)
{
std::wstring commandLine;
auto it = args.begin();
while (it != args.end())
{
if (it != args.begin())
{
commandLine += L" ";
}
commandLine += *it;
++it;
}
Run(pathName, commandLine);
}
Process::Process(const std::wstring& pathName, const std::wstring& args)
{
Run(pathName, args);
}
void Process::Run(const std::wstring& pathName, const std::wstring& args)
{
auto pos = pathName.find_last_of(L"\\/:");
m_name = pos != std::wstring::npos ? pathName.substr(pos + 1) : pathName;
std::wstring commandLine;
commandLine += L"\"";
commandLine += pathName;
commandLine += L"\"";
if (!args.empty())
{
commandLine += L" ";
commandLine += args;
}
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = 1;
saAttr.lpSecurityDescriptor = nullptr;
HANDLE stdInRd;
HANDLE stdInWr;
if (CreatePipe(&stdInRd, &stdInWr, &saAttr, 0) == 0)
{
ThrowLastError("CreatePipe");
}
Handle stdInRd2(stdInRd);
m_stdIn.reset(stdInWr);
if (SetHandleInformation(stdInWr, HANDLE_FLAG_INHERIT, 0) == 0)
{
ThrowLastError("SetHandleInformation stdInWr");
}
HANDLE stdOutRd;
HANDLE stdOutWr;
if (CreatePipe(&stdOutRd, &stdOutWr, &saAttr, 0) == 0)
{
ThrowLastError("CreatePipe");
}
Handle stdOutWr2(stdOutWr);
m_stdOut.reset(stdOutRd);
if (SetHandleInformation(stdOutRd, HANDLE_FLAG_INHERIT, 0) == 0)
{
ThrowLastError("SetHandleInformation stdOutRd");
}
HANDLE stdErrRd;
HANDLE stdErrWr;
if (CreatePipe(&stdErrRd, &stdErrWr, &saAttr, 0) == 0)
{
ThrowLastError("CreatePipe");
}
Handle stdErrWr2(stdErrWr);
m_stdErr.reset(stdErrRd);
if (SetHandleInformation(stdErrRd, HANDLE_FLAG_INHERIT, 0) == 0)
{
ThrowLastError("SetHandleInformation stdErrRd");
}
STARTUPINFO startupInfo;
startupInfo.cb = sizeof(startupInfo);
startupInfo.lpReserved = nullptr;
startupInfo.lpDesktop = nullptr;
startupInfo.lpTitle = nullptr;
startupInfo.dwX = 0;
startupInfo.dwY = 0;
startupInfo.dwXSize = 0;
startupInfo.dwYSize = 0;
startupInfo.dwXCountChars = 0;
startupInfo.dwYCountChars = 0;
startupInfo.dwFillAttribute = 0;
startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startupInfo.wShowWindow = SW_HIDE;
startupInfo.cbReserved2 = 0;
startupInfo.lpReserved2 = nullptr;
startupInfo.hStdInput = stdInRd2.get();
startupInfo.hStdOutput = stdOutWr2.get();
startupInfo.hStdError = stdErrWr2.get();
PROCESS_INFORMATION processInformation;
if (!CreateProcess(
nullptr,
commandLine.data(),
nullptr,
nullptr,
1,
0,
nullptr,
nullptr,
&startupInfo,
&processInformation))
{
ThrowLastError("CreateProcess");
}
m_hProcess.reset(processInformation.hProcess);
m_hThread.reset(processInformation.hThread);
m_processId = processInformation.dwProcessId;
m_threadId = processInformation.dwThreadId;
}
std::wstring Process::GetName() const
{
return m_name;
}
HANDLE Process::GetStdIn() const
{
return m_stdIn.get();
}
HANDLE Process::GetStdOut() const
{
return m_stdOut.get();
}
HANDLE Process::GetStdErr() const
{
return m_stdErr.get();
}
HANDLE Process::GetProcessHandle() const
{
return m_hProcess.get();
}
HANDLE Process::GetThreadHandle() const
{
return m_hThread.get();
}
unsigned long Process::GetProcessId() const
{
return m_processId;
}
unsigned long Process::GetThreadId() const
{
return m_threadId;
}
bool Process::IsRunning() const // todo: check advantages of this against bool win32::IsProcessRunning(HANDLE handle);
{
return GetExitCodeProcess(m_hProcess) == STILL_ACTIVE;
}
void Process::Wait() const
{
WaitForSingleObject(m_hProcess);
}
} // namespace Win32
} // namespace fusion