In my Inno Setup project, I download all files from the server and also download a file that has a version number. Now I want to read the version from the file and assign it to [Setup] section AppVersion in the Code section. My question is that possible to set the app version in the Code section?
Combining these two questions:
How to set version number from Inno Setup Pascal Script
Inno Setup - HTTP request - Get www/web content
[Setup]
AppVersion={code:GetAppVersion}
[Code]
var
Version: string;
function GetAppVersion(Param: string): string;
var
WinHttpReq: Variant;
begin
if Version = '' then
begin
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.Open('GET', 'https://www.example.com/version.txt', False);
WinHttpReq.Send('');
if WinHttpReq.Status <> 200 then
begin
Log('HTTP Error: ' + IntToStr(WinHttpReq.Status) + ' ' + WinHttpReq.StatusText);
MsgBox('Cannot obtain version', mbError, MB_OK);
Abort();
end
else
begin
Version := Trim(WinHttpReq.ResponseText);
Log('Version: ' + Version);
// you may want to validate that the value is meaningful here
end;
end;
Result := Version;
end;
Related
I am looking for a code for Inno Setup, that I can use to make my setup verify my permission to install the program. This code must check a text file on a web server.
If the file has the value "False", the setup has to cancel an installation and save the value in a registry file to cancel it always when Internet connection is not available.
If the file has the value "True", the setup will continue to install, and it will delete the registry file value if it exists.
If there is no Internet and the registry value does not exist the setup will continue to install.
Use InitializeSetup event function to trigger your check using HTTP request.
[Code]
const
SubkeyName = 'Software\My Program';
AllowInstallationValue = 'Allow Installation';
function IsInstallationAllowed: Boolean;
var
Url: string;
WinHttpReq: Variant;
S: string;
ResultDWord: Cardinal;
begin
try
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
Url := 'https://www.example.com/can_install.txt';
WinHttpReq.Open('GET', Url, False);
WinHttpReq.Send('');
if WinHttpReq.Status <> 200 then
begin
RaiseException(
'HTTP Error: ' + IntToStr(WinHttpReq.Status) + ' ' + WinHttpReq.StatusText);
end
else
begin
S := Trim(WinHttpReq.ResponseText);
Log('HTTP Response: ' + S);
Result := (CompareText(S, 'true') = 0);
if RegWriteDWordValue(
HKLM, SubkeyName, AllowInstallationValue, Integer(Result)) then
Log('Cached response to registry')
else
Log('Error caching response to registry');
end;
except
Log('Error: ' + GetExceptionMessage);
if RegQueryDWordValue(HKLM, SubkeyName, AllowInstallationValue, ResultDWord) then
begin
Log('Online check failed, using cached result');
Result := (ResultDWord <> 0);
end
else
begin
Log('Online check failed, no cached result, allowing installation by default');
Result := True;
end;
end;
if Result then Log('Can install')
else Log('Cannot install');
end;
function InitializeSetup(): Boolean;
begin
Result := True;
if not IsInstallationAllowed then
begin
MsgBox('You cannot install this', mbError, MB_OK);
Result := False;
end;
end;
We prepare installers using Inno Setup. So a user installs the software. When new version is released, a new installer updates the software. So far so good.
But some people want to have both old version of the software and new one.
Is it possible to make an installer ask if a user wants to update current installation or install new version side by side.
In InitializeSetup event function, detect if the application is installed already. If it is, ask user, and if he/she chooses to install a new copy, change AppId and DefaultDirName to version-specific values to force a new installation.
[Setup]
#define AppId "My Program"
#define SetupReg "Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
#define DisplayVersionReg "DisplayVersion"
#define ApplicationVersion() \
ParseVersion('MyProg.exe', Local[0], Local[1], Local[2], Local[3]), \
Str(Local[0]) + "." + Str(Local[1])
[Setup]
AppId={code:GetAppId}
AppName=My Program
AppVersion={#ApplicationVersion}
DefaultDirName={code:GetDefaultDirName}
UsePreviousLanguage=no
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
[Code]
var
AppId: string;
DefaultDirName: string;
function GetAppId(Param: string): string;
begin
Result := AppId;
Log('AppId = ' + Result);
end;
function GetDefaultDirName(Param: string): string;
begin
Result := DefaultDirName;
Log('DefaultDirName = ' + Result);
end;
function InitializeSetup(): Boolean;
var
PrevVersion: string;
CurVersion: string;
Message: string;
R: Integer;
begin
CurVersion := '{#ApplicationVersion}';
Log(Format('Installing "%s"', [CurVersion]));
Result := True;
AppId := '{#AppId}';
DefaultDirName := ExpandConstant('{pf}\My Program');
if RegQueryStringValue(HKLM, '{#SetupReg}', '{#DisplayVersionReg}', PrevVersion) or
RegQueryStringValue(HKCU, '{#SetupReg}', '{#DisplayVersionReg}', PrevVersion) then
begin
Message :=
Format(
'Version is %s already installed. Do you want to upgrade to %s?'#13#10#13#10+
'Press Yes, to replace %0:s with %1:s.'#13#10+
'Press No, to keep %0:s and add separate installation of %1:s.'#13#10, [
PrevVersion, CurVersion]);
R := MsgBox(Message, mbConfirmation, MB_YESNOCANCEL);
if R = IDYES then
begin
Log('User chose to replace previous installation');
end
else
if R = IDNO then
begin
AppId := AppId + CurVersion;
DefaultDirName := DefaultDirName + ' ' + CurVersion;
Log('User chose to install new copy - using ID ' + AppId);
end
else
begin
Log('User chose to cancel installation');
Result := False;
end;
end;
end;
How do I get installed Version of MS office Excel from registry using inno Script? i tried bellow code,it gives 'Key not found' but it exist
function InitializeSetup(): Boolean;
var
CurVer: Cardinal;
key: string;
if RegQueryDWordValue(HKCR, 'Excel.Application\CurVer\','Default', CurVer) then
begin
// Successfully read the value
MsgBox('Excel Version: ' + IntTOStr(CurVer),mbInformation, MB_OK);
end else begin
MsgBox('Key not found',mbInformation, MB_OK);
end;
end;
changed RegQueryDWordValue to RegQueryStringValue
function InitializeSetup(): Boolean;
var
CurVer: Cardinal;
key: string;
begin
//if RegQueryDWordValue(HKCR, 'Excel.Application\\CurVer\\','', CurVer) then
if RegQueryStringValue(HKCR, 'Excel.Application\CurVer\','', key) then
begin
// Successfully read the value
MsgBox('Excel Version: ' + key,mbInformation, MB_OK);
end else begin
MsgBox('Excel Not installed',mbInformation, MB_OK);
end;
end;
Remove the trailing backslash.
Also, the (Default) value in RegEdit is the one with no name:
if RegQueryDWordValue(HKCR, 'Excel.Application\CurVer','', CurVer) then
I'm new to Inno, but want to check '{pf32}\Google\Drive\googledrivesync.exe' is installed before install.
However, the code does not pick up {pf32} as it does in Check: for a file.
New some can you assist please
Michael
[Code]
function InitializeSetup(): Boolean;
var FilePath3, FP1: String;
begin
//Result := FileExists(FilePath3);
//FP1:= {pf32} FilePath3:= '\Google\Drive\googledrivesync.exe'
if not FileExists({pf32} + FilePath3) Then
begin MsgBox({pf32} + FilePath3 + 'Google Drive not installed correctly - Setup will now exit - Please reinstall Google Drive!', mbError, MB_OK);
abort;
end;
end;
You have to use ExpandConstant in order to...well...expand constants. :-)
function InitializeSetup(): Boolean;
var
FilePath3: String;
PFDir: string;
begin
PFDir := ExpandConstant('{pf32}');
FilePath3:= '\Google\Drive\googledrivesync.exe'
Result := FileExists(PFDir + FilePath3);
if not Result then
begin
MsgBox(PFDir + FilePath3 + #13#13 +
'Google Drive not installed' +
'correctly - Setup will now exit.'#13 +
'Please reinstall Google Drive!', mbError, MB_OK);
Abort;
end;
end;
NOTE: I don't recall off-hand if ExpandConstant includes the trailing backslash or not, so you'll have to test. If so, you'll need to remove the backslash that starts FilePath3.
I have just started using inno setup, and it seems to work well. However, when I run the installer with the app already installed it reinstalls. I would like to give the user to uninstall. Is this possible, and if so, how can it be done?
To be specific, I have written a game for a homework assignment. I made an installer using inno setup. The app installs fine and can be uninstalled using the control panel, but my professor would like to be able to uninstall the application by re-running the installer and choosing an uninstall option. This will save him time since he has about 50 of these assignments to mark.
Thanks,
Gerry
The next script will make the following options form when the application is already installed on the target system when the setup is started:
When the user clicks Repair button, the setup is normally started. When user clicks the Uninstall button, the previously installed application is uninstalled. When user closes that form, nothing happens.
Here is the script (don't forget to specify, ideally some unique, value for the AppId setup directive in your script):
[Setup]
AppName=My Program
AppVersion=1.5
AppId=1C9FAC66-219F-445B-8863-20DEAF8BB5CC
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
[CustomMessages]
OptionsFormCaption=Setup options...
RepairButtonCaption=Repair
UninstallButtonCaption=Uninstall
[Code]
const
mrRepair = 100;
mrUninstall = 101;
function ShowOptionsForm: TModalResult;
var
OptionsForm: TSetupForm;
RepairButton: TNewButton;
UninstallButton: TNewButton;
begin
Result := mrNone;
OptionsForm := CreateCustomForm;
try
OptionsForm.Width := 220;
OptionsForm.Caption := ExpandConstant('{cm:OptionsFormCaption}');
OptionsForm.Position := poScreenCenter;
RepairButton := TNewButton.Create(OptionsForm);
RepairButton.Parent := OptionsForm;
RepairButton.Left := 8;
RepairButton.Top := 8;
RepairButton.Width := OptionsForm.ClientWidth - 16;
RepairButton.Caption := ExpandConstant('{cm:RepairButtonCaption}');
RepairButton.ModalResult := mrRepair;
UninstallButton := TNewButton.Create(OptionsForm);
UninstallButton.Parent := OptionsForm;
UninstallButton.Left := 8;
UninstallButton.Top := RepairButton.Top + RepairButton.Height + 8;
UninstallButton.Width := OptionsForm.ClientWidth - 16;
UninstallButton.Caption := ExpandConstant('{cm:UninstallButtonCaption}');
UninstallButton.ModalResult := mrUninstall;
OptionsForm.ClientHeight := RepairButton.Height + UninstallButton.Height + 24;
Result := OptionsForm.ShowModal;
finally
OptionsForm.Free;
end;
end;
function GetUninstallerPath: string;
var
RegKey: string;
begin
Result := '';
RegKey := Format('%s\%s_is1', ['Software\Microsoft\Windows\CurrentVersion\Uninstall',
'{#emit SetupSetting("AppId")}']);
if not RegQueryStringValue(HKEY_LOCAL_MACHINE, RegKey, 'UninstallString', Result) then
RegQueryStringValue(HKEY_CURRENT_USER, RegKey, 'UninstallString', Result);
end;
function InitializeSetup: Boolean;
var
UninstPath: string;
ResultCode: Integer;
begin
Result := True;
UninstPath := RemoveQuotes(GetUninstallerPath);
if UninstPath <> '' then
begin
case ShowOptionsForm of
mrRepair: Result := True;
mrUninstall:
begin
Result := False;
if not Exec(UninstPath, '', '', SW_SHOW, ewNoWait, ResultCode) then
MsgBox(FmtMessage(SetupMessage(msgUninstallOpenError), [UninstPath]), mbError, MB_OK);
end;
else
Result := False;
end;
end;
end;
For some reason your code
RegKey := Format('%s\%s_is1', ['Software\Microsoft\Windows\CurrentVersion\Uninstall',
'{#emit SetupSetting("AppId")}']);
returned an extra { to the _is1 value. I didn't had the time to check why or where i was wrong in my implementation,
all i confirm is that my installer works with the
RegKey := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1');
alternate.
Hope it helps.
Thank you for the code sample.
When using Inno Setup, there's no reason to uninstall a previous version unless that version was installed by a different installer program. Otherwise upgrades are handled automatically.
Your answer is here :
InnoSetup: How to automatically uninstall previous installed version? previous-installed-version