【问题标题】:how to tell when an external application ends in delphidelphi 如何判断外部应用程序何时结束
【发布时间】:2010-01-21 00:26:26
【问题描述】:

我正在使用 ShellExecute 运行外部应用程序 如何判断外部应用程序何时结束?

这是我的代码

theProgram     :=  'MySql.exe';
itsParameters  :=  ' -u user1 -ppassword -e "create database abc"’;
rslt := ShellExecute(0, 'open',
                       pChar (theProgram),
                       pChar (itsParameters),
                       nil,
                       SW_SHOW);

【问题讨论】:

    标签: windows delphi shellexecute


    【解决方案1】:

    试试下面的功能。 WaitForSingleObject 可以满足您的需求。

    function ExecAppAndWait(const sApp, sParams: String; wShow: Word; sCurrentDirectory: String = ''): DWord;
    { Parameter wShow: SW_HIDE, SW_SHOWNORMAL, SW_NORMAL, SW_MAXIMIZE ...}
    var
      aSI     : TStartupInfo;        // Win32 : STARTUPINFO
      aPI     : TProcessInformation; // Win32 : PROCESS_INFORMATION
      aProc   : THandle;             // Win32
      aCurrentDirectory: PChar;
      s: String;
    begin
      s := sApp + ' ' + sParams;
      FillChar(aSI, SizeOf(aSI), 0);
      aSI.cb := SizeOf(aSI);
      aSI.wShowWindow := wShow;
      aSi.dwFlags := STARTF_USESHOWWINDOW;
    
    
      if sCurrentDirectory = '' then
        aCurrentDirectory := nil
      else
        aCurrentDirectory := PChar(sCurrentDirectory);
    
      Win32Check(CreateProcess(nil, PChar(s), nil, nil,
                 False, Normal_Priority_Class, nil, aCurrentDirectory, aSI, aPI));
       // in TProcessInformation.hProcess -> Process-Handle
      aProc := aPI.hProcess;
    
      CloseHandle(aPI.hThread);
    
    
      if WaitForSingleObject(aProc, Infinite) <> Wait_Failed then
        GetExitCodeProcess(aProc, Result);
      // free Ressource
      CloseHandle(aProc);
    end;
    

    【讨论】:

      【解决方案2】:

      ShellExecute 是一个直接的 WinAPI 函数。要获取有关执行进程的任何信息,您需要改用ShellExecuteEx

      【讨论】:

      • 然后在 LPSHELLEXECUTEINFO.hProcess 上使用 WaitForSingleObject
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多