|
使用CreateProcess()函数
3 J* A' `/ i3 c) g/ W1 ]; | f3 k1 a* P' ^6 O
以下是一个使用CreateProcess()函数的例子: ; T6 B) G: i; V1 g/ E# `
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) 2 ` V# ^4 R, a) a: f4 O
{
7 s4 r0 F1 ?4 [! {- O6 nPROCESS_INFORMATION piProcInfo; ! ^* ?$ ]5 u* g6 ?3 _8 ^- f
STARTUPINFO siStartInfo; 7 V0 F9 d0 m- f) s; K7 k6 I
// Set up members of STARTUPINFO structure.# K+ E" d4 i* Q$ m/ T
siStartInfo.cb = sizeof(STARTUPINFO);
5 w& @9 w5 D# M$ k9 I" b1 M$ | F/ w' |siStartInfo.lpReserved = NULL;
& S8 ]/ K6 z* q8 b L- IsiStartInfo.lpReserved2 = NULL; 6 Q; g& e5 {1 e- M( P0 |
siStartInfo.cbReserved2 = 0;4 Y* X- `% K9 y) x
siStartInfo.lpDesktop = NULL;
! {& d- P \% B9 ksiStartInfo.dwFlags = 0; " G9 H9 i7 D: Q& _/ E
: a: x2 t4 @8 M! J) q// Create the child process.
% l. Q( r& h9 ~8 L8 g# J7 g% ZCreateProcess( & D( I1 e& ^: ~/ {
NULL,
' c1 ?4 b4 ?5 @strCmdLine.c_str(),
/ |" @& y, ]' w8 g% G" }NULL, // process security attributes
! t8 P0 b H [2 ONULL, // primary thread security attributes! H. U% X4 u& y: v8 X( S
0, // handles are inherited, ]9 I4 Q8 l" \" l$ o' k9 `; p
0, // creation flags- W: w# e, O" [- {0 k( H! {( M& J% K
NULL, // use parent's environment
7 L) a- H* F, y/ D8 t: b9 u CNULL, // use parent's current directory, v2 O8 o/ x9 w* k5 k
&siStartInfo, // STARTUPINFO pointer
9 N& b- k$ J$ h4 n% t&piProcInfo); // receives PROCESS_INFORMATION
+ `3 ?+ O& ^, i! m8 `4 e; Z// Wait for the processs to finish. M3 q0 O" d$ O
DWORD rc = WaitForSingleObject(
9 I6 h2 Z. ~0 {, x/ z( p% g" a* RpiProcInfo.hProcess, // process handle
& s2 S1 e% E$ @/ uINFINITE); ; j8 Z0 `" I1 U+ V: n' ?
}
5 a( A, I6 g& \! D 2 [ J8 X7 N3 O) U4 w+ U. I
|