|
使用CreateProcess()函数 C q& I2 D9 @1 ]* b1 f
% K9 n* j2 Y4 d) `( Z" @
以下是一个使用CreateProcess()函数的例子: 0 Y1 X( R8 R, D( c
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
/ @% J* i" m4 L S, m* s" n$ _{ 7 p5 h# M$ I& a {
PROCESS_INFORMATION piProcInfo; / F+ q9 {7 [7 B
STARTUPINFO siStartInfo; ' v6 [; J0 Y" B
// Set up members of STARTUPINFO structure.# ^# Q! w) G7 e, r, o
siStartInfo.cb = sizeof(STARTUPINFO);
: n# U% H* B2 [ s6 t) X' I" vsiStartInfo.lpReserved = NULL;
9 { `4 R9 y# [2 jsiStartInfo.lpReserved2 = NULL;
C5 Y( q' l9 N# p2 L# NsiStartInfo.cbReserved2 = 0;! ^8 O2 p9 j; {
siStartInfo.lpDesktop = NULL;
% B6 i" b) B, |5 y7 X9 B ]siStartInfo.dwFlags = 0;
9 n% X A9 {0 Q5 S- n; u
2 t( h6 h% P1 v& f% a* L// Create the child process.
( O5 J+ p9 J% s7 F) YCreateProcess( 2 u3 a! }8 F4 P2 g
NULL,
7 ]& g8 e$ T7 _9 [3 B0 L" EstrCmdLine.c_str(),
: b5 T# w, I# A* t1 h2 ]( BNULL, // process security attributes5 y, F6 M A+ x7 m) [
NULL, // primary thread security attributes
8 T' \; `& p k6 U% c0, // handles are inherited
x# d* e% g/ \" C* t: N) n& o) {4 E! o0, // creation flags
8 d/ {/ ? v8 Z; s- A1 f mNULL, // use parent's environment
2 h7 v7 X: R& ?" h7 c: I3 K |NULL, // use parent's current directory
. w" X& q4 w3 Y&siStartInfo, // STARTUPINFO pointer
* }: I' l- E2 U/ n$ n$ M0 n! D&piProcInfo); // receives PROCESS_INFORMATION
9 h: S6 q! e" ]. a5 S// Wait for the processs to finish
/ v' v5 b9 h# \$ w9 [3 y: N7 GDWORD rc = WaitForSingleObject(
, e) J g. B- U4 `% r' H+ npiProcInfo.hProcess, // process handle
) N5 y. d* p7 w/ t5 N7 XINFINITE);
1 M) P& q% L' l' f}/ u) L; R8 n9 f2 d# @& O
7 e1 @) b8 z1 I) B4 b
|