5

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?

Community
  • 1
  • 1
codeymccodeface
  • 372
  • 3
  • 11
  • As a side note, after further testing I found that only the first parameter "qlist" is ever getting passed. I've been banging my head trying to think what I could be doing wrong, but maybe it has nothing to do with redirection. Also, I can see in the debugger that all the variables are populated and they obviously show up properly in the message box. – codeymccodeface Jul 12 '12 at 00:52
  • 1
    Could you be more specific, *can't get it to work* is not a good way to describe your problem. Have you tried to check what returns the `Exec` function in your `ResultCode` ? Also the `Exec` function has a return value and if it fails (returns False) then you have most probably passed wrong parameters to it. – TLama Jul 12 '12 at 00:52
  • TLama, your comment is apt, hence my comment above. – codeymccodeface Jul 12 '12 at 00:53

2 Answers2

10

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.

Miral
  • 12,637
  • 4
  • 53
  • 93
0

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.

JasonMcF
  • 632
  • 8
  • 18