频道分类

Delphi 使用ShellExecuteEx运行应用程序并等待完成

作者:admin 来源: 日期:2019/12/1 22:24:29 人气: 标签:

 

uses Winapi.ShellAPI;


{ Runs application using ShellExecuteEx and waits for completion. }
function ShellExecuteAndWait(FileName: String; Params: String): Boolean;
var
  exInfo: TShellExecuteInfo;
  Ph: DWORD;
begin
  FillChar(exInfo, SizeOf(exInfo), 0);
  with exInfo do
  begin
    cbSize := SizeOf(exInfo);
    fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
    Wnd := GetActiveWindow();
    exInfo.lpVerb := 'open';
    exInfo.lpParameters := PChar(Params);
    lpFile := PChar(FileName);
    nShow := SW_SHOWNORMAL;
  end;
  if ShellExecuteEx(@exInfo) then
    Ph := exInfo.hProcess
  else
  begin
    ShowMessage(SysErrorMessage(GetLastError));
    Result := true;
    exit;
  end;
  while WaitForSingleObject(exInfo.hProcess, 50) <> WAIT_OBJECT_0 do
    Application.ProcessMessages;
  CloseHandle(Ph);
  Result := true;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin

ShellExecuteAndWait('regedit.exe','');

end;

上一篇:Delphi 关闭SQL Server 数据库所有使用连接下一篇:没有资料