How can I copy the installer to the temp file and

2020-07-31 03:26发布

问题:

I am trying to copy the installer to the temp folder and then run it from that location.

This is what I am trying to do, but so far to no avail.

FileCopy(ExpandConstant('{srcexe}'), ExpandConstant('{tmp}\Setup.exe'), True);
Exec(ExpandConstant('{tmp}\Setup.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)

How can I copy the installer to the temp file and then run it from the code section?

回答1:

No idea why, but there's an explicit check that prevents FileCopy function from copying the installer itself.

See a PathCompare check in FileCopy branch of CmnFunc2Proc in ScriptFunc_R.pas:

end else if Proc.Name = 'FILECOPY' then begin
  ExistingFilename := Stack.GetString(PStart-1);
  if PathCompare(ExistingFilename, SetupLdrOriginalFilename) <> 0 then
    Stack.SetBool(PStart, CopyFileRedir(ScriptFuncDisableFsRedir,
      ExistingFilename, Stack.GetString(PStart-2), Stack.GetBool(PStart-3)))
  else
    Stack.SetBool(PStart, False);

I do not know what is the reason for this, but there probably is some. So beware, you might be doing something that the installer does not like.


You can of course workaround that by calling Windows copy command:

Exec(
  ExpandConstant('{cmd}'),
  Format('/C copy "%s" "%s"', [ExpandConstant('{srcexe}'), ExpandConstant('{tmp}\Setup.exe')]),
  '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

Or you can implement a replacement function with use of TFileStream class:

function FileCopyUnrestricted(const ExistingFile, NewFile: String): Boolean;
var
  Buffer: string;
  Stream: TFileStream;
  Size: Integer;
begin
  Result := True;
  try
    Stream := TFileStream.Create(ExistingFile, fmOpenRead or fmShareDenyNone);
    try
      Size := Stream.Size;
      SetLength(Buffer, Size div 2 + 1);
      Stream.ReadBuffer(Buffer, Size);
    finally
      Stream.Free;
    end;
  except
    Result := False;
  end;

  if Result then
  begin
    try
      Stream := TFileStream.Create(NewFile, fmCreate);
      try
        Stream.WriteBuffer(Buffer, Size);
      finally
        Stream.Free;
      end;
    except
      Result := False;
    end;
  end;
end;

This is an inefficient implementation that loads whole file to memory. If your installer is large, you will have to improve the code to copy the file in chunks.

The code is for Unicode version of Inno Setup (the only version as of Inno Setup 6).