-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProcess_WIN32.cpp
More file actions
54 lines (44 loc) · 1.44 KB
/
Copy pathProcess_WIN32.cpp
File metadata and controls
54 lines (44 loc) · 1.44 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
#include "hacklib/Process.h"
#include <Windows.h>
#include <stdexcept>
int hl::Process::join()
{
if (!m_id)
{
throw std::runtime_error("Process is not joinable");
}
if (WaitForSingleObject((HANDLE)m_handle, INFINITE) != WAIT_OBJECT_0)
{
throw std::runtime_error("WaitForSingleObject failed");
}
DWORD exitCode;
if (GetExitCodeProcess((HANDLE)m_handle, &exitCode) == 0)
{
throw std::runtime_error("GetExitCodeProcess failed");
}
m_id = 0;
CloseHandle((HANDLE)m_handle);
return exitCode;
}
hl::Process hl::LaunchProcess(const std::string& command, const std::vector<std::string>& args,
const std::string& initialDirectory)
{
std::string cmdline = command;
for (const auto& arg : args)
{
cmdline += " ";
cmdline += arg;
}
const char* initialDirectoryCStr = initialDirectory.empty() ? NULL : initialDirectory.c_str();
STARTUPINFOA startupInfo = {};
startupInfo.cb = sizeof(startupInfo);
PROCESS_INFORMATION processInfo{};
BOOL result = CreateProcessA(NULL, const_cast<char*>(cmdline.c_str()), NULL, NULL, false, 0, NULL,
initialDirectoryCStr, &startupInfo, &processInfo);
if (!result)
{
throw std::runtime_error("CreateProcess failed");
}
CloseHandle(processInfo.hThread);
return Process(processInfo.dwProcessId, (uintptr_t)processInfo.hProcess);
}