|
使用CreateProcess()函数5 v1 t$ E7 F" o( M( q6 `3 Z; P
! b& c( F: i# f, f4 G: b 以下是一个使用CreateProcess()函数的例子: & ^$ Q# V) a9 L. p
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) R9 C0 S( {8 ?2 K
{
" r+ Y6 z _. A3 ^PROCESS_INFORMATION piProcInfo; 2 \2 l' L9 V- n% J( W6 x
STARTUPINFO siStartInfo;
7 N0 R- z6 {: y# f9 ~// Set up members of STARTUPINFO structure.
: }) X1 k" g# u+ t- ZsiStartInfo.cb = sizeof(STARTUPINFO);
& O$ Q3 o1 \) U8 X/ N4 \siStartInfo.lpReserved = NULL;
8 a6 V# O3 E) s" ]siStartInfo.lpReserved2 = NULL; 8 a' x) W0 n3 D/ n
siStartInfo.cbReserved2 = 0;$ r& ^% n, c' z7 L: Z k
siStartInfo.lpDesktop = NULL;
$ k4 \1 Y# L. W- K5 j% c5 ]1 fsiStartInfo.dwFlags = 0; 8 o# a1 S9 ?8 a& m
0 h3 L( A) [1 L* ^6 c* I// Create the child process.( \% n2 P* m' o( s
CreateProcess(
* k9 L+ |) J! `: _2 V0 n4 B' T% g: xNULL,* q) b! j J# F# J
strCmdLine.c_str(),0 Z6 E( w* K3 e; O
NULL, // process security attributes E' ^: X0 r" o! `
NULL, // primary thread security attributes
4 a- y4 S# U/ P/ ~0, // handles are inherited
3 g9 U! S8 T# z# t+ R0, // creation flags4 s/ C% k9 d- U3 O$ v$ ]! V
NULL, // use parent's environment
3 C0 a( S% h* |. E- x* @; t6 |NULL, // use parent's current directory
% e/ B7 b% @0 I3 q7 K6 s* m- B&siStartInfo, // STARTUPINFO pointer
: X y4 q4 j- U8 t&piProcInfo); // receives PROCESS_INFORMATION
, d- Q8 e2 U: w( {) v// Wait for the processs to finish
% P, H P% ~# |3 Q) A" P$ t/ T8 sDWORD rc = WaitForSingleObject(1 r' _, y G) W: }! t
piProcInfo.hProcess, // process handle7 D4 a8 O: B, S/ Z" H; Y3 U$ y! d
INFINITE);
' d7 G( G" ], H}
7 S# S2 ], ?6 R- ^6 V" Y
' ]. \5 j: c6 O; B: _8 V |