2894:WinExecAndWait
KEYWORDS: WinExecAndWait AREA: WinAPI
Q: How do I execute a program and have my code wait until it is finished?
A:
uses Wintypes,WinProcs,Toolhelp,Classes,Forms;
Function WinExecAndWait(Path : string; Visibility : word) : word;
var
InstanceID : THandle;
PathLen : integer;
begin
{ inplace conversion of a String to a PChar }
PathLen := Length(Path);
Move(Path[1],Path[0],PathLen);
Path[PathLen] := #0;
{ Try to run the application }
InstanceID := WinExec(@Path,Visibility);
if InstanceID < 32 then { a value less than 32 indicates an Exec error }
WinExecAndWait := InstanceID
else
begin
Repeat
Application.ProcessMessages;
until Application.Terminated or (GetModuleUsage(InstanceID) = 0);
WinExecAndWait := 32;
end;
end;
TI