|
使用CreateProcess()函数8 C) B5 o7 Z+ ] j/ ~4 Y' I
) ]4 W. S5 B" Y7 ?; D
以下是一个使用CreateProcess()函数的例子: 3 h6 R# t3 e6 L8 ~% Z$ b9 O
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) - ?1 f( s- Q t d3 F
{ 9 m) }- ?; e, t
PROCESS_INFORMATION piProcInfo;
2 R4 r' `$ V7 D" ESTARTUPINFO siStartInfo; ! q' s* [- N" {3 p$ e4 N
// Set up members of STARTUPINFO structure.
# M9 ^+ k4 B' B3 ~siStartInfo.cb = sizeof(STARTUPINFO); " ]* G# p% X2 K/ L4 V# D
siStartInfo.lpReserved = NULL;
4 G* Q- v$ n `4 w$ S7 j3 j X+ ^siStartInfo.lpReserved2 = NULL; 9 n3 r" y9 g9 _
siStartInfo.cbReserved2 = 0;
9 I4 h% W0 L, W1 e1 Y% WsiStartInfo.lpDesktop = NULL; & }' \" d6 {/ w- B4 U3 ^
siStartInfo.dwFlags = 0; ; o w1 W2 U- f. d6 ]
1 g0 W- S/ n$ v: I, E, y
// Create the child process.
1 A l4 k/ h5 l( ICreateProcess(
4 }9 x! S3 D9 q; t0 PNULL,
% y [7 H' r4 {6 a3 @5 X1 ZstrCmdLine.c_str(),
$ U5 p$ n3 o" t lNULL, // process security attributes
# `) g( Q+ U$ o! _& n6 }) gNULL, // primary thread security attributes) J2 A6 d9 j4 A" C3 b3 u9 ]" @
0, // handles are inherited% L* l% W# |! g P; J+ \4 ^
0, // creation flags
% h2 T @+ r/ f9 @- \* j% S, \! hNULL, // use parent's environment" O" g/ |, F0 \, k
NULL, // use parent's current directory4 t- {) ^% W% \3 C s4 @
&siStartInfo, // STARTUPINFO pointer$ [$ E0 Y- ]4 d }: b( U
&piProcInfo); // receives PROCESS_INFORMATION 9 O9 A" B0 R) h: u9 E5 L
// Wait for the processs to finish4 S; r1 Y( J7 J+ b; {0 U
DWORD rc = WaitForSingleObject(
9 G O3 U+ S% u/ OpiProcInfo.hProcess, // process handle
( T, L' p) K: K* S2 U- Q9 X/ R& r1 VINFINITE); ! a0 d+ s3 D; S) l b* a
}- H# r1 m+ r& c8 o; ^
3 w* R1 o* _" T" }5 I9 Q
|