How to get Exec'ed exit in Inno Setup?

Can I get the output of the Exec file?

I want to show the user an information request page, but in the input field specify the default MAC address value. Is there any other way to achieve this?

+27
inno-setup
Jul 16 '09 at 10:40
source share
2 answers

Yes, use redirecting standard output to a file:

 [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; 

Please note that there may be several network adapters and therefore several MAC addresses to choose from.

+32
Jul 16 '09 at 11:31
source share

I had to do the same (execute command line calls and get the result) and came up with a more general solution.

It also corrects strange errors if the quoted paths are used in real calls using the /S flag for cmd.exe .

 { 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; 

Using:

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

The result can also be loaded into the TStringList object to get all the rows:

 Lines := TStringList.Create; Lines.Text := ExecStdout; { ... some code ... } Lines.Free; 
+12
Dec 03 '15 at 15:40
source share



All Articles