|
使用CreateProcess()函数! d# P+ J# L8 c* c3 X, j
+ ^7 ]" _/ H3 H& N/ y
以下是一个使用CreateProcess()函数的例子:
7 x" q% u9 l5 a. @- s; V$ avoid CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine)
* `6 M4 x& H7 z. C. C9 B{
7 f, _9 L' ]+ u nPROCESS_INFORMATION piProcInfo;
+ N, ]; U4 _0 t' g( F* OSTARTUPINFO siStartInfo; 2 [8 q, \+ G, Y. ^1 n0 y. Q
// Set up members of STARTUPINFO structure.
E# c% d( l, b9 hsiStartInfo.cb = sizeof(STARTUPINFO); * ]+ G9 F0 m; L5 G+ l
siStartInfo.lpReserved = NULL;
$ |3 c& B" k4 b8 L- `siStartInfo.lpReserved2 = NULL; 7 x3 i8 _- n {
siStartInfo.cbReserved2 = 0;5 i. j6 i, g2 W
siStartInfo.lpDesktop = NULL; 0 I; _) M# v4 x g
siStartInfo.dwFlags = 0;
2 E' F$ {! h# G+ X( w! {; z5 i# i9 q2 m7 I, y
// Create the child process.
1 U& e j# x8 L: F* E% X' T5 tCreateProcess( 3 E1 r a3 b! F( `
NULL,- _# h5 z) O+ K" q: O; }$ E( _
strCmdLine.c_str(),
$ e0 p' c" k2 [NULL, // process security attributes
, b" d% G8 d" _/ oNULL, // primary thread security attributes
& l# W8 [8 e% H4 j; l t0, // handles are inherited* A/ a8 c& q5 R% ^7 Z5 ~
0, // creation flags
/ f2 K9 s, X0 G/ U. O6 O- X+ Y6 wNULL, // use parent's environment
4 A) O0 T8 a, V9 rNULL, // use parent's current directory& Z/ a% \6 @# n; c# u* S
&siStartInfo, // STARTUPINFO pointer7 I G; c/ a8 y1 @9 Q$ G
&piProcInfo); // receives PROCESS_INFORMATION
# y0 m6 k, ^: m// Wait for the processs to finish: H: X# @2 M' |- ~
DWORD rc = WaitForSingleObject(! V: s5 n L1 X2 `5 B$ a; M
piProcInfo.hProcess, // process handle
. V9 w+ i; V E# ?" l1 mINFINITE);
5 ~& y9 @8 u' C1 X3 v0 R}
5 ]/ b1 [" k P0 _ * G1 t7 [; Z2 q# Y; _8 X0 C
|