Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inno Setup - How to copy a file before setup start?

Tags:

inno-setup

I need to copy a file to one folder, before Inno Setup starts or before the "select directory" page. I want this file to be copied from the installer and not from an external source.

I am using this code:

function NextButtonClick(PageID: Integer): Boolean;
begin
  Result := True;
  if (PageId = wpWelcome) then
  begin
    FileCopy(
      ExpandConstant('file.exe'),
      ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\file.exe'), false);
  end;
end;
like image 636
Dielo Avatar asked Nov 24 '12 00:11

Dielo


2 Answers

To extract a file from the setup archive any time you need you'll have to use ExtractTemporaryFile procedure. This procedure extracts the file from the [Files] section to a temporary directory used by the setup application, which you can find on the path specified by the {tmp} constant. Then you'll just copy such extracted file to a target directory from there by expanding the mentioned constant.

If you want to do something when the setup is being initialized, but before the wizard form is created, use the InitializeSetup event function. Note, that you can even exit the setup from that function without seeing the wizard form e.g. if the file you're going to copy is critical that much. Here's a sample code, but first take a look at the commented version of it for some details:

[Code]
function InitializeSetup: Boolean;
begin
  Result := True;
  ExtractTemporaryFile('File.exe');
  if FileCopy(ExpandConstant('{tmp}\File.exe'), 
    ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\File.exe'), False) 
  then
    MsgBox('File copying succeeded!', mbInformation, MB_OK)
  else
    MsgBox('File copying failed!', mbError, MB_OK)  
end;
like image 98
TLama Avatar answered Nov 05 '22 17:11

TLama


You'll need to Extract the file, first to a temporary directory, then copy it to where you want. Something like this should work:

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "My Program"
#define MyAppVersion "1.5"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "MyProg.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{5820E516-8DD7-4481-A016-63D3F00438C8}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "C:\Program Files (x86)\Inno Setup 5\Examples\MyProg.exe"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, "&", "&&")}}"; Flags: nowait postinstall skipifsilent


[Code]
function InitializeSetup: Boolean;
var
  S: AnsiString;
begin
  // Show the contents of Readme.txt (non Unicode) in a message box
  log('Before Extract');
  ExtractTemporaryFile('myprog.exe');
  log('Before FileCopy. Dest:' + ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\file.exe'));
  log('temp: ' + ExpandConstant('{tmp}\myprog.exe'));
  FileCopy(ExpandConstant('{tmp}\myprog.exe'), ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\file.exe'), false);
  log('After FileCopy');
  Result := True;
end;
like image 31
mirtheil Avatar answered Nov 05 '22 18:11

mirtheil