0

Based on How to get an output of an Exec'ed program in Inno Setup? I've following method:

function ExecWithResult(const Command, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer; var ResultString: AnsiString): Boolean;
var
  TempFilename: String;
  CommandFull: String;
begin
  log('ExecWithResult - begin'); 
  log('Command: ' + Command); 
  TempFilename := ExpandConstant('{tmp}\cmdOutput.txt');  
  // Command := Format('%s" /S /C ""%s" %s > "%s"', [ExpandConstant('{cmd}'), Filename, Params, TempFilename]); // command wrapped with extra double quotes
  CommandFull := ExpandConstant('{cmd}') + ' /S /C "' + Command + '" > "' + TempFilename + '"';
  log('CommandFull: ' + CommandFull);
  Result := Exec(ExpandConstant('{cmd}'), CommandFull, WorkingDir, ShowCmd, Wait, ResultCode);
  LoadStringFromFile(TempFilename, ResultString);
  log('ResultCode: ' + IntToStr(ResultCode));
  log('ResultString: ' + ResultString);
  // DeleteFile(TempFilename);
  log('ExecWithResult - end');  
end;

The file cmdOutput.txt is empty and ResultCode is 1. If I execute the CommandFull manualy in cmd, %errorlevel% is 0 and cmdOutput.txt has desired content.

From the log file:

2023-02-10 14:31:41.075   ExecWithResult - begin
2023-02-10 14:31:41.075   Command: fsutil fsinfo sectorinfo C:
2023-02-10 14:31:41.075   CommandFull: C:\Windows\system32\cmd.exe /S /C "fsutil fsinfo sectorinfo C:" > "C:\Users\admin\AppData\Local\Temp\is-4FCD4.tmp\cmdOutput.txt"
2023-02-10 14:31:41.184   ResultCode: 1
2023-02-10 14:31:41.184   ResultString: 
2023-02-10 14:31:41.184   ExecWithResult - end

And screen of manual execution manual execution

Any idea what's wrong with the script?

UPDATE & SOLUTION:

Thanks to @Martin Prikryl for the 'kick', the original code is fine except LoadStringFromFile(TempFilename, ResultString);. ResultString has to be AnsiString.

With LoadStringFromFile and StringChangeEx from Unicode Inno Setup (Ansi file) is this working:

function ExecWithResult(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; 
                        var ResultCode: Integer; var ResultString: String; var ResultAnsiString: AnsiString): Boolean;
var OutputFilePath, Command: String;
begin
  OutputFilePath := ExpandConstant('{tmp}\cmdOutput.txt');
  Command := Format('"%s" /S /C ""%s" %s > "%s""', [ExpandConstant('{cmd}'), Filename, Params, OutputFilePath]);
  Result := Exec(ExpandConstant('{cmd}'), Command, WorkingDir, ShowCmd, Wait, ResultCode);
  (*
  if not Result then
    Exit;
  *)
  if LoadStringFromFile(OutputFilePath, ResultAnsiString) then
  begin
    ResultString := String(ResultAnsiString);
  end;
  // DeleteFile(TempFilename);
  if (Length(ResultString) >= 2) and (ResultString[Length(ResultString) - 1] = #13) and
     (ResultString[Length(ResultString)] = #10) then
    Delete(ResultString, Length(ResultString) - 1, 2);
end;

with usage:

function MyFunction: Boolean;
var OutputText: String;
var OutputAnsiText: AnsiString;
var ResultCode: Integer;
var Success: Boolean;
var SysDrive, Command, Args: String;
begin
  Command := 'fsutil';
  SysDrive := ExpandConstant('{sd}');
  Args:= 'fsinfo sectorinfo ' + SysDrive;
  Success := ExecWithResult(Command, Args, '', SW_HIDE, ewWaitUntilTerminated, ResultCode, OutputText, OutputAnsiText);
  { decision based on OutputText}
  Result := True; // or false by decision
end;
pburgr
  • 1,722
  • 1
  • 11
  • 26

0 Answers0