本帖最后由 nbwbcn 于 2009-06-10 11:34:35 编辑

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,ShellAPI, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function OpenExe(FileName:string):Integer;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.OpenExe(FileName: string): Integer;
    var
      appName: array [0..512] of char;
      sInfo:TStartupInfo;
      pInfo:TProcessInformation;
      code:DWord;
    begin
      StrPCopy(appName,FileName);
      FillChar(sInfo,SizeOf(sInfo),#0);
      sInfo.cb := SizeOf(sInfo);
      sInfo.dwFlags := STARTF_USESHOWWINDOW;
      sInfo.wShowWindow := 1;
      if not CreateProcess(nil,appName,nil,nil,False,CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,nil,nil,sInfo,pInfo) then
      begin
        Result := -1;
      end
      else
      begin
        WaitForSingleObject(pInfo.hProcess,INFINITE);
        GetExitCodeProcess(pInfo.hProcess,code);
        Result := code;
      end;end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      OpenExe('notepad');//执行第一个记事本,关闭后才能执行下一个
      OpenExe('notepad');
    end;end.
      

  2.   

    正解
    CreateProcess
    WaitForSingleObject
      

  3.   

    我已经说明了:外部程序等待执行已经实现。 返回Success可判断执行结果。CreateProcess WaitForSingleObject  RunAndWait=你的OpenExe。
    现在是多个复选框,多个外部程序,先判断是否选中,再顺序运行。现在问题就是他们会同时运行!!!

      

  4.   

    代码中已经给出,你自己去试试,根据复选框顺序去执行,OpenExe会等待所执行的程序关闭,才会返回,去执行另一个程序if checkbox1=true then
    begin
      OpenExe(程序1);
    end;
    程序1执行完后,才会执行下面的代码
    if checkbox2=true then
    begin
      OpenExe(程序2);
    end;
    你还可以判断OpenExe的返回值,检查进程是否创建成功
      

  5.   

    如果你已经确保得到了进程的id,那就WaitForSingleObject  等待吧
      

  6.   

    根据复选框把待运行的命令行写到一个bat文件,然后一次性运行
      

  7.   

    也就是说1楼这样的写法。
    多个OpenExe如果外面加上if就不会依次了。就会同时运行。假想解决思路例如:
    var runlist:???;
    if checkbox1=true then
    runlist='1.exe|'
    end;
    if checkbox2=true then
    runlist=runlist+'2.exe|'
    end;
    ...运行时分拆runlist
    循环
    OpenExe(runlist(i))
    也许这样不会一起运行。
      

  8.   

    现在 用的。会同时执行。  if drv1.Checked=true then
      begin
      Success := False;
      try
      Num:=Num+1;
      State1.Lines.Add('');
      State1.Lines.Add('1 Checked.');
      State1.Lines.Add('1 Installing...');
      State1.Lines.Add('Please wait...');
          InstanceID := RunAndWait( PChar(ExtractFileDir(paramstr(0))+'\1.exe -s'),1,PChar(ExtractFileDir(paramstr(0))));
          Success := InstanceID >= 32; 
      finally    if not Success then
          begin
          State1.Lines.Add('');
          State1.Lines.Add('ERROR: 1 Not Found!');
          end
      end;
          State1.Lines.Add('');
          State1.Lines.Add('1 Installed.');
      if drv2.Checked=true then
      begin  Success := False;
      try  Num:=Num+1;
      State1.Lines.Add('');
      State1.Lines.Add('2 Checked.');
      State1.Lines.Add('2 Installing...');
      State1.Lines.Add('Please wait...');
      if GetSystemStr='Windows 32' then    //<Windows
          begin
          InstanceID := RunAndWait( PChar(ExtractFileDir(paramstr(0))+'\21.exe'),1,PChar(ExtractFileDir(paramstr(0))));
          Success := InstanceID >= 32; //
          end
      else if (GetSystemStr='Windows Xp') OR (GetSystemStr='Windows 2003') then //XP&2003      if GetSystemXbit='x64' then
            begin
            InstanceID := RunAndWait( PChar(ExtractFileDir(paramstr(0))+'\22.exe'),1,PChar(ExtractFileDir(paramstr(0));
            Success := InstanceID >= 32; // 
            end
          else
            begin
            InstanceID := RunAndWait( PChar(ExtractFileDir(paramstr(0))+'\23.exe'),1,PChar(ExtractFileDir(paramstr(0))));
            Success := InstanceID >= 32; // 
            end
               else if (GetSystemStr='Windows Vista') OR (GetSystemStr='Windows Server 2008') OR (GetSystemStr='Windows 7') OR (GetSystemStr='Windows Server 2008 R2') then
        
          if GetSystemXbit='x64' then
            begin
            InstanceID := RunAndWait( PChar(ExtractFileDir(paramstr(0))+'\24.exe'),1,PChar(ExtractFileDir(paramstr(0))));
            Success := InstanceID >= 32; // 小于32可就错了
            end
          else
            begin
            InstanceID := RunAndWait( PChar(ExtractFileDir(paramstr(0))+'\25.exe'),1,PChar(ExtractFileDir(paramstr(0))));
            Success := InstanceID >= 32; // 
            end  else
        begin
        State1.Lines.Add('');
        State1.Lines.Add('The operating system does not support!');
        State1.Lines.Add('Please Try manually install!');
        Success:= False;
        end;
           finally    
          if not Success then
          begin
          State1.Lines.Add('');
          State1.Lines.Add('ERROR: 2 Not Found!');
          end
        end;                                    
          State1.Lines.Add('');
          State1.Lines.Add('2 Installed.'); 
    end;