forked from JonathanMeans/EvilC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_windows.cpp
63 lines (53 loc) · 2.08 KB
/
platform_windows.cpp
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
#if defined(WIN32) || defined(_WIN32)
#include <Windows.h>
#include <numeric>
#include <vector>
#include "platform.h"
PlatformWindows::PlatformWindows() : PlatformBase("a.exe")
{
}
void PlatformWindows::runProgram(
const std::string& programName,
const std::vector<std::string>& arguments) const
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
auto appendArgument = [](const std::string& initial,
const std::string& appending) {
return initial + " " + appending;
};
std::string fullArgumentString = std::accumulate(arguments.begin(),
arguments.end(),
programName,
appendArgument);
std::vector<char> windowsArguments(fullArgumentString.size() + 1);
strcpy_s(windowsArguments.data(),
windowsArguments.size(),
fullArgumentString.c_str());
windowsArguments[windowsArguments.size() - 1] = NULL;
// start the program up
if (!CreateProcessA(NULL,
windowsArguments.data(), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NEW_CONSOLE, // Opens file in a separate console
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
))
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
#endif