为什么我用CreateProcess创建一个进程,执行一个dos命令(就一个dir/s,正常大约1分钟)
用 GetExitCodeProcess来获得进程结束代号,要等四五分钟才结束,
而用WaitForSingleObject(hProc, INFINITE)干脆就无限期等待了
procedure TForm1.Button2Click(Sender: TObject);
var
  pi: Process_Information;
  ExitCode: DWord;
  si: TStartUPINFO;
  Ti: integer;
begin
  FillChar(si, Sizeof(si), 0);
  si.cb := sizeof(si);
  si.wShowWindow := SW_HIDE;
  si.dwFlags := STARTF_USESHOWWINDOW;
 if CreateProcess(nil, PChar('C:\BB.bat'), nil, nil, True, NORMAL_PRIORITY_CLASS,
    nil, PChar('C:\'), si, pi) then
 begin
  WaitForSingleObject(pi.hProcess, 1000);
  {repeat
    GetExitCodeProcess(pi.hProcess, INFINITE);
  until ExitCode <> STILL_ACTIVE;}
  closeHandle(pi.hProcess);
  closeHandle(pi.hThread);
 end;
 
但是如果用下面的代码,却很快就返回
function ExcuteCmd(const CmdLine, CmdDir: string; var RetStr: string):Boolean;
var
  hPipe,hRead1, hWrite1, hRead2, hWrite2: THandle;
  si: TStartupInfo;
  sa: SECURITY_ATTRIBUTES;
  pi: Process_Information;
  ExitCode: DWord;
  ByteCount: Integer;
  dwBytesRead,dwTotal, dwLeft: Dword;
  Buf: PChar;
  bSucc:  Boolean;
const
  BufSize = 5120;begin
  Result := False;
  FillChar(si, Sizeof(si), 0);
  sa.nLength := sizeOf(sa);
  sa.lpSecurityDescriptor := nil;
  sa.bInheritHandle := True;
  CreatePipe(hRead1, hWrite1, @sa, 0);
  CreatePipe(hRead2, hWrite2, @sa, 0);
  si.cb := sizeof(si);
  si.wShowWindow := SW_HIDE;
  si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  si.hStdInput := hRead1;
  si.hStdOutput := hWrite2;
  Buf := StrAlloc(BufSize);
  if CreateProcess(nil, PChar(CmdLine), nil, nil, True, NORMAL_PRIORITY_CLASS,
    nil, PChar(CmdDir), si, pi) then
  begin
    Result := True;
    repeat
      GetExitCodeProcess(pi.hProcess, ExitCode);
      while True do
      begin
        PeekNamedPipe(hRead2, Buf, BufSize, @dwBytesRead, @dwTotal, @dwLeft);
        if dwBytesRead = 0 then Break;
        ZeroMemory(Buf, BufSize);
        ReadFile(hRead2, Buf^, BufSize, dwBytesRead, nil);
        ByteCount := ByteCount + dwBytesRead;
        RetStr := RetStr + StrPas(Buf);
      end;
    until ExitCode <> STILL_ACTIVE;
  end;
  CloseHandle(hRead1);
  CloseHandle(hRead2);
  CloseHandle(hWrite1);
  CloseHandle(hWrite2);
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  StrDispose(Buf)
end;请赐教...

解决方案 »

  1.   

    //我自己写的,我自用没问题
    //执行处部可执行文件
    Function LaunchApp(pExePath,WorkDir:String;ShowWindow,WaitFlag:Boolean):Boolean;
    var ExePath : array[0..512] of char;
        StartupInfo:TStartupInfo;
        ProcessInfo:TProcessInformation;
    begin
      StrPCopy(exePath, pExePath);
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      if ShowWindow then
        StartupInfo.wShowWindow := 1
      else
        StartupInfo.wShowWindow := 0;
      if not CreateProcess(nil,
                          ExePath,
                          nil,
                          nil,
                          false,
                          CREATE_NEW_CONSOLE or
                          NORMAL_PRIORITY_CLASS,
                          nil,
                          nil,
                          StartupInfo,
                          ProcessInfo) then  begin
        Result:=False;
        Exit;
      end else begin
        if WaitFlag then
          WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
        Result:=True;
      end;
    end;