使用CreateProcess创建子进程,保存hChildProcess,然后调用
if (WaitForSingleObject(hChildProcess,INFINITE) == WAIT_FAILED)
{
     //error handler
}
INFINITE表示一直等待,也可用一个具体的值表示等待时间

解决方案 »

  1.   

    用CreateProcess启动程序,然后调用WaitForSingleObject 等待进程结束。
      

  2.   

    PROCESS_INFORMATION pi;
        STARTUPINFO si;
        si.cb = sizeof(STARTUPINFO);
        si.lpReserved = NULL;
        si.lpDesktop = NULL;
        si.lpTitle = NULL;
        si.dwFlags = 0;
        si.cbReserved2 = 0;
        si.lpReserved2 = NULL;
        BOOL bres = CreateProcess(NULL,"test a.txt b.txt",NULL,NULL,false,
                            NORMAL_PRIORITY_CLASS,
                            NULL,NULL,&si,&pi);
    if(bres==false)
    {
    AfxMessageBox("CreateProcess failed");
    }
    else
    {
    CloseHandle(pi.hThread);
    DWORD dwret=WaitForSingleObject(pi.hProcess, 1000*30); 
    switch(dwret)
    {
    case WAIT_OBJECT_0:
    DWORD dwexitcode;
    bres = GetExitCodeProcess(pi.hProcess,&dwexitcode);
    TCHAR exitmsgbuf[1024];
    if(bres)
    {
    wsprintf(exitmsgbuf,"exit code:%d",dwexitcode);
    }
    else
    wsprintf(exitmsgbuf,"exit code failed to return"); AfxMessageBox(exitmsgbuf);
    break;
    default:
    AfxMessageBox("exit for other reason");
    }
    CloseHandle(pi.hProcess);
    }