|
使用CreateProcess()函数8 l. f6 O4 T. B3 f# X+ i( j9 _
8 `9 O0 R- i9 N/ d3 _, `5 F
以下是一个使用CreateProcess()函数的例子:
% Y! g" ?. A( i9 |void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) 9 Y* G+ p9 M2 c
{
( T* x7 i6 I- P# }6 O" h% d2 H2 P3 ?PROCESS_INFORMATION piProcInfo; / l, I4 w+ Y& V$ H
STARTUPINFO siStartInfo; 6 W6 @$ q6 l: ?; K7 \3 n
// Set up members of STARTUPINFO structure." f% m Z- o+ z# b
siStartInfo.cb = sizeof(STARTUPINFO); - j/ h D8 H1 p9 {8 s' p
siStartInfo.lpReserved = NULL;2 P0 q' `; I( j q3 p: [# n
siStartInfo.lpReserved2 = NULL; - O+ M$ o& n/ Y( { e. R8 g3 r
siStartInfo.cbReserved2 = 0;
! V1 J& K7 h i1 y1 ~+ @siStartInfo.lpDesktop = NULL;
0 G: ? K' R p2 i: c, h) l/ U: g- GsiStartInfo.dwFlags = 0;
# h7 J# Z1 f3 G- c0 D% R: g W2 L# M3 l$ Z
// Create the child process.! `* m' V" q% U3 E7 r
CreateProcess(
: h2 v+ B, s% i% R* f3 e# ENULL,
& M l( Z( O) @strCmdLine.c_str(),
0 {' W/ E0 C! y5 E8 ^NULL, // process security attributes
" L5 a/ T) x, A7 `. hNULL, // primary thread security attributes
/ B8 V# U) P8 u( {; t0, // handles are inherited
! k) c) P6 G* a" x0, // creation flags7 j6 p; [% ~) O# X
NULL, // use parent's environment
. ]' i5 T! u3 s% G/ gNULL, // use parent's current directory
8 _/ p" \ B* a1 x4 x! Z2 k&siStartInfo, // STARTUPINFO pointer: U4 I5 `1 u `. ]1 \
&piProcInfo); // receives PROCESS_INFORMATION , G' p. T6 O& H5 c" F; P+ d
// Wait for the processs to finish7 Z( D v H V$ Y; u d2 H. l
DWORD rc = WaitForSingleObject(" n( z& P6 U0 X) }! a6 D
piProcInfo.hProcess, // process handle
4 N, ^% P$ h# r: ?1 u0 d; w$ sINFINITE); # ~) t; E; }( u3 X* ^, p# z' n
}7 j4 c' i' E1 h5 v' m" a2 y( Z
/ }+ e p5 J$ J% \2 d
|