使用CreateProcess()函数: t: s, y- S1 p) P$ z% G, T
; Z' S& q; g3 Z$ O! v% e9 g
以下是一个使用CreateProcess()函数的例子: 3 L x6 w) @* O* B% J
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) 3 T4 k! r7 ]' O+ A2 N
{ 3 x; `. B H. p/ l* u
PROCESS_INFORMATION piProcInfo;
) i/ B+ C" B F* r# ^$ S* f2 N+ u8 T! WSTARTUPINFO siStartInfo; ) ~& U2 @2 j |+ E2 C: Q$ M, o
// Set up members of STARTUPINFO structure.
4 `! M( p' ^# j2 F7 G* usiStartInfo.cb = sizeof(STARTUPINFO); ! a3 w8 I% e! n' K3 e
siStartInfo.lpReserved = NULL;
3 R1 C- B* c- |& @siStartInfo.lpReserved2 = NULL; 2 h- `' U0 m2 Z0 l
siStartInfo.cbReserved2 = 0;0 Z* I) h$ z. W9 d' ] s: y; [9 i: |* [
siStartInfo.lpDesktop = NULL;
+ q5 }7 Q g( C7 rsiStartInfo.dwFlags = 0;
2 q# m. ?4 K* J0 q7 v1 O* w+ D. r8 ]* W( R% _. n* K0 p, _9 G* e2 P
// Create the child process. ~' ~# o5 Y3 _7 _. d; X: J
CreateProcess( 5 b6 D; c D+ M% _" ~# `' S
NULL,
7 X" G" g! e5 A+ o3 G, U6 OstrCmdLine.c_str(),8 p$ q3 h# }% s+ Y2 ~5 n( o
NULL, // process security attributes
* G/ A" I! B0 g; t$ w" QNULL, // primary thread security attributes
$ T5 _" ~/ ]$ f' S0, // handles are inherited; }" K! N! w- w1 G# @6 U8 k
0, // creation flags0 V2 g8 ?2 c# h# ~4 G0 m9 B% P
NULL, // use parent's environment
' K- O& A4 r& e$ ^% n( G1 a& hNULL, // use parent's current directory
) S# V+ i. ~3 Z&siStartInfo, // STARTUPINFO pointer7 R% N& R5 f/ i+ K; j
&piProcInfo); // receives PROCESS_INFORMATION $ j. [' r l7 [- R2 _2 X
// Wait for the processs to finish: w7 B5 |) ?& u& c6 f z
DWORD rc = WaitForSingleObject(2 D: x8 C% S1 ^( j e
piProcInfo.hProcess, // process handle
4 n5 g* |. _4 c# cINFINITE); . n' K9 s0 C4 b& W2 x @- a
}$ x% Q' c( k: s
- X( F; k; J0 e5 D0 Z* w: j/ y |