|
使用CreateProcess()函数9 X/ q( n* d& f6 ]# j7 D
5 R# R: {' m7 y3 Q# t
以下是一个使用CreateProcess()函数的例子:
% x) W |. g) w; r/ _void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
5 l/ C4 G, ~+ D) K7 B{ 6 u/ @3 V# e- x9 i8 x( N$ K Y
PROCESS_INFORMATION piProcInfo; ! ]( W+ q0 S' w
STARTUPINFO siStartInfo;
% w4 N2 s7 C3 U* b Z// Set up members of STARTUPINFO structure.- z* z+ w6 j/ H' u- X# M
siStartInfo.cb = sizeof(STARTUPINFO);
" r; L8 V: s$ R- m6 D( O: jsiStartInfo.lpReserved = NULL;
, q, J1 `+ x4 w" n8 AsiStartInfo.lpReserved2 = NULL;
+ H- |3 y- N3 _. [6 `siStartInfo.cbReserved2 = 0;, C2 w: V3 M2 `5 w% l
siStartInfo.lpDesktop = NULL; % i7 {& p, @( R. T- q
siStartInfo.dwFlags = 0; & ?1 H( E# @# b
' J% n3 R' @4 ? @0 p
// Create the child process.5 ^/ }( h* f, B$ z, x/ s* `0 q
CreateProcess( ( p2 E5 ~3 J* k
NULL,
' q! [, ?1 o2 b8 }. u! A* }7 A- U0 G. J CstrCmdLine.c_str(),0 c- ~6 t& X% |& A+ Y# Q4 B
NULL, // process security attributes0 D- A0 g& h( t1 g) P
NULL, // primary thread security attributes
; q0 w' S' j) Q3 P( Q2 l) m. [1 h2 {0, // handles are inherited
9 y! \ ?1 S+ d( z+ A0, // creation flags
+ `8 B7 _7 u* a$ z. PNULL, // use parent's environment# \4 \ L+ D3 G7 }, t7 g
NULL, // use parent's current directory
! q6 u3 ~+ [6 M) y$ {&siStartInfo, // STARTUPINFO pointer
6 n/ j% C& U! _0 z/ q; \" a&piProcInfo); // receives PROCESS_INFORMATION
$ l$ r \/ t7 y8 J// Wait for the processs to finish2 P2 q; V- R1 ^* Y {0 ]+ H
DWORD rc = WaitForSingleObject(8 Q+ U; O( g) p, `( I+ |8 e' S
piProcInfo.hProcess, // process handle. k& d4 p9 O4 r
INFINITE);
4 T8 s' M, G, B) _2 T}( H$ {5 M0 k& Y0 O% j j
" K$ \& S; {! w |