-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_process_win32.c
54 lines (45 loc) · 2.03 KB
/
os_process_win32.c
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 <windows.h>
// I would've made a struct for it, but I literally need this for no other reason right now.
// basically, this is system(const char*), but synchronous.
struct os_process_information {
// OS Specific
HANDLE process_handle;
HANDLE thread_handle;
};
// NOTE(jerry): Does not do promise based stuff with callbacks.
struct os_process_information os_process_shell_start(char* shell_command) {
STARTUPINFO startup_information = {};
PROCESS_INFORMATION process_information = {};
{
startup_information.cb = sizeof(STARTUPINFO);
startup_information.hStdError = GetStdHandle(STD_ERROR_HANDLE);
startup_information.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
}
bool create_process_result = CreateProcess(NULL, shell_command, NULL, NULL, false, 0, NULL, NULL, &startup_information, &process_information);
return (struct os_process_information) {
.process_handle = process_information.hProcess,
.thread_handle = process_information.hThread,
};
}
void os_process_await_multiple(struct os_process_information* processes, int* return_codes, size_t count) {
HANDLE handles[4096] = {};
for (size_t process_index = 0; process_index < count; ++process_index) {
handles[process_index] = processes[process_index].process_handle;
}
WaitForMultipleObjects(count, handles, true, INFINITE);
if (return_codes) {
for (size_t process_index = 0; process_index < count; ++process_index) {
if (!GetExitCodeProcess(processes[process_index].process_handle, &return_codes[process_index])) {
// error
}
CloseHandle(processes[process_index].process_handle);
CloseHandle(processes[process_index].thread_handle);
}
}
}
int os_process_shell_start_and_run_synchronously(char* shell_command) {
int return_code = 0;
struct os_process_information process = os_process_shell_start(shell_command);
os_process_await_multiple(&process, &return_code, 1);
return return_code;
}