简体   繁体   中英

How does output redirection work in Inno Setup?

I saw this question here: How to get an output of an Exec'ed program in Inno Setup?

But I can't get it to work myself, the commented out code are my attempts to make this work, but I resorted to a bat file because I couldn't make my redirection work. CacheInstanceName and CacheInstanceDir are global variable defined elsewhere:

function CheckCacheExists(): Integer;
var
  args: String;
  buffer: String;
  ResultCode: Integer;
begin
  // args := 'qlist ' + CacheInstanceName + ExpandConstant(' nodisplay > {tmp}\appcheck.txt');
  // MsgBox(args, mbInformation, MB_OK);
  // Exec(CacheInstanceDir + '\bin\ccontrol.exe', 'qlist ' + CacheInstanceName + ExpandConstant(' nodisplay > "{tmp}\appcheck.txt"'), '', SW_SHOW,

  ExtractTemporaryFile('checkup.BAT');
  Exec(ExpandConstant('{tmp}\checkup.BAT'), CacheInstanceDir + ' ' + 
    CacheInstanceName + ' ' + ExpandConstant('{tmp}'), '', SW_SHOW,
    ewWaitUntilTerminated, ResultCode);
  LoadStringFromFile(ExpandConstant('{tmp}\appcheck.txt'),buffer);
  if Pos('^', buffer) = 0 then
  begin
    Result := 0
  end
  else 
  begin
    Result := 1
  end 
end;

What am I doing wrong?

The output redirection syntax is a feature of the command prompt, not the core Windows APIs. Therefore if you want to redirect output then you need to invoke the command via {cmd} /c actual-command-line > output-file . Don't forget to include quotes where appropriate, as {tmp} (and other constants) may contain spaces.

However, you should strongly consider rewriting whatever is in that batch file into actual code. Anything you can do in a batch file you can do either directly in the Inno script or in a DLL that you call from the script. And this permits you greater control over error checking and the format of whatever data you want to retrieve.

Try running the command directly on your command line with the arguments in your args string to see what the result is which may give an indication of the problem.

Also, check that the file you are trying to redirect your output to is not in use by another process. I have found that when this occurs the actual command may execute successfully with the Exec command returning True but the ResultCode indicates an error and no output is written to the file used in the redirect. In this particular instance of the file being used by another instance the SysErrorMessage(ResultCode) command returns simply Incorrect function . However, testing directly on the command line as I mentioned first returns that the file is in use by another process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM