|
使用CreateProcess()函数
' `: P9 D# Y3 P/ Q% i ) Y" c! U, G R: O. ]& q
以下是一个使用CreateProcess()函数的例子: 6 |* p2 Y" t" `: S. \
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
' r: E7 f7 a% t- m. U7 y{
6 o+ \+ s- J4 y! @PROCESS_INFORMATION piProcInfo;
9 E9 i$ {! v" W% wSTARTUPINFO siStartInfo; / c" o4 e9 y$ |
// Set up members of STARTUPINFO structure.
% _. ~' r2 T1 \/ R& f9 EsiStartInfo.cb = sizeof(STARTUPINFO);
% Z* K: y. J6 \5 a/ l0 VsiStartInfo.lpReserved = NULL;) }2 d8 M& O: `2 q) }9 V
siStartInfo.lpReserved2 = NULL;
4 }, z* B& |# {6 ]- P4 @8 J" U' Z( _siStartInfo.cbReserved2 = 0;" n$ }! q: P+ r8 S. C% P3 D
siStartInfo.lpDesktop = NULL;
) d* \; N$ D* c( J* e' f3 _6 ysiStartInfo.dwFlags = 0; $ T* | d$ w; {, V1 T, D
m' [; e1 `& X7 s- ]$ Z. p// Create the child process.9 q& \; U; M% u! L3 D' l0 d7 @
CreateProcess( : F! w n' U" Y8 H# K5 a. \
NULL,
0 z& `, f6 n. P! [, o6 tstrCmdLine.c_str(),- g: t# E! T; b3 q6 k
NULL, // process security attributes
; Z- Z; _/ H% V) y' NNULL, // primary thread security attributes
6 [) `4 u! G* k0, // handles are inherited K; p' y5 z* o6 A. y9 T0 h
0, // creation flags9 b% l5 k+ ~2 g0 B
NULL, // use parent's environment
3 N; E3 ]7 u0 FNULL, // use parent's current directory
/ U3 o& R: d f3 j&siStartInfo, // STARTUPINFO pointer1 f2 q6 q+ w+ j3 b6 a5 @
&piProcInfo); // receives PROCESS_INFORMATION 5 r$ X9 G0 ]' V( Q
// Wait for the processs to finish) x Z5 v3 Y3 [
DWORD rc = WaitForSingleObject(' X5 A3 p9 Y* M0 O. I8 a
piProcInfo.hProcess, // process handle5 I0 W5 J8 c8 o4 {
INFINITE);
7 @6 L" [6 `2 v! `" y# |+ v}
& S. d. _ |0 n, ?: h, l/ d$ r
( \; e( K! T$ _8 K9 T5 u |