1
votes

I have several batch files which I execute from Inno Setup. I use ShellExecuteEx() to execute batch files:

function ShellExecuteWait(const Verb, Filename, Params, WorkingDir: string; const ShowCmd: integer; const Timeout: cardinal; var ExitCode: DWORD): boolean;
var
  ExecInfo: TShellExecuteInfo;
begin
  Result := false;

  ExecInfo.cbSize := SizeOf(ExecInfo);
  ExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  ExecInfo.Wnd := 0;
  ExecInfo.lpVerb := Verb;
  ExecInfo.lpFile := Filename;
  ExecInfo.lpParameters := Params;
  ExecInfo.lpDirectory := WorkingDir;
  ExecInfo.nShow := ShowCmd;

  if ShellExecuteEx(ExecInfo) then
  begin
    if WaitForSingleObject(ExecInfo.hProcess, Timeout) = WAIT_TIMEOUT then
    begin
      TerminateProcess(ExecInfo.hProcess, WAIT_TIMEOUT);
    end else begin
    end;
    GetExitCodeProcess(ExecInfo.hProcess, ExitCode);
    Result := (ExitCode = ERROR_SUCCESS);
  end else begin
    ExitCode := GetLastError();
  end;
end;

if (not ShellExecuteWait('', 
                         Command,
                         Params,
                         ExpandConstant('{tmp}'), 
                         SW_SHOW, 
                         Timeout,
                         ResultCode)) then...

But no matter what I try, I cannot get the exit code from the batch file in ResultCode (I always get back 0).

In researching this problem, I have read that the batch must NOT use exit /b NN. So I removed the /b switch, but I still always get 0.

What must I do to successfully get the exit code from a batch file?

Dennis

1
Any reason you use ShellExecuteEx and not plain ShellExec?Martin Prikryl

1 Answers

0
votes

I found the problem. The command to invoke the batch file includes a pipe through another program, so the result code I get back is actually coming from the program through which the script's output is piped.