uses
WinTypes, WinProcs, SysUtils;
{ WindowState is one of the SW_xxx constants.
Look up ShowWindow in the API help for a list.}
function ExecAndWait(
const Filename, Params:
string;
WindowState: word): boolean;
{$IFDEF WIN32}
var
SUInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine:
string;
begin
{ Enclose filename in quotes to take care of
long filenames with spaces. }
CmdLine := '
"' + Filename + '
" ' + Params;
FillChar(SUInfo, SizeOf(SUInfo), #0);
with SUInfo
do begin
cb := SizeOf(SUInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := WindowState;
end;
Result := CreateProcess(
NIL, PChar(CmdLine),
NIL,
NIL, FALSE,
CREATE_NEW_CONSOLE
or
NORMAL_PRIORITY_CLASS,
NIL,
PChar(ExtractFilePath(Filename)),
SUInfo, ProcInfo);
{ Wait for it to finish. }
if Result
then
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
{$ELSE}
var
InstanceID : THandle;
Buff:
array[0..255]
of char;
begin
StrPCopy(Buff, Filename + '
' + Params);
InstanceID := WinExec(Buff, WindowState);
if InstanceID < 32
then
{ a value less than 32 indicates an Exec error }
Result := FALSE
else begin
Result := TRUE;
repeat
Application.ProcessMessages;
until Application.Terminated
or
(GetModuleUsage(InstanceID) = 0);
end;
{$ENDIF}
end;