使用CreateProcess()函数
; P; N/ u' x+ g1 B' \
+ Q, {8 U, ^ K0 Q/ {6 W: s9 L 以下是一个使用CreateProcess()函数的例子: 1 `1 l7 y' }7 R, \8 a4 q. T& [4 l
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) 3 f3 @2 `5 \7 O- `3 K
{
2 I# o2 z1 W! l/ I# G! ^PROCESS_INFORMATION piProcInfo;
$ z1 y% z \/ o2 R) R; s/ J* w9 ySTARTUPINFO siStartInfo;
# {9 \7 c2 u2 }! p& \// Set up members of STARTUPINFO structure.
8 V- K9 P+ L8 q/ W0 ^9 n/ X9 AsiStartInfo.cb = sizeof(STARTUPINFO); ) ~+ ~' ^ p: r! ?
siStartInfo.lpReserved = NULL;
* s9 v. c. ]' v0 o$ h( L" N4 ^siStartInfo.lpReserved2 = NULL;
1 n. ~$ D" I! \5 }+ QsiStartInfo.cbReserved2 = 0;
3 Y# f! r$ a6 g; rsiStartInfo.lpDesktop = NULL; , g! Q5 D( g" T( Z. K: x5 |
siStartInfo.dwFlags = 0;
& o/ {2 ], D7 Q: x: V7 a# c/ M3 A: s" P5 j& j6 M5 d1 ~- F1 o* M
// Create the child process.1 `/ {+ `$ b, m2 z
CreateProcess( + Y, @! t% e, F" _0 F- }, J4 r
NULL,' o# P4 G- j- t) p: |1 [
strCmdLine.c_str(), H- Z7 L) A- T7 ?" O
NULL, // process security attributes; J9 ?. |1 v4 C! p/ ]' b: k A
NULL, // primary thread security attributes
8 d* j1 o, p4 R# V6 A0 j$ X* \' u0, // handles are inherited5 P' t% T* G8 @4 A6 w4 P$ \
0, // creation flags
4 t7 ^; H% `/ O6 u' INULL, // use parent's environment* h3 U! @1 B% o
NULL, // use parent's current directory
. \& I) k* t$ B% B$ W0 ?&siStartInfo, // STARTUPINFO pointer
# ^9 O8 P; i8 G& G9 p* `" i' f&piProcInfo); // receives PROCESS_INFORMATION
! {5 q8 W: P' r% F g/ R// Wait for the processs to finish
; b- o! Q, h: [1 d6 {DWORD rc = WaitForSingleObject(
/ n7 e1 n; Q8 T7 l9 IpiProcInfo.hProcess, // process handle/ K- t6 w3 k% f( u, o7 R& g T
INFINITE);
4 R4 ?& s! h- {; ^1 h}" a) n# I: @: r* y( [' Z2 N
1 ]; z* D% Z) B, B |