Cómo ejecutar un programa y esperar a que termine

El código siguiente es una función que permite ejecutar un programa y esperar a que termine, obteniendo el código de retorno.

int WinExecAndWait32(AnsiString FileName, int Visibility)
{
  char zAppName[512];
  char zCurDir[255];
  AnsiString WorkDir;
  TStartupInfo StartupInfo;
  TProcessInformation ProcessInfo;

  strcpy(zAppName, FileName.c_str());
  WorkDir = GetCurrentDir();
  strcpy(zCurDir, WorkDir.c_str());
  setmem(&StartupInfo, sizeof(StartupInfo), 0);

  StartupInfo.cb = sizeof(StartupInfo);
  StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow = Visibility;

  if (!CreateProcess(NULL, zAppName, // pointer to command line string
    NULL, // pointer to process security attributes
    NULL, // pointer to thread security attributes
    false, // handle inheritance flag
    CREATE_NEW_CONSOLE | // creation flags
    NORMAL_PRIORITY_CLASS,
    NULL, // pointer to new environment block
    NULL, // pointer to current directory name
    &StartupInfo, // pointer to STARTUPINFO
    &ProcessInfo))
{
  return -1; // pointer to PROCESS_INF
}
  else
  {
  WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
  DWORD ExitCode;
  GetExitCodeProcess(ProcessInfo.hProcess, &ExitCode);
  return ExitCode;
  }