|
使用CreateProcess()函数, s0 F1 ]8 x/ s* i
7 R7 Z9 K# d1 n6 v4 ?
以下是一个使用CreateProcess()函数的例子: 8 d* h- u% q+ [6 E% {
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
9 J) j( {. |! V, S4 b* b{
( O4 z1 K3 F" o7 F- uPROCESS_INFORMATION piProcInfo;
# @) M- G& H* U& G4 E+ X# c% [+ pSTARTUPINFO siStartInfo; * E8 ^/ B3 u5 M& z9 o+ w: T
// Set up members of STARTUPINFO structure.4 f3 V8 f3 |( |1 p2 L
siStartInfo.cb = sizeof(STARTUPINFO); 1 Q5 |% M+ E2 T; e" P* y; ]3 r
siStartInfo.lpReserved = NULL;7 l1 R4 J, v A
siStartInfo.lpReserved2 = NULL; 8 {) ]% @, }7 [( s' f Q! j* Q+ V
siStartInfo.cbReserved2 = 0;
7 m6 q J2 w7 l9 ysiStartInfo.lpDesktop = NULL;
, ]0 D5 @* ], h* ]siStartInfo.dwFlags = 0; $ e: I- n, f" k1 e/ b) T
# H/ s# s2 E3 P0 ]+ u1 k8 t
// Create the child process.
( }6 L. Q3 \1 d T3 XCreateProcess(
( k" M4 B0 b9 s" K) F+ ~NULL,$ U) k3 ]7 e6 w% t( E# h) c
strCmdLine.c_str(),
- g$ i4 R1 A. Z% O, f7 RNULL, // process security attributes/ ^ f' @# B# D- j. h- m2 \, G
NULL, // primary thread security attributes
8 O; Q! r$ ]# r7 {' O. N0, // handles are inherited: g- k* a/ c% o% n
0, // creation flags
, l9 F" b0 G9 ?2 `5 hNULL, // use parent's environment
1 Y0 v( B3 g! N k% FNULL, // use parent's current directory
$ t4 H. a, w4 K* K% d4 b9 M$ T# f&siStartInfo, // STARTUPINFO pointer
g) g; {8 A9 j; D7 O- p4 a&piProcInfo); // receives PROCESS_INFORMATION 8 L6 ^' D- {' O1 k& G
// Wait for the processs to finish1 e) \ a' B% p2 T' [1 Z
DWORD rc = WaitForSingleObject(
8 N: p, q; n1 i* w" @) B2 RpiProcInfo.hProcess, // process handle c& x3 W* U% [& b; {7 A' [$ m
INFINITE); : o5 W0 x+ x' W$ Z3 U* @! Q/ ]
}- y4 M" }) V9 x( Y: N& w( F
" d' j& c8 y/ n3 N2 l0 W8 S+ [6 f
|