; Inno Setup Compiler 5.0.8
; ISPP 5.0.6.0
;retrieve version of the program from exe
#define AppVersion GetFileVersion(AddBackslash(SourcePath) + "MyProg.exe")
[Setup]
AppName=Upgrade
AppID=Upgrade
;use the version in AppVerName
AppVerName=Upgrade {#AppVersion}
;output file name include the version
OutputBaseFilename=Ugrade_{#AppVersion}
;the setup exe has same version
VersionInfoVersion={#AppVersion}
VersionInfoDescription=Upgrade {#AppVersion}
; set the default path where the application was previusly installed GetPrevDir
DefaultDirName={code:GetPrevDir|{pf}\Upgrade}
DisableProgramGroupPage=true
DisableStartupPrompt=true
[Files]
;Backup MyProg.exe
Source: {app}\MyProg.exe; DestDir: {code:GetBckdir|''}; Flags: external skipifsourcedoesntexist
; Install MyProg.exe
Source: MyProg.exe; DestDir: {app}
[Code]
procedure DecodeVersion( verstr: String; var verint: array of Integer );
var
i,p: Integer; s: string;
begin
// initialize array
verint := [0,0,0,0];
i := 0;
while ( (Length(verstr) > 0) and (i < 4) ) do
begin
p := pos('.', verstr);
if p > 0 then
begin
if p = 1 then s:= '0' else s:= Copy( verstr, 1, p - 1 );
verint[i] := StrToInt(s);
i := i + 1;
verstr := Copy( verstr, p+1, Length(verstr));
end
else
begin
verint[i] := StrToInt( verstr );
verstr := '';
end;
end;
end;
// This function compares version string
// return -1 if ver1 < ver2
// return 0 if ver1 = ver2
// return 1 if ver1 > ver2
function CompareVersion( ver1, ver2: String ) : Integer;
var
verint1, verint2: array of Integer;
i: integer;
begin
SetArrayLength( verint1, 4 );
DecodeVersion( ver1, verint1 );
SetArrayLength( verint2, 4 );
DecodeVersion( ver2, verint2 );
Result := 0; i := 0;
while ( (Result = 0) and ( i < 4 ) ) do
begin
if verint1[i] > verint2[i] then
Result := 1
else
if verint1[i] < verint2[i] then
Result := -1
else
Result := 0;
i := i + 1;
end;
end;
// Get where the application was installed
function GetPathInstalled( AppID: String ): String;
var
sPrevPath: String;
begin
sPrevPath := '';
if not RegQueryStringValue( HKLM,
'Software\Microsoft\Windows\CurrentVersion\Uninstall'+AppID+'_is1',
'Inno Setup: App Path', sPrevpath) then
RegQueryStringValue( HKCU, 'Software\Microsoft\Windows\CurrentVersion\Uninstall'+AppID+'_is1' ,
'Inno Setup: App Path', sPrevpath);
Result := sPrevPath;
end;
const THIS_VERSION = '{#AppVersion}';
var
// previuos install dir
PrevDir: String;
// tell if it's an upgrade
Upgrade: Boolean;
// installed program version
InstalledVer: String;
// backupdir
BckDir: String;
function InitializeSetup(): Boolean;
var
vercomp, answ : Integer;
begin
Result := true;
// read the installtion folder
PrevDir := GetPathInstalled( '{#SetupSetting("AppID")}' );
if length( Prevdir ) > 0 then begin
// I found the folder so
// It's an upgrade
Upgrade := true;
// read the version of MyProg.exe already installed
GetVersionNumbersString( PrevDir + '\MyProg.exe' , InstalledVer );
// compare versions
vercomp := CompareVersion( InstalledVer, THIS_VERSION );
// version smaller
if vercomp < 0 then
begin
// keep Result as true and go on with upgrade
end;
// same version ask what to do
if vercomp = 0 then
begin
answ := MsgBox( 'Found same version of {#SetupSetting("AppName")} ('+ InstalledVer +').'
+ #13#13 + 'Do you want to upgrade anyway ?',
mbConfirmation, MB_YESNO )
// If user answer is NO abort setup
Result := ( answ = IDYES );
end;
// version bigger, cannot upgrade with a smaller version
if vercomp > 0 then
begin
MsgBox('Found '+ InstalledVer +' version of {#SetupSetting("AppName")} '+
' and you want to install ' + THIS_VERSION
+ #13#13 + 'Setup/Upgrade aborted.', mbError, MB_OK );
Result := false;
end;
end
else begin
MsgBox( 'No installation found of MyProg.exe, upgrade aborted.',
mbInformation, MB_OK );
// abort setup
Result := false;
end;
// backup dir has the program version found
BckDir := Prevdir + '\Backup' + InstalledVer;
end;
function GetPrevDir( s: String ) : String;
begin
if length( PrevDir ) = 0 then
Result := s
else
Result := PrevDir;
end;
function GetBckDir( s: String): String;
begin
ForceDirectories( BckDir );
Result := BckDir;
end;
function ShouldSkipPage(PageID: Integer): Boolean;
begin
// skip selectdir if It's an upgrade
if ( PageID = wpSelectDir ) and Upgrade then
Result := true
else
Result := false;
end;