使用CreateProcess()函数& d" n/ k+ k* G( L6 ]* ~& n$ a
+ u' Z+ ~, `8 ~5 q
以下是一个使用CreateProcess()函数的例子: * ?6 X6 Z1 h4 x! p2 T" U( G3 S" M
void CreateChildProcessAndWaitUntilDone(const AnsiString& strCmdLine) 6 Z. ?% B% k/ L, {1 ~0 R! i
{
m$ X. w7 w4 TPROCESS_INFORMATION piProcInfo; ; x: ? J7 e _$ Y3 P
STARTUPINFO siStartInfo; ' H* d/ ?. k; T1 C; k& j# h
// Set up members of STARTUPINFO structure.0 F8 ?+ T9 i" q- D4 }4 I% Y
siStartInfo.cb = sizeof(STARTUPINFO);
6 s, J# O7 l* M, M% Z8 Y8 T( H" ~% PsiStartInfo.lpReserved = NULL;# `* n$ m! B& g+ B7 s
siStartInfo.lpReserved2 = NULL;
% o6 Q% m, P: T* PsiStartInfo.cbReserved2 = 0;4 q( P) @, [% r/ Y0 ^' f, t4 b
siStartInfo.lpDesktop = NULL;
- X u) G8 w. f, {4 e% PsiStartInfo.dwFlags = 0; 9 K8 L! w1 j. Q0 _( D" k
9 p* u1 u3 e; n) L' w0 e// Create the child process.$ L/ a7 v+ t0 A, a4 ~
CreateProcess( ' p+ Q2 b+ R' a' c& c
NULL,
@. g& H% p5 t1 w7 MstrCmdLine.c_str(),8 h* h Y% S3 [* Q
NULL, // process security attributes
# p q+ K5 ~1 y- hNULL, // primary thread security attributes$ y' A3 j6 i- a* e
0, // handles are inherited
1 b" J9 _& u" G7 V5 @! V0, // creation flags
/ m; J! u# |2 I# ~( yNULL, // use parent's environment
4 w3 K' p) r5 p- {9 P8 a3 XNULL, // use parent's current directory9 G$ K; R9 r, I( o; L' S. {
&siStartInfo, // STARTUPINFO pointer
) M( K, y0 X- h9 f9 k- @&piProcInfo); // receives PROCESS_INFORMATION / y5 Y8 C9 |3 c- y2 B4 b$ f
// Wait for the processs to finish* U* g1 z. L) U2 c/ ~8 [/ p
DWORD rc = WaitForSingleObject($ T7 ]5 I% G( C3 X* T* K
piProcInfo.hProcess, // process handle
6 t5 Y" y# H. N6 Q( G. \, JINFINITE);
0 `! t3 j& N5 f: j}
1 M9 C$ L1 w) Z# S# f $ b/ ^- f1 t, c: S/ p; d5 U1 m
|