AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

ShellExecute, and Wait!!

Ein Thema von moritz · begonnen am 3. Okt 2004 · letzter Beitrag vom 4. Okt 2004
Antwort Antwort
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
jensw_2000
(Gast)

n/a Beiträge
 
#2

Re: ShellExecute, and Wait!!

  Alt 3. Okt 2004, 22:05
Hi,

ich habe mal diese "RunAndWait" Procedure in einem kleinen Projekt verbaut.
Das Programm wurde auf Win98, WinNT, Win2000 und WinXP Rechnern verwendet. Die Procedure lief problemlos ...
Unter Win95 und ME habe ich es leider noch nie testen können.

Eventuell ist das ja was für dich...


Code:
procedure RunAndWaitShell(Executable, Parameter: STRING; ShowParameter: INTEGER);
var
  Info: TShellExecuteInfo;
  pInfo: PShellExecuteInfo;
  exitCode: DWord;
begin
  {Pointer to Info}
  pInfo := @Info;
  {Fill info}
  with Info do
  begin
    cbSize := SizeOf(Info);
    fMask := SEE_MASK_NOCLOSEPROCESS;
    wnd   := application.Handle;
    lpVerb := NIL;
    lpFile := PChar(Executable);
    {Parametros al ejecutable}
    {Executable parameters}
    lpParameters := PChar(Parameter + #0);
    lpDirectory := NIL;
    nShow       := ShowParameter;
    hInstApp    := 0;
  end;
  {Execute}
  ShellExecuteEx(pInfo);

  {Wait to finish}
  repeat
    exitCode := WaitForSingleObject(Info.hProcess, 500);
    Application.ProcessMessages;
  until (exitCode <> WAIT_TIMEOUT);
end;

Gruß,
Jens
  Mit Zitat antworten Zitat
supermuckl

Registriert seit: 1. Feb 2003
1.340 Beiträge
 
FreePascal / Lazarus
 
#3

Re: ShellExecute, and Wait!!

  Alt 3. Okt 2004, 22:09
für diese warteschleife und überprüfen hab ich da auch was aus nem quelltext übersetzt

Delphi-Quellcode:
 GetExitCodeProcess(pi.hProcess,exitcode); //while the process is running
     if (exitcode <> STILL_ACTIVE) then break;
das geht auch. evtl anderst als WaitForSingleObject falls das das problem ist

evtl auch mal da schauen: http://www.delphipraxis.net/internal...ct.php?t=35979
Das echte Leben ist was für Leute...
... die im Internet keine Freunde finden!
  Mit Zitat antworten Zitat
moritz

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

Re: ShellExecute, and Wait!!

  Alt 4. Okt 2004, 07:22
Hallo,

Danke. Aber wie Lösoe ich das Problem mit dem Working DIrectory?

Gruß, moritz
"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
Robert Marquardt
(Gast)

n/a Beiträge
 
#5

Re: ShellExecute, and Wait!!

  Alt 4. Okt 2004, 07:26
Funktioniert das "Info.lpDirectory := PChar(Directory);" nicht?
Das sollte doch die working directory des zu startenden Prozesses sein.
  Mit Zitat antworten Zitat
moritz

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

Re: ShellExecute, and Wait!!

  Alt 4. Okt 2004, 07:27
Oha, das habe ich glatt übersehen! Danke!
"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
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:19 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz