使用CreateProcess()函数+ C, I5 g' X9 c( p# h
: Q( P3 a t `# O3 u* `# s+ P 以下是一个使用CreateProcess()函数的例子: ; d! O0 t& v+ A: Y
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) 7 m" ?& B- `% K* C; m
{ . K# ^8 r& k6 [# h
PROCESS_INFORMATION piProcInfo; ' I/ l* ^4 h* o6 i, x$ P4 S
STARTUPINFO siStartInfo;
% K$ }; @: S( M4 x+ p// Set up members of STARTUPINFO structure.
% j6 s- ]; |7 M- I- A+ m0 ~# LsiStartInfo.cb = sizeof(STARTUPINFO); - p# n/ G! d/ `! P; T( w5 ?
siStartInfo.lpReserved = NULL;
$ p( ^* J1 M/ K! f7 V; @siStartInfo.lpReserved2 = NULL;
Y4 |- w6 A3 zsiStartInfo.cbReserved2 = 0; {" Q% ?+ e. ^/ F' }$ z
siStartInfo.lpDesktop = NULL; : N# g! L* s7 l" g" s
siStartInfo.dwFlags = 0; # t* N+ ^) Z( a3 B0 k
% C! m" B" t* O) _' o! W1 P& N// Create the child process.5 r2 w m( f) Q( U" @
CreateProcess( ! A/ T9 _" R. Y2 ^7 w, U
NULL,$ d5 V R D1 s3 T
strCmdLine.c_str(),$ Y; j" l* M( F4 \' Z
NULL, // process security attributes$ q! T) B" L& g4 w2 n/ B0 R
NULL, // primary thread security attributes4 @ |9 S3 Q4 t9 B" f
0, // handles are inherited
* A1 `# m7 ?1 t+ y* ]% N0, // creation flags. ] I. ^7 d( h$ ]
NULL, // use parent's environment
3 l2 X( U4 ^9 t, ^NULL, // use parent's current directory
- c; g X4 i/ V2 |% J( m&siStartInfo, // STARTUPINFO pointer
- S7 z* H/ {! U, x&piProcInfo); // receives PROCESS_INFORMATION
) h/ X3 P3 \. e4 p- ~+ {// Wait for the processs to finish; q! Y% ?0 q7 c# x$ |
DWORD rc = WaitForSingleObject(# }' i6 @6 _6 v% m7 R$ [) S" H
piProcInfo.hProcess, // process handle
4 v b& @* P: t. \INFINITE);
$ f( Q8 I% G" p! E. a7 a}
. {$ [ T6 k$ l0 D. O$ u3 J. g' j 1 p5 r l9 t# z+ p' w) @
|