![]() |
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; ![]() 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" |
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 |
Re: ShellExecute, and Wait!!
für diese warteschleife und überprüfen hab ich da auch was aus nem quelltext übersetzt
Delphi-Quellcode:
das geht auch. evtl anderst als WaitForSingleObject falls das das problem ist
GetExitCodeProcess(pi.hProcess,exitcode); //while the process is running
if (exitcode <> STILL_ACTIVE) then break; evtl auch mal da schauen: ![]() |
Re: ShellExecute, and Wait!!
Hallo,
Danke. Aber wie Lösoe ich das Problem mit dem Working DIrectory? Gruß, moritz |
Re: ShellExecute, and Wait!!
Funktioniert das "Info.lpDirectory := PChar(Directory);" nicht?
Das sollte doch die working directory des zu startenden Prozesses sein. |
Re: ShellExecute, and Wait!!
Oha, das habe ich glatt übersehen! Danke! :drunken:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:03 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz