2 |
|
|
3 |
#include <windows.h> |
#include <windows.h> |
4 |
|
|
5 |
|
// background process bookkeeping |
6 |
|
HANDLE plist[MAXIMUM_WAIT_OBJECTS-1]; |
7 |
|
int plist_count = 0; |
8 |
|
|
9 |
BOOL os_start_process(char * filename, BOOL background) { |
BOOL os_start_process(char * filename, BOOL background) { |
10 |
|
|
11 |
STARTUPINFO si; |
STARTUPINFO si; |
12 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
13 |
|
BOOL proc_ok; |
14 |
|
|
15 |
//printf("start_process: '%s'\n", filename); |
//printf("start_process: '%s'\n", filename); |
16 |
|
|
20 |
ZeroMemory(&pi,sizeof(pi)); |
ZeroMemory(&pi,sizeof(pi)); |
21 |
|
|
22 |
// actually do something now... |
// actually do something now... |
23 |
CreateProcess( |
proc_ok = CreateProcess( |
24 |
NULL, filename, |
NULL, filename, |
25 |
NULL, NULL, FALSE, 0, |
NULL, NULL, FALSE, 0, |
26 |
NULL, NULL, |
NULL, NULL, |
27 |
&si, |
&si, |
28 |
&pi |
&pi |
29 |
); |
); |
30 |
check_alert_error("CreateProcess"); |
|
31 |
|
// did something fail? |
32 |
|
if (!proc_ok) { |
33 |
|
check_alert_error("CreateProcess"); |
34 |
|
return FALSE; |
35 |
|
} |
36 |
|
|
37 |
// 1. background mode |
// 1. background mode |
38 |
if (background) { |
if (background) { |
39 |
// TODO: background process bookkeeping |
// background process bookkeeping |
40 |
|
plist[plist_count] = pi.hProcess; |
41 |
|
plist_count++; |
42 |
|
|
43 |
// 2. normal (blocking) mode |
// 2. normal (blocking) mode |
44 |
} else { |
} else { |
45 |
long status; |
long status; |
46 |
status = WaitForSingleObject(pi.hProcess, INFINITE); |
status = WaitForSingleObject(pi.hProcess, INFINITE); |
47 |
|
|
48 |
// close process and thread handles. |
// close process handle |
49 |
CloseHandle(pi.hProcess); |
CloseHandle(pi.hProcess); |
|
CloseHandle(pi.hThread); |
|
50 |
|
|
51 |
// check for and handle errors |
// check for and handle errors |
52 |
if (status == WAIT_FAILED) { |
if (status == WAIT_FAILED) { |
58 |
return TRUE; |
return TRUE; |
59 |
|
|
60 |
} |
} |
61 |
|
|
62 |
|
BOOL os_wait_for_processes() { |
63 |
|
|
64 |
|
long status; |
65 |
|
int i; |
66 |
|
HANDLE hProcess; |
67 |
|
|
68 |
|
if (plist_count == 0) |
69 |
|
return TRUE; |
70 |
|
|
71 |
|
status = WaitForMultipleObjects(plist_count, plist, TRUE, INFINITE); |
72 |
|
|
73 |
|
// close process handles |
74 |
|
for (i = 0; i < plist_count; i++) { |
75 |
|
hProcess = plist[i]; |
76 |
|
CloseHandle(hProcess); |
77 |
|
} |
78 |
|
|
79 |
|
// reset process counter |
80 |
|
plist_count = 0; |
81 |
|
|
82 |
|
// check for and handle errors |
83 |
|
if (status == WAIT_FAILED) { |
84 |
|
check_alert_error("WaitForMultipleObjects"); |
85 |
|
return FALSE; |
86 |
|
} |
87 |
|
|
88 |
|
return TRUE; |
89 |
|
} |