利用WaitForInputIdle等待初始化完成CreateProcess

 1 //函数原型
 2 
 3 BOOL WINAPI CreateProcess(
 4   __in_opt     LPCTSTR lpApplicationName,
 5   __inout_opt  LPTSTR lpCommandLine,
 6   __in_opt     LPSECURITY_ATTRIBUTES lpProcessAttributes,
 7   __in_opt     LPSECURITY_ATTRIBUTES lpThreadAttributes,
 8   __in         BOOL bInheritHandles,
 9   __in         DWORD dwCreationFlags,
10   __in_opt     LPVOID lpEnvironment,
11   __in_opt     LPCTSTR lpCurrentDirectory,
12   __in         LPSTARTUPINFO lpStartupInfo,
13   __out        LPPROCESS_INFORMATION lpProcessInformation
14 );
15 
16 //
17 DWORD WINAPI WaitForInputIdle(
18   __in  HANDLE hProcess,
19   __in  DWORD dwMilliseconds
20 );
21 Return code/value Description 
22 0
23  The wait was satisfied successfully.
24  
25 WAIT_TIMEOUT
26  The wait was terminated because the time-out interval elapsed.
27  
28 WAIT_FAILED
29  An error occurred.
30  
31 
32 // 创建子进程
33         STARTUPINFO startup;
34         PROCESS_INFORMATION procinfo;
35         ::ZeroMemory(&startup, sizeof(startup));
36         startup.cb = sizeof(startup);
37         startup.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
38         startup.wShowWindow = SW_HIDE; // hidden console window
39         startup.hStdInput = NULL; // not used
40         startup.hStdOutput = hPipeOutW;
41         startup.hStdError = hPipeErrW;
42 
43         BOOL started = ::CreateProcess(NULL,        // command is part of input string
44                         _command,         // (writeable) command string
45                         NULL,        // process security
46                         NULL,        // thread security
47                         TRUE,        // inherit handles flag
48                         CREATE_SUSPENDED,           // flags
49                         NULL,        // inherit environment
50                         _curDir,        // inherit directory
51                         &startup,    // STARTUPINFO
52                         &procinfo);  // PROCESS_INFORMATION
53 if(!started)
54 {
55        error(TEXT("CreateProcess"), result, 1002);
56 }
57 else
58 {
59        if (WaitForInputIdle(pinfo.hProcess, 30000) == 0)  
60        {
61              // ......
62        }
63 }
64 
65              CloseHandle(procinfo.hThread);  
66              CloseHandle(procinfo.hProcess);
posted @ 2012-11-08 14:42  歪歪不歪  阅读(1064)  评论(0)    收藏  举报