Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#4

AW: Too stupid to execute and wait

  Alt 9. Aug 2011, 10:41
Funktioniert wunderbar:
Delphi-Quellcode:
uses
  ShellAPI;

procedure ExecAndWait(Filename, Params: Widestring; WindowState: word = SW_SHOWNORMAL);
var
  ShExecInfo: SHELLEXECUTEINFOW;
const
  SEE_MASK_NOASYNC= $100;
begin
  ZeroMemory(@ShExecInfo, SizeOf(ShExecInfo));
  ShExecInfo.Wnd := GetForegroundWindow;
  ShExecInfo.cbSize := sizeof(SHELLEXECUTEINFOW);
  ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC;
  ShExecInfo.lpVerb := 'open';
  ShExecInfo.lpFile := PWideChar(Filename);
  ShExecInfo.lpParameters := PWideChar(Params);
  ShExecInfo.lpDirectory := PWideChar(WideString(ExtractFileDir(Filename)));
  ShExecInfo.nShow := WindowState;
  if ShellExecuteExW(@ShExecInfo) then
  begin
    WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
    CloseHandle(ShExecInfo.hProcess);
  end
  else
    RaiseLastOSError;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    ExecAndWait('C:\Windows\Notepad.exe', '');
  except
    on E: Exception do
      ShowMessage(E.Message);
  end;
  ShowMessage('Fertig');
end;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat