|
使用CreateProcess()函数1 Q! d- h a: X8 K. t8 _
) C% D8 ]- L7 ~( P" {2 Q: m
以下是一个使用CreateProcess()函数的例子:
9 m2 _- a4 ?2 \% Y, r3 evoid CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
?6 ~+ b0 C, b: V C# r. A{
- C: `' z: y) |4 kPROCESS_INFORMATION piProcInfo; ' c$ V; m- k, H& r7 C% t
STARTUPINFO siStartInfo;
0 C% @4 s/ V6 T( K) r( B- }0 y// Set up members of STARTUPINFO structure.
' J+ M# ?2 }# Y3 gsiStartInfo.cb = sizeof(STARTUPINFO); & `9 k+ h" u$ I1 r$ ]! k0 a3 n
siStartInfo.lpReserved = NULL;1 C( x/ k' E a0 W! b% b8 t" a
siStartInfo.lpReserved2 = NULL;
0 E8 N+ ]3 h. O* I+ y8 J# |- AsiStartInfo.cbReserved2 = 0;( N, r U7 d- O) p
siStartInfo.lpDesktop = NULL; 6 U& i% |& R, _1 ~/ \
siStartInfo.dwFlags = 0;
, K3 D+ r5 ^4 U8 b( W# t5 S9 c6 J5 D% D& @. l) t1 j* O
// Create the child process.
; g# ]& n7 \: f$ z4 _CreateProcess( + |6 `. |; d# h6 z7 ?9 y
NULL,9 p- k) @, D% H6 u& B7 m/ v/ N
strCmdLine.c_str(),( g$ A1 ^5 z% L. c: x
NULL, // process security attributes f+ O- ?6 s4 V( P, _' a
NULL, // primary thread security attributes* T g3 J" |) s- o
0, // handles are inherited
0 _. Q9 F1 `7 S! D% x6 ^0, // creation flags2 G9 H$ C* a* v: t% z
NULL, // use parent's environment- o! U: A. E7 g, @$ ^. m8 j- ^
NULL, // use parent's current directory9 y8 S+ W! u2 E
&siStartInfo, // STARTUPINFO pointer
4 p" }( y: V# @$ a&piProcInfo); // receives PROCESS_INFORMATION
/ t) N8 L$ z+ \& H9 _/ m// Wait for the processs to finish
; ^, W7 T$ z' E7 `8 V; U! v" T1 xDWORD rc = WaitForSingleObject(: z6 n2 z8 i% k1 \
piProcInfo.hProcess, // process handle
# L7 ]( ?- Y9 P/ g, A, y6 oINFINITE);
6 p+ {3 U$ q. {1 _3 \# D( M}/ H5 Y/ [7 E$ j0 u
+ z8 g4 t. m" @$ y1 Q6 W |