|
使用CreateProcess()函数
- K- }( `6 D8 d& K - s. I$ o9 {4 G% w
以下是一个使用CreateProcess()函数的例子: ( _: w* H/ n$ i" z9 e( O, h
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
2 A, E% U n2 w2 i6 _{
. P J3 _. ^: C+ r$ v0 X9 z; F! xPROCESS_INFORMATION piProcInfo;
6 Z! D7 t6 W+ ^/ S5 w' o/ ]STARTUPINFO siStartInfo; & z2 b' k ?, y; K1 l d
// Set up members of STARTUPINFO structure.7 q: c" @/ @6 H: r" Z
siStartInfo.cb = sizeof(STARTUPINFO);
* M' _) Y* J l. t; L- G1 S) osiStartInfo.lpReserved = NULL;
# I8 \0 z2 t/ A6 k8 n. H% R. WsiStartInfo.lpReserved2 = NULL; " x) s- n: N" g# ]( I" t. ^: Z8 v
siStartInfo.cbReserved2 = 0;
: j* Z a1 u! asiStartInfo.lpDesktop = NULL; 8 P! N6 n% S7 R8 \+ I8 C
siStartInfo.dwFlags = 0; 0 D9 P+ H6 Z5 M2 q3 u5 K5 W/ \
" T$ P1 S% k5 K4 }8 [# H
// Create the child process.
8 B* N: \6 l3 I+ g/ `, ]CreateProcess(
. q; S" Q- O& N+ f8 @ WNULL,+ z: C4 p2 g4 U( j& m6 y2 v; R
strCmdLine.c_str(),% o3 I% r) p/ r3 z! Z
NULL, // process security attributes
+ c; w. X' m5 e$ K9 O! TNULL, // primary thread security attributes
- d E) D0 c4 P% R0, // handles are inherited
+ G0 J. h# e) V$ }5 M0, // creation flags
1 W O2 v- _+ I0 k' F8 NNULL, // use parent's environment
5 @9 |4 f# I" e6 T0 DNULL, // use parent's current directory* h! t: f# n2 G+ N
&siStartInfo, // STARTUPINFO pointer% X" H8 w- C' |4 ~) j4 J
&piProcInfo); // receives PROCESS_INFORMATION
& H* E M; [8 e( t' K// Wait for the processs to finish
5 o9 n5 C8 C- M2 w. e3 ~DWORD rc = WaitForSingleObject( c: d0 U4 b& e6 j; k$ u% }, z @
piProcInfo.hProcess, // process handle6 w u4 d1 G; a" y& E5 ]
INFINITE);
8 B0 g* K; X& {- N+ u}6 v( |/ U2 Z" E2 x3 c+ W; z
; V/ D6 C7 }" a- D$ F |