如何获得在Inno安装程序执行程序的输出?

是否有可能得到Exec的可执行文件的输出?

我想向用户显示信息查询页面,但在input框中显示MAC地址的默认值。 有没有其他办法可以做到这一点?

是的,使用标准输出redirect到一个文件:

 [Code] function NextButtonClick(CurPage: Integer): Boolean; var TmpFileName, ExecStdout: string; ResultCode: integer; begin if CurPage = wpWelcome then begin TmpFileName := ExpandConstant('{tmp}') + '\ipconfig_results.txt'; Exec('cmd.exe', '/C ipconfig /ALL > "' + TmpFileName + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); if LoadStringFromFile(TmpFileName, ExecStdout) then begin MsgBox(ExecStdout, mbInformation, MB_OK); { do something with contents of file... } end; DeleteFile(TmpFileName); end; Result := True; end; 

请注意,可能有多个networking适配器,因此可能有多个MAC地址可供​​select。

我必须这样做(执行命令行调用,并得到结果),并提出了一个更通用的解决scheme。

它还修复了如果在实际调用中使用带有/S标志的cmd.exe引用的path的奇怪的错误。

 { Exec with output stored in result. } { ResultString will only be altered if True is returned. } function ExecWithResult(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer; var ResultString: String): Boolean; var TempFilename: String; Command: String; begin TempFilename := ExpandConstant('{tmp}\~execwithresult.txt'); { Exec via cmd and redirect output to file. Must use special string-behavior to work. } Command := Format('"%s" /S /C ""%s" %s > "%s""', [ ExpandConstant('{cmd}'), Filename, Params, TempFilename]); Result := Exec(ExpandConstant('{cmd}'), Command, WorkingDir, ShowCmd, Wait, ResultCode); if not Result then Exit; LoadStringFromFile(TempFilename, ResultString); { Cannot fail } DeleteFile(TempFilename); { Remove new-line at the end } if (Length(ResultString) >= 2) and (ResultString[Length(ResultString) - 1] = #13) and (ResultString[Length(ResultString)] = #10) then Delete(ResultString, Length(ResultString) - 1, 2); end; 

用法:

 Success := ExecWithResult('ipconfig', '/all', '', SW_HIDE, ewWaitUntilTerminated, ResultCode, ExecStdout) or (ResultCode <> 0); 

结果也可以加载到一个TStringList对象来获取所有行:

 Lines := TStringList.Create; Lines.Text := ExecStdout; { ... some code ... } Lines.Free;