繁体   English   中英

如何判断外部应用程序何时以delphi结尾

[英]how to tell when an external application ends in delphi

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

在这里我的代码

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

尝试以下功能。 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;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM