使用CreateProcess()函数
' p- V& Z) c& Q% s, g- [
6 B. e3 Y( e3 ^; l" i 以下是一个使用CreateProcess()函数的例子: ) q9 h: v' ]2 i0 r* T
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
) \4 y0 Y% j( m2 E9 g{
2 o8 n( f3 \' }% N, yPROCESS_INFORMATION piProcInfo; / n, D8 S& e! Z
STARTUPINFO siStartInfo; " `, S" o$ H8 |0 C
// Set up members of STARTUPINFO structure., f7 `8 ?8 |+ b( c O
siStartInfo.cb = sizeof(STARTUPINFO); 7 a3 B! X. `* |# s8 e
siStartInfo.lpReserved = NULL; J& g" J, J& t+ E
siStartInfo.lpReserved2 = NULL; ) T7 p' {4 b9 I, V' }# m& R8 d8 ~
siStartInfo.cbReserved2 = 0;
7 O& W8 h1 M1 {siStartInfo.lpDesktop = NULL; ! g" Z( a& b+ W% ~- U
siStartInfo.dwFlags = 0; % q1 N( \. h& l! K
! j& r( q/ s7 m6 S% y6 ]% q+ @
// Create the child process.: Z) F8 a# ] T. e& g' e
CreateProcess( ) `) D$ ]& H% f W9 G' e- Y( h6 f
NULL,. v& T, r3 y% z( g) ^
strCmdLine.c_str(),
0 C' x8 g" c/ w% M$ P' aNULL, // process security attributes
: F$ b. u& e9 A1 H' @' I. z0 W _NULL, // primary thread security attributes
! j4 H& u/ _& h" @ X! ^3 D7 z% i0, // handles are inherited
* K$ H% ]1 Q% X% G0, // creation flags
; O: S" Y; o- }' B7 aNULL, // use parent's environment
" ?5 g6 d1 M9 t g) Z! r0 RNULL, // use parent's current directory
9 l. } Q$ m9 ]( V&siStartInfo, // STARTUPINFO pointer7 V! e0 C) R$ x
&piProcInfo); // receives PROCESS_INFORMATION
9 _: }! e- O8 q# D/ e6 e( b1 l& O// Wait for the processs to finish
+ c. i( _, _ ?/ ~2 RDWORD rc = WaitForSingleObject(
* C, W) F0 Q' v$ p) d8 S# T' XpiProcInfo.hProcess, // process handle" W$ U' o) q( h
INFINITE); 5 w& H5 @, w' ^1 ]5 S- W
}
4 X4 |3 w- z" ^0 u2 d# u) ~ * _) L+ y1 J- I% ]
|