Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

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

AW: Too stupid to execute and wait

  Alt 9. Aug 2011, 13:27
Delphi-Quellcode:
uses
  ShellAPI;

procedure ExecAndWait(Filename, Params: string; WindowState: word = SW_SHOWNORMAL);
var
  ShExecInfo: SHELLEXECUTEINFO;
  RetValue: DWORD;
const
  SEE_MASK_NOASYNC= $100;
begin
  ZeroMemory(@ShExecInfo, SizeOf(ShExecInfo));
  ShExecInfo.Wnd := Application.Handle ; //GetForegroundWindow;
  ShExecInfo.cbSize := sizeof(SHELLEXECUTEINFO);
  ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC;
  ShExecInfo.lpVerb := 'open';
  ShExecInfo.lpFile := PChar(Filename);
  ShExecInfo.lpParameters := PChar(Params);
  ShExecInfo.lpDirectory := PChar(ExtractFileDir(Filename));
  ShExecInfo.nShow := WindowState;
  if ShellExecuteEx(@ShExecInfo) then
  begin
    repeat
      RetValue := MsgWaitForMultipleObjects(1, ShExecInfo.hProcess, False, INFINITE, QS_ALLINPUT);
      if RetValue <> WAIT_OBJECT_0 then
        Application.ProcessMessages;
    until RetValue = WAIT_OBJECT_0;
    CloseHandle(ShExecInfo.hProcess);
  end
  else
    RaiseLastOSError;
end;

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

Geändert von Luckie ( 9. Aug 2011 um 14:05 Uhr)
  Mit Zitat antworten Zitat