|
使用CreateProcess()函数0 \ S! y I' y
- I6 ~, q' ^8 F, G" A
以下是一个使用CreateProcess()函数的例子:
9 H* a7 {5 _' w9 z9 cvoid CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
; F5 d8 w2 {6 f# _. D{ 6 t1 W' n4 z- g( ]+ D
PROCESS_INFORMATION piProcInfo; / `0 B. i6 |0 c! z
STARTUPINFO siStartInfo;
3 W+ S' @7 ?) R8 T5 F// Set up members of STARTUPINFO structure.
0 m! ^ Q0 ?7 O8 U2 s1 s# c3 T9 esiStartInfo.cb = sizeof(STARTUPINFO); 0 U0 y" w; t1 [9 C' W! [
siStartInfo.lpReserved = NULL;
1 d" J5 l' J: p" i) q6 xsiStartInfo.lpReserved2 = NULL;
4 z) h; `' Q7 v7 h) ?siStartInfo.cbReserved2 = 0;7 B% t3 q o: g* |2 O
siStartInfo.lpDesktop = NULL; 3 K% d% [6 X/ h2 p5 O0 R# ~! |
siStartInfo.dwFlags = 0; 6 B3 r6 D# g& h! g9 y P
8 }- t; U6 t/ A0 x3 E2 N: R2 D" p: N// Create the child process.5 V! P/ m, M) ]
CreateProcess( 2 \9 ^* u# y% N& q7 \* S9 u
NULL,
% _! ^! h. [$ W5 GstrCmdLine.c_str(),
; L! T/ ~7 r4 m; v( ENULL, // process security attributes: }7 o' A+ d3 l) K# A
NULL, // primary thread security attributes- l( B" @: A/ B' f7 ]
0, // handles are inherited: `4 m. p# G5 g! G
0, // creation flags6 x- o! ~; ]2 @/ W( e
NULL, // use parent's environment
; C0 e3 ^' ]9 |2 xNULL, // use parent's current directory
3 b% z* Q* K0 n+ b# J&siStartInfo, // STARTUPINFO pointer
% }/ [7 [( g8 p% B+ g" J, U&piProcInfo); // receives PROCESS_INFORMATION
8 `7 m5 G9 M' g8 ?# T$ i8 W8 h// Wait for the processs to finish
3 X9 G& F# U$ f1 h% S5 S6 M& aDWORD rc = WaitForSingleObject(" l3 p* ?/ f! t, l# M5 }4 N$ e$ C! @
piProcInfo.hProcess, // process handle, E) z- P6 F' m0 y. a3 y& `
INFINITE);
0 }3 o- i2 K: M}
8 }. r, D% J4 t1 m) r- X% K 7 {% p$ D5 n' z3 _; `
|