|
使用CreateProcess()函数
! X! G1 X- ~4 ] ( o8 s [+ \$ C& t$ v! o
以下是一个使用CreateProcess()函数的例子:
, Z" n! ^0 I- `9 s+ [, @/ yvoid CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
: _4 ~5 ?( K. U$ T/ Y4 m{
! |* z" L+ T$ S6 T) |PROCESS_INFORMATION piProcInfo;
2 S7 p' e) ]9 v1 U: e' x% _0 VSTARTUPINFO siStartInfo;
2 N/ \) W, T/ n& c. o// Set up members of STARTUPINFO structure.
, u& k* Y+ s: P/ I1 k. v' WsiStartInfo.cb = sizeof(STARTUPINFO); # r Z" s1 j* C5 }1 ^9 e# N
siStartInfo.lpReserved = NULL;
7 Z" Q4 `7 N, P1 Z. nsiStartInfo.lpReserved2 = NULL; 8 I% F* s, b. ?) x1 N
siStartInfo.cbReserved2 = 0;: ]8 b( O7 P9 D2 |
siStartInfo.lpDesktop = NULL;
1 w, T/ o3 c1 t, {8 s/ ksiStartInfo.dwFlags = 0; . |& h7 {2 v0 _ _& h
9 c( ?4 T; @' ?; r. s2 ]: F% {: j
// Create the child process.
, P$ U( \, t) M$ sCreateProcess(
) g; X2 i2 e% ?6 G7 M5 ?NULL,
2 x7 Y$ T6 ? VstrCmdLine.c_str(),% y8 o3 R6 g! Z
NULL, // process security attributes1 F* Z5 Z! h0 z: W; [1 _0 h8 |, [
NULL, // primary thread security attributes
' j/ Q: _% h4 {1 j- C _" C% N0, // handles are inherited% b2 |$ @+ j9 Y9 W1 m9 k
0, // creation flags! F: t' K. K! \# m( ~. F9 l
NULL, // use parent's environment0 w5 \$ I8 r+ ]( G' H+ _, \
NULL, // use parent's current directory; R9 w9 w6 g" Z, a) t% m+ v/ L ~
&siStartInfo, // STARTUPINFO pointer
' O7 |' ?& m+ N" i# Y7 N&piProcInfo); // receives PROCESS_INFORMATION
g6 k. l. j0 f; `$ v// Wait for the processs to finish- ~9 P! R2 c$ e, i+ p9 A. \& Y
DWORD rc = WaitForSingleObject(
/ k1 q l" M0 }+ N' z9 z2 q& tpiProcInfo.hProcess, // process handle
1 S- ]. I9 g: n7 ?) t! K/ wINFINITE); 9 x4 w6 a! K4 u+ R0 s: X
}
4 F# S5 A- J- h " i9 C5 ~6 L# V( G; L7 x
|