shellexecute执行成功,只是指示了它成功激活了一个文件,比如exe程序,那么,
怎么样判定所执行的exe程序是否执行完毕,以及执行有没有正常退出?

解决方案 »

  1.   

    使用CreateProcess到程序执行结束procedure TForm1.btnRunClick(Sender: TObject);
    var
     hReadPipe,hWritePipe:THandle;
     si:STARTUPINFO;
     lsa:SECURITY_ATTRIBUTES;
     pi:PROCESS_INFORMATION;
     mDosScreen:String;
     cchReadBuffer:DWORD;
     ph:PChar;
     fname:PChar;
     i,j:integer;
    begin
     fname:=allocmem(255);
     ph:=AllocMem(5000);
     lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
     lsa.lpSecurityDescriptor :=nil;
     lsa.bInheritHandle :=True; if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
     begin
       ShowMessage('Can not create pipe!');
       exit;
     end;
     fillchar(si,sizeof(STARTUPINFO),0);
     si.cb :=sizeof(STARTUPINFO);
     si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
     si.wShowWindow :=SW_HIDE;
     si.hStdOutput :=hWritePipe;
     StrPCopy(fname,'CMD.EXE /c dir/w'); if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False  then
     begin
       ShowMessage('can not create process');
       FreeMem(ph);
       FreeMem(fname);
       Exit;
     end; while(true) do
     begin
       if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
       if cchReadBuffer<>0 then
       begin
         if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
         ph[cchReadbuffer]:=chr(0);
                                   Memo1.Lines.Add(ph);
       end
       else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
       Sleep(100);
     end; ph[cchReadBuffer]:=chr(0);
              Memo1.Lines.Add(ph);
     CloseHandle(hReadPipe);
     CloseHandle(pi.hThread);
     CloseHandle(pi.hProcess);
     CloseHandle(hWritePipe);
     FreeMem(ph);
     FreeMem(fname);
    end;  
      

  2.   

    这么复杂!几乎没有看得懂的!:(
    这些算是DELPHI中的哪一块呢?windows核心编程么?
    还请介绍几本相关的简明一点的书和几本详细一点的。
    谢谢。
      

  3.   

    things(平) (枕流以洗耳,漱石以磨牙):太复杂了
    //我自己写的,我自用没问题
    //执行外部可执行文件
    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;