2016-01-23 85 views
1

我有幾個批處理文件,我從Inno Setup執行。我用ShellExecuteEx()執行批處理文件:使用ShellExecuteEx從Inno Setup中的批處理文件獲取退出代碼

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

我必須做什麼才能成功獲得從一個批處理文件的退出代碼?

Dennis

+0

您使用'ShellExecuteEx',而不是簡單的['ShellExec'任何原因(http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_shellexec)? –

回答

0

我發現了這個問題。調用批處理文件的命令包含通過另一個程序的管道,所以我返回的結果代碼實際上來自腳本輸出管道的程序。

相關問題