Thema: Delphi ShellExecute, and Wait!!

Einzelnen Beitrag anzeigen

moritz

Registriert seit: 18. Apr 2003
1.037 Beiträge
 
#1

ShellExecute, and Wait!!

  Alt 3. Okt 2004, 20:31
Hi Leute,

ich hab ein kleines Problem mit ShellExecute. Ich muss ein Programm asuführen und warten bis es beendet ist, zudem muss ich ein Working Direcotry übermitteln. Ich habe mal bei SwissDelphiCenter gesucht und mehrere Tipps gefunden (Suchwort "warten"), wovon jedoch nur einer ein Working Directory unterstüzt:
Delphi-Quellcode:
function ShellExecute_AndWait(Operation, FileName, Parameter, Directory: string;
  Show: Word; bWait: Boolean): Longint;
var
  bOK: Boolean;
  Info: TShellExecuteInfo;
{
  ****** Parameters ******
  Operation:

  edit  Launches an editor and opens the document for editing.
  explore Explores the folder specified by lpFile.
  find Initiates a search starting from the specified directory.
  open Opens the file, folder specified by the lpFile parameter.
  print Prints the document file specified by lpFile.
  properties Displays the file or folder's properties.

  FileName:

  Specifies the name of the file or object on which
  ShellExecuteEx will perform the action specified by the lpVerb parameter.

  Parameter:

  String that contains the application parameters.
  The parameters must be separated by spaces.

  Directory:

  specifies the name of the working directory.
  If this member is not specified, the current directory is used as the working directory.

  Show:

  Flags that specify how an application is to be shown when it is opened.
  It can be one of the SW_ values

  bWait:

  If true, the function waits for the process to terminate
}

begin
  FillChar(Info, SizeOf(Info), Chr(0));
  Info.cbSize := SizeOf(Info);
  Info.fMask := SEE_MASK_NOCLOSEPROCESS;
  Info.lpVerb := PChar(Operation);
  Info.lpFile := PChar(FileName);
  Info.lpParameters := PChar(Parameter);
  Info.lpDirectory := PChar(Directory);
  Info.nShow := Show;
  bOK := Boolean(ShellExecuteEx(@Info));
  if bOK then
  begin
    if bWait then
    begin
      while
        WaitForSingleObject(Info.hProcess, 100) = WAIT_TIMEOUT
        do Application.ProcessMessages;
      bOK := GetExitCodeProcess(Info.hProcess, DWORD(Result));
    end
    else
      Result := 0;
  end;
  if not bOK then Result := -1;
end;
(http://swissdelphicenter.ch/de/showcode.php?id=93)

Soweit so gut, unter XP funktioniert das auch sehr gut. Allerdings funtkionert diese Funktion unter niedrigen Versionen nicht mehr. Ich hab ein paar Lösungsansätze theorethisch im Kopf, allerdings weiß ich nicht wie ich sie parktisch umsetzten soll und ob sie überhaupt umsetzbar sind:
1) Die Instance holen (Rückgabe einer FUnktion) und warten, bis er das Programm nicht mehr findet
2) Das Working Directory irgendwie anders setzen
Wisst ihr andere Lösungen bzw. wie man eine dieser umsetzten könnte?

Freue mich über Hilfe!

Gruß, Moritz

P.S.: Im auszuführenden Programm kann ich nichts ändern. Also à la "Baue ein Code im Program ein der ne Nachricht sendet wenn es fertig ist"
"Optimistisch ist diejenige Weltanschauung, die das Sein höher als das Nichts stellt und so die Welt und das Leben als etwas an sich Wertvolles bejaht."
Albert Schweitzer
  Mit Zitat antworten Zitat