|
使用CreateProcess()函数8 E6 N: s0 `. O9 [( L/ L0 D
% U# L- ~ M. K3 _. {4 U2 s
以下是一个使用CreateProcess()函数的例子: $ O8 W$ |- m4 M# `/ N
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
" F1 E% N, Z0 j% _( p{
' o, b& E( m! h$ h7 Z' i; SPROCESS_INFORMATION piProcInfo;
8 \/ P+ Q5 r2 a% zSTARTUPINFO siStartInfo; " ]# _3 H' _2 \: v
// Set up members of STARTUPINFO structure.; Q) c+ s0 k/ n- Q2 E' n# ^
siStartInfo.cb = sizeof(STARTUPINFO);
/ c% d$ c- q6 ]* x+ i1 }" {siStartInfo.lpReserved = NULL;/ w) B) V! U3 N8 X6 D* E& }
siStartInfo.lpReserved2 = NULL;
9 @, X9 Q- [, t1 r; }7 `& P# ?siStartInfo.cbReserved2 = 0;
& {5 x6 T; s( A7 g/ a \5 ^ A" NsiStartInfo.lpDesktop = NULL; % E8 q* w1 m1 v0 w1 Z9 W
siStartInfo.dwFlags = 0;
2 C) }/ x* w' Z' i! i5 e0 j. e1 r& y0 w
// Create the child process.
& z2 [/ E' d' `+ [) J& eCreateProcess(
7 @7 U, `9 Q/ U$ a; Y- J, eNULL,3 Z8 h. f$ J1 h
strCmdLine.c_str(),8 M0 t4 l9 O- @) \/ l d H
NULL, // process security attributes
4 ?& q7 }& q! s; ~NULL, // primary thread security attributes
6 G& O9 D2 V# ]4 z5 D% f$ D; ^0, // handles are inherited
& o! P9 _. Y4 }0, // creation flags: c5 R0 O) b1 v+ @; ~6 i4 [
NULL, // use parent's environment
' ^/ s2 L+ o$ q; ^NULL, // use parent's current directory. [5 g. \/ {8 @1 k
&siStartInfo, // STARTUPINFO pointer
( M% `" \! ] e( w% B: ]&piProcInfo); // receives PROCESS_INFORMATION
1 Y; G' t% C; E5 ^// Wait for the processs to finish0 l+ N9 @$ C# J" f2 C
DWORD rc = WaitForSingleObject(
# n5 J8 s& K# C% W* V, qpiProcInfo.hProcess, // process handle' H+ ?) a2 i7 t; a, ?- {8 l
INFINITE);
6 x4 q1 H4 Z' C) r! j}: s! S. V) h# {( X9 t
E1 Z, H( U! `* X
|