1 |
/* $Id: utils_proc.c,v 1.3 2006/06/16 00:31:50 joko Exp $ */ |
2 |
|
3 |
#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) { |
10 |
|
11 |
STARTUPINFO si; |
12 |
PROCESS_INFORMATION pi; |
13 |
BOOL proc_ok; |
14 |
|
15 |
//printf("start_process: '%s'\n", filename); |
16 |
|
17 |
// initialize structures |
18 |
ZeroMemory(&si,sizeof(si)); |
19 |
si.cb = sizeof(STARTUPINFO); |
20 |
ZeroMemory(&pi,sizeof(pi)); |
21 |
|
22 |
// actually do something now... |
23 |
proc_ok = CreateProcess( |
24 |
NULL, filename, |
25 |
NULL, NULL, FALSE, 0, |
26 |
NULL, NULL, |
27 |
&si, |
28 |
&pi |
29 |
); |
30 |
|
31 |
// did something fail? |
32 |
if (!proc_ok) { |
33 |
check_alert_error("CreateProcess"); |
34 |
return FALSE; |
35 |
} |
36 |
|
37 |
// 1. background mode |
38 |
if (background) { |
39 |
// TODO: background process bookkeeping |
40 |
plist[plist_count] = pi.hProcess; |
41 |
plist_count++; |
42 |
|
43 |
// 2. normal (blocking) mode |
44 |
} else { |
45 |
long status; |
46 |
status = WaitForSingleObject(pi.hProcess, INFINITE); |
47 |
|
48 |
// close process handle |
49 |
CloseHandle(pi.hProcess); |
50 |
|
51 |
// check for and handle errors |
52 |
if (status == WAIT_FAILED) { |
53 |
check_alert_error("WaitForSingleObject"); |
54 |
return FALSE; |
55 |
} |
56 |
} |
57 |
|
58 |
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 |
//printf("close process handles\n"); |
76 |
hProcess = plist[i]; |
77 |
CloseHandle(hProcess); |
78 |
} |
79 |
|
80 |
// reset process counter |
81 |
plist_count = 0; |
82 |
|
83 |
// check for and handle errors |
84 |
if (status == WAIT_FAILED) { |
85 |
check_alert_error("WaitForMultipleObjects"); |
86 |
return FALSE; |
87 |
} |
88 |
|
89 |
return TRUE; |
90 |
} |