Execute And Wait For Application     D1 D2 D3
A function to execute another application and wait for it to close before continuing using WinExec and GetModuleUsage (Delphi 1), or CreateProcess and WaitForSingleObject (Delphi 2 and 3). Thanks to numerous people on Usenet for variations on this.
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;