inno-setupdocumentationpascalscript

Why do we specify the path in ExtractTemporaryFiles function in Inno Setup?


I've been reading through Inno Setup documentation and got confused about why do we specify the path in ExtractTemporaryFiles function if according to the documentation it extracts the files matching the wildcard specified by Pattern from the [Files] section to a temporary directory.? If we have this Source: "Readme.txt"; Flags: dontcopy in the [Files] section why do we type ExtractTemporaryFiles('{tmp}\Readme.txt') in the [Code] section? Is that a mistake or I just don't understand how this function works? The same applies to the ExtractTemporaryFile procedure.

Here's the code from ExtractTemporyFile page:

[Files]
Source: "Readme.txt"; Flags: dontcopy noencryption

[Code]
function InitializeSetup: Boolean;
var
  S: AnsiString;
begin
  // Show the contents of Readme.txt (non Unicode) in a message box
  ExtractTemporaryFile('Readme.txt');
  if LoadStringFromFile(ExpandConstant('{tmp}\Readme.txt'), S) then
  begin
    MsgBox(S, mbInformation, MB_OK);
  end;

  Result := True;
end;

And here's the code from ExtractTemporaryFiles page:

[Files]
Source: "Readme.txt"; Flags: dontcopy
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "{app}"

[Code]
function InitializeSetup: Boolean;
var
  S: AnsiString;
  ResultCode: Integer;
begin
  // Show the contents of Readme.txt (non Unicode) in a message box
  ExtractTemporaryFiles('{tmp}\Readme.txt');
  if LoadStringFromFile(ExpandConstant('{tmp}\Readme.txt'), S) then
  begin
    MsgBox(S, mbInformation, MB_OK);
  end;

  // Extract all MyProg files and launch it. Note how {app} is left unexpanded.
  ExtractTemporaryFiles('{app}\MyProg.*');
  ExecAsOriginalUser(ExpandConstant('{tmp}\')+'{app}\MyProg.exe', '', '',
    SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);

  Result := True;
end;

In the first case we have:

[Files]
Source: "Readme.txt"; Flags: dontcopy noencryption
[Code]
ExtractTemporaryFile('Readme.txt');

And in the second one we have:

[Files]
Source: "Readme.txt"; Flags: dontcopy
[Code]
ExtractTemporaryFiles('{tmp}\Readme.txt');

What's the difference between those two? (noencryption flag does not count) Why do we need to specify the {tmp} constant in the second case if we already extracting from there? In order to be able to use those methods correctly I need to have a clear understanding of the syntax.


Solution

  • The specific answer to your question:

    Why do we type ExtractTemporaryFiles('{tmp}\Readme.txt') in the [Code] section?

    The {tmp} reference in the ExtractTemporaryFiles example code is apparently required for the ExtractTemporaryFiles function, although this is undocumented. The {tmp} reference is not present in the documentation sample code for the ExtractTemporaryFile procedure:

    [Files]
    Source: "Readme.txt"; Flags: dontcopy noencryption
    
    [Code]
    function InitializeSetup: Boolean;
    var
      S: AnsiString;
    begin
      // Show the contents of Readme.txt (non Unicode) in a message box
      ExtractTemporaryFile('Readme.txt');
      if LoadStringFromFile(ExpandConstant('{tmp}\Readme.txt'), S) then
      begin
        MsgBox(S, mbInformation, MB_OK);
      end;
    
      Result := True;
    end;
    

    The only differences that I can see between ExtractTemporaryFile and ExtractTemporaryFiles are:

    1. ExtractTemporaryFile only extracts one file and has no return value

    2. ExtractTemporaryFiles extracts filenames based on a wildcard, and returns type integer (number of files extracted), and apparently requires a directory name prefix to disambiguate the filename(s) being extracted (although this is not documented)