|
使用CreateProcess()函数6 ]" P7 K% b' W0 _. m3 ^7 t
) c: a7 ~9 t9 \ v. k
以下是一个使用CreateProcess()函数的例子:
& z2 {2 `/ Z8 M: Jvoid CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) ' B6 [0 G& M+ F: H% h. E, d
{
: ^, z! J1 [* a2 K+ ~( z( D1 c" YPROCESS_INFORMATION piProcInfo; , e& N) @9 f7 r2 r! v' ?; \
STARTUPINFO siStartInfo;
' F7 K" w$ t* z) f// Set up members of STARTUPINFO structure.
4 r% b, p" C. t. H1 osiStartInfo.cb = sizeof(STARTUPINFO); & a, M7 e7 M0 J- s* K, t: k$ U
siStartInfo.lpReserved = NULL;
, O+ W# z! h' ^8 W- e/ OsiStartInfo.lpReserved2 = NULL; * P4 u# ]4 k' v
siStartInfo.cbReserved2 = 0;
; S; r3 O: `* _ V Q: ?+ [siStartInfo.lpDesktop = NULL;
2 }% }" | C/ U! MsiStartInfo.dwFlags = 0; 1 T" d y7 \1 [7 Q/ \
: y8 V5 @1 l% O// Create the child process.
/ B% n7 ^* T* b% GCreateProcess(
& P5 N( E" ?+ f8 g/ X3 g" ?NULL,2 w$ [. D1 L3 T$ a8 o. m. c0 z% M$ A
strCmdLine.c_str()," a! V7 q$ b9 M# a- _6 X
NULL, // process security attributes
% R/ O1 N6 p5 C7 _2 pNULL, // primary thread security attributes4 B; j0 _, ?3 \' W L4 \
0, // handles are inherited1 M; l6 [6 Y; F
0, // creation flags- w. ?2 Y3 C$ {& Q! T! o o0 d
NULL, // use parent's environment; t; V! O% B" q7 q s9 y$ t
NULL, // use parent's current directory
5 Y) p. {. \& s2 ^&siStartInfo, // STARTUPINFO pointer0 x }: _. E$ u, f9 C6 E, I9 l
&piProcInfo); // receives PROCESS_INFORMATION 4 ^3 Z( ?3 \+ l) k ]
// Wait for the processs to finish. ^- @. A: D$ X* N1 o( a! b
DWORD rc = WaitForSingleObject(, ^$ v* k8 u4 B7 `7 H* Y
piProcInfo.hProcess, // process handle
1 t/ k& P& V9 x" BINFINITE); 0 l- s) Z z" A5 [6 _. ~9 B( G
}
X7 o2 n% P$ O" Z! ~9 h8 s
X( m: Z# ^9 W& U3 g |