how to tell when an external application ends in delphi

六眼飞鱼酱① 提交于 2019-12-24 00:56:51

问题


I am using ShellExecute to run external application How can i tell when the external application ends ?

Here my code

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

回答1:


Try the following function. WaitForSingleObject does what you need.

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 is a direct WinAPI function. To obtain any information on the executed process, you need to use ShellExecuteEx instead.



来源:https://stackoverflow.com/questions/2102890/how-to-tell-when-an-external-application-ends-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!