使用ShellExecuteEx从Inno Setup中的批处理文件中获取退出代码

时间:2016-01-23 00:57:54

标签: batch-file inno-setup exit-code shellexecuteex

我有几个批处理文件,我从Inno Setup执行。我使用ShellExecuteEx()来执行批处理文件:

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...

但无论我尝试什么,我都无法从ResultCode中的批处理文件中获取退出代码(我总是回到0)。

在研究这个问题时,我已经读过批次不能使用exit /b NN。所以我删除了/b开关,但我仍然总是0。

如何从批处理文件中成功获取退出代码?

丹尼斯

1 个答案:

答案 0 :(得分:0)

我发现了问题。调用批处理文件的命令包括通过另一个程序的管道,因此我得到的结果代码实际上来自脚本输出管道的程序。