请教在Delphi中使用ShellExecute运行另一个.EXE文件时如何不使程序继续执行,只有当执行的程序运行完成后才继续执行下面的语句?...

解决方案 »

  1.   

    ShellExcute 不行的
    用ShellExcuteEx就可以了
    使用ShellExcuteEx, 你可以得到新进程的HANDLE
    然后使用WaitForSingleObject 就可以等待进程结束
      

  2.   

    spirit_sheng(老盛) 您好! 我是初學的,您能給出完整的代碼嗎?
      

  3.   

    顶楼上的或者用CreateProcess,然后再WaitFor...也要以
      

  4.   

    var
       CmdLine: string;
       st: TStartUpInfo;
       pp: TProcessInformation;
    begin
          CmdLine := 'C:\CreateProcessSample.exe';
          FillChar(st, sizeof(st), #0);
          with st do
          begin
             cb := sizeof(st);
             dwFlags := STARTF_USESHOWWINDOW;
             lptitle := nil;
             wShowWindow := SW_SHOW;
          end;
          CreateProcess(nil, PChar(CmdLine), nil, nil, LongBool(0), 0, nil, nil, st, pp);
          WaitForSingleObject(pp.hProcess, INFINITE);
      

  5.   

    function TMainForm.WinExecAndWait32(FileName:String; Visibility : integer):integer;
    var 
      zAppName:array[0..512] of char;
      zCurDir:array[0..255] of char; 
      WorkDir:String; 
      StartupInfo:TStartupInfo; 
      ProcessInfo:TProcessInformation;
      r:Cardinal;
    begin 
      StrPCopy(zAppName,FileName);
      GetDir(0,WorkDir);
      StrPCopy(zCurDir,WorkDir);
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := Visibility;
      if not CreateProcess(nil, 
        zAppName,                       { pointer to command line string } 
        nil,                            { pointer to process security attributes } 
        nil,                            { pointer to thread security attributes } 
        false,                          { handle inheritance flag } 
        CREATE_NEW_CONSOLE or           { creation flags }
        NORMAL_PRIORITY_CLASS,
        nil,                            { pointer to new environment block } 
        nil,                            { pointer to current directory name }
        StartupInfo,                    { pointer to STARTUPINFO }
        ProcessInfo) then Result := -1 { pointer to PROCESS_INF }   else begin
        WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
        GetExitCodeProcess(ProcessInfo.hProcess,R);
      end;
      result:=r;
    end;