Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi ShellExecute, and Wait!! (https://www.delphipraxis.net/31067-shellexecute-wait.html)

moritz 3. Okt 2004 20:31


ShellExecute, and Wait!!
 
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"

jensw_2000 3. Okt 2004 22:05

Re: ShellExecute, and Wait!!
 
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

supermuckl 3. Okt 2004 22:09

Re: ShellExecute, and Wait!!
 
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

moritz 4. Okt 2004 07:22

Re: ShellExecute, and Wait!!
 
Hallo,

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

Gruß, moritz

Robert Marquardt 4. Okt 2004 07:26

Re: ShellExecute, and Wait!!
 
Funktioniert das "Info.lpDirectory := PChar(Directory);" nicht?
Das sollte doch die working directory des zu startenden Prozesses sein.

moritz 4. Okt 2004 07:27

Re: ShellExecute, and Wait!!
 
Oha, das habe ich glatt übersehen! Danke! :drunken:


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:10 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