|
使用CreateProcess()函数. @. N" A* n2 C* [3 X# H
7 c0 ^5 C! Z3 h8 p2 V, f8 w 以下是一个使用CreateProcess()函数的例子: 8 x, q. e4 `" q( k( ]$ R7 z2 U
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
3 V. @& U: W+ p$ P* ?: V5 u' z& }{ / U$ m0 P" a2 f7 {* j0 R) y
PROCESS_INFORMATION piProcInfo; * t4 o+ J: E" C6 c, x
STARTUPINFO siStartInfo; ) h9 g6 t( E" s+ b
// Set up members of STARTUPINFO structure.
# W; x. K9 B- K( hsiStartInfo.cb = sizeof(STARTUPINFO); ! R- }, c/ ^( C+ Q0 u0 Y$ o- @; ?
siStartInfo.lpReserved = NULL;
_. q' }- b" G! fsiStartInfo.lpReserved2 = NULL;
8 q4 ]2 t' j- V7 d# |siStartInfo.cbReserved2 = 0;
' i# _( L: A3 f7 HsiStartInfo.lpDesktop = NULL;
( w# {) Z" P% j- h' t* SsiStartInfo.dwFlags = 0; ; t2 {$ S8 c+ \" h, [" u4 j
$ O9 f2 S; w: h
// Create the child process.
: d3 }& C1 M, o9 O8 A1 O% x, J$ sCreateProcess( : p/ ]* W- Q/ w# P) R
NULL,
1 H8 w) g# J# i2 y( LstrCmdLine.c_str(),
# d- Z f2 ]! NNULL, // process security attributes
$ R; [0 Y8 Y ]9 O! RNULL, // primary thread security attributes! v* W" [" b0 H, `1 K+ Q# S9 L
0, // handles are inherited
( T, T* u j3 l0, // creation flags! |) _1 l' V* D( S
NULL, // use parent's environment
9 q5 X1 x( l2 v9 e' CNULL, // use parent's current directory* O4 s$ i! ^5 Z) J+ i
&siStartInfo, // STARTUPINFO pointer9 O/ K# F' ?( c' O
&piProcInfo); // receives PROCESS_INFORMATION
$ a, O2 }# R& @ k// Wait for the processs to finish1 {4 q, J. g# X5 R8 z* w
DWORD rc = WaitForSingleObject(9 ?; x4 o# o5 L2 \* d5 [9 D8 Z
piProcInfo.hProcess, // process handle
) C- Z2 q9 d- y0 O" _INFINITE); 7 S9 b3 {5 \8 e* M
}
0 x' b3 w r# R) j' J$ v. n $ q! @- ]- s) s6 o/ S
|