|
使用CreateProcess()函数
& e+ `' B4 _) G. m1 P% {) S
& K, Z( {' q* V5 K3 x3 C 以下是一个使用CreateProcess()函数的例子: 0 A9 @" |+ k8 E9 S
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
0 ]/ n) a: C% T* S{
! k1 ^. p9 A* M7 u# XPROCESS_INFORMATION piProcInfo;
# M# A2 v$ r2 r( e+ CSTARTUPINFO siStartInfo; 0 s' _2 E1 R7 a! e5 Y! F
// Set up members of STARTUPINFO structure.
. Z7 v3 L* u4 ysiStartInfo.cb = sizeof(STARTUPINFO); : U' P; S& G! Q( H# I4 F
siStartInfo.lpReserved = NULL;
: y N9 N, p/ A5 @( m, dsiStartInfo.lpReserved2 = NULL;
& P# g: u- s7 HsiStartInfo.cbReserved2 = 0;
, f7 |$ p- [0 ^. t& u' f8 J8 fsiStartInfo.lpDesktop = NULL; 5 `$ q% k$ P T3 h' i
siStartInfo.dwFlags = 0; 1 F5 l5 c2 o1 Y- H
, Z' `5 W2 `# T* @
// Create the child process.
% w1 p* a3 s; C* w8 x$ t! cCreateProcess(
' `1 Q% B# ?3 L8 @2 SNULL,7 K. `( w/ b. h6 B4 ]
strCmdLine.c_str(),
* Z# T5 ?7 K, {. F7 }# G6 `NULL, // process security attributes
" Y3 j5 L; }" v7 N/ k5 RNULL, // primary thread security attributes& V2 {7 o' A: s! d; y! n7 U
0, // handles are inherited8 t3 x) \7 J! K, M. m6 d6 n
0, // creation flags
- T4 T$ p/ t; s" TNULL, // use parent's environment2 Q! X+ k4 |8 g' }& o7 |
NULL, // use parent's current directory" y5 G9 p$ n% L
&siStartInfo, // STARTUPINFO pointer
" C: ?8 Q7 w3 k4 R, q7 n&piProcInfo); // receives PROCESS_INFORMATION
/ R5 i$ ~ [! R+ B5 r// Wait for the processs to finish: Z0 `) L* u" D( \
DWORD rc = WaitForSingleObject($ m* E6 S9 c4 c) a5 |6 s
piProcInfo.hProcess, // process handle% s: F* K$ e, M- ]8 H9 ^, h8 U
INFINITE);
$ d! y6 l5 W2 V; s, w! O; b9 e( q}
- T; |* o9 d$ w+ X3 ~4 u( s9 u! c+ x/ ^ * _; ^5 u) z5 A
|