Inno Script: Skip password if the application is already installed - inno-setup

I am trying to create an Inno Setup installer that will require a password from the user if the application has never been installed on the local machine.
I have the script that gets a password, and I have a Code section that checks for the existence of the uninstall registry key, but being new to Inno Setup scripting, I'm not sure how to link the two parts together.
Can anyone explain how to forgo the user from entering a password if the app is already installed?
Here is the (test) script...
#define myAppID "2B7D6E48-74A8-4070-8BA7-621115D6FD00"
[Setup]
AppId={{{#myAppID}}
Password=123456
[Code]
function checkForPreviousInstall(): Boolean;
begin
Result := False;
if RegKeyExists(HKEY_LOCAL_MACHINE,'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{#myAppId}_is1') or
RegKeyExists(HKEY_CURRENT_USER, 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{#myAppId}_is1') then
begin
MsgBox('The application is installed already.', mbInformation, MB_OK);
Result := True;
end;
end;

Skip the password page, if the application is installed already.
Use ShouldSkipPage event function:
function ShouldSkipPage(PageID: Integer): Boolean;
begin
Result := False;
if (PageID = wpPassword) and checkForPreviousInstall then Result := True;
end;

Related

Run application after click on the Finish button (not after install)

I have a setup for installing my application, and I need to run the application after successfully installing. I used postinstall to do this.
but it shows a checkbox and the user can uncheck it. I need to run the application without asking because of it kinda service which needs to runs at startup. if the user unchecked it he needs to restart the PC to launch.
So I can use the Filename: "{app}\myapp.exe" code without any flags in the Run section to launch the application but the problem is, It runs immediately after installing not after the finish button clicked.
The first issue is my application has an instruction window. it shows up at the launch so the setup window goes to the back. And the second issue is my application does not allow terminating unless uninstall becouse it need to run in the background. Setup waiting to process end to finish.
Is there any way to run the application after the finish button click in inno setup?
Simplifying the answer from Run Files and Programs according to custom checkboxes after clicking on Finish Button in Inno Setup, you can use a code like this:
[Code]
function NextButtonClick(CurPageID: Integer): Boolean;
var
ResultCode: Integer;
Path, Msg: string;
begin
if CurPageID = wpFinished then
begin
Path := ExpandConstant('{app}\MyProg.exe');
if ExecAsOriginalUser(Path, '', '', SW_SHOW, ewNoWait, ResultCode) then
begin
Log('Executed MyProg');
end
else
begin
Msg := 'Error executing MyProg - ' + SysErrorMessage(ResultCode);
MsgBox(Msg, mbError, MB_OK);
end;
end;
Result := True;
end;
Replace ExecAsOriginalUser with Exec, if you want to run the program with elevated/Administrator privileges (if the installer uses them at all).
Add code section to your script like this:
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
ResultCode: Integer;
begin
if CurStep = ssDone then
Exec(ExpandConstant('{app}\MyProg.exe'), '', '', SW_SHOW, ewNoWait, ResultCode);
end;
It will be triggered only on a success installation.
Use ExecAsOriginalUser instead of Exec if you don't want the exe run as admin.

Inno Setup: Checking existence of a file in 32-bit System32 (Sysnative) folder

I have a sys file in the System32\Drivers folder called gpiotom.sys (custom sys file). My my application is strictly 32-bit compatible only, so my installer runs in 32-bit mode. My script needs to find if this sys file exists or not.
I used the FileExists function explained on the below post but it does not work since it only works for 64-bit application only:
InnoSetup (Pascal): FileExists() doesn't find every file
Is there any way I can find if my sys file exists or not in a 32-bit mode?
Here is my code snippet in Pascal Script language:
function Is_Present() : Boolean;
begin
Result := False;
if FileExists('{sys}\driver\gpiotom.sys') then
begin
Log('File exists');
Result := True;
end;
end;
In general, I do not think there is any problem running an installer for 32-bit application in 64-bit mode. Just make sure you use 32-bit paths where necessary, like:
[Setup]
DefaultDirName={pf32}\My Program
Anyway, if you want to stick with 32-bit mode, you can use EnableFsRedirection function to disable WOW64 file system redirection.
With use of this function, you can implement a replacement for FileExists:
function System32FileExists(FileName: string): Boolean;
var
OldState: Boolean;
begin
if IsWin64 then
begin
Log('64-bit system');
OldState := EnableFsRedirection(False);
if OldState then Log('Disabled WOW64 file system redirection');
try
Result := FileExists(FileName);
finally
EnableFsRedirection(OldState);
if OldState then Log('Resumed WOW64 file system redirection');
end;
end
else
begin
Log('32-bit system');
Result := FileExists(FileName);
end;
if Result then
Log(Format('File %s exists', [FileName]))
else
Log(Format('File %s does not exists', [FileName]));
end;

Perform DeleteFile only on NEW installation [duplicate]

My InnoSetup script opens a web page (with the user's default browser) at the end of the install process:
[Run]
Filename: http://example.com; Flags: shellexec
However, I'd like the web page to not be opened if the app already exists, i.e., if the user is installing a new version of the program. The web page should only be opened after the initial install. (I assume it's worth mentioning that the install includes an AppID, obviously, and enters values in the registry beside installing files.)
Thank you, as always -- Al C.
Yes, this is easy to do with scripting.
Just write
[Run]
Filename: "http://example.com"; Flags: shellexec; Check: NotAnUpdate
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = wpInstalling then
IsUpdate := FileExists(ExpandConstant('{app}\TheFileNameOfMyApp.exe'));
end;
function NotAnUpdate: Boolean;
begin
result := not IsUpdate;
end;
The answer by #AndreasRejbrand won't work, if user chooses to install the executable to a different location than the last time.
You can query installer-specific Inno Setup registry keys:
#define AppId "your-app-id"
#define SetupReg \
"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
#define SetupAppPathReg "Inno Setup: App Path"
[Setup]
AppId={#AppId}
...
[Run]
Filename: "https://www.example.com/"; Flags: shellexec; Check: not IsUpgrade
...
[Code]
function IsUpgrade: Boolean;
var
S: string;
begin
Result :=
RegQueryStringValue(HKLM, '{#SetupReg}', '{#SetupAppPathReg}', S) or
RegQueryStringValue(HKCU, '{#SetupReg}', '{#SetupAppPathReg}', S);
end;
For an example how to use IsUpgrade in [Code] section, see
Excludes part of Code section in ssPostInstall step if installation is update in Inno Setup
Check this if your "AppId" contains a left-curly-bracket:
Checking if installation is fresh or upgrade does not work when AppId contains a curly bracket

Check condition before extracting files inno

I want to check if there is a certain file before starting to extract, because if there is that file installation must stop. Is it possible?
I just found a way; I've used the CurStepChanged event method and wait there for CurStep=ssInstall which indicates the installation process is about to start. At that time I check if the file exists, and if so, I terminate the setup process:
[Code]
procedure ExitProcess(uExitCode: UINT);
external 'ExitProcess#kernel32.dll stdcall';
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssInstall then
begin
if FileExists(ExpandConstant('{app}\.versionC204v1')) then
begin
MsgBox('A patched version detected. Setup will now exit.', mbInformation, MB_OK);
ExitProcess(0);
end;
end;
end;

Can Inno Setup respond differently to a new install and an update?

My InnoSetup script opens a web page (with the user's default browser) at the end of the install process:
[Run]
Filename: http://example.com; Flags: shellexec
However, I'd like the web page to not be opened if the app already exists, i.e., if the user is installing a new version of the program. The web page should only be opened after the initial install. (I assume it's worth mentioning that the install includes an AppID, obviously, and enters values in the registry beside installing files.)
Thank you, as always -- Al C.
Yes, this is easy to do with scripting.
Just write
[Run]
Filename: "http://example.com"; Flags: shellexec; Check: NotAnUpdate
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = wpInstalling then
IsUpdate := FileExists(ExpandConstant('{app}\TheFileNameOfMyApp.exe'));
end;
function NotAnUpdate: Boolean;
begin
result := not IsUpdate;
end;
The answer by #AndreasRejbrand won't work, if user chooses to install the executable to a different location than the last time.
You can query installer-specific Inno Setup registry keys:
#define AppId "your-app-id"
#define SetupReg \
"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
#define SetupAppPathReg "Inno Setup: App Path"
[Setup]
AppId={#AppId}
...
[Run]
Filename: "https://www.example.com/"; Flags: shellexec; Check: not IsUpgrade
...
[Code]
function IsUpgrade: Boolean;
var
S: string;
begin
Result :=
RegQueryStringValue(HKLM, '{#SetupReg}', '{#SetupAppPathReg}', S) or
RegQueryStringValue(HKCU, '{#SetupReg}', '{#SetupAppPathReg}', S);
end;
For an example how to use IsUpgrade in [Code] section, see
Excludes part of Code section in ssPostInstall step if installation is update in Inno Setup
Check this if your "AppId" contains a left-curly-bracket:
Checking if installation is fresh or upgrade does not work when AppId contains a curly bracket

Resources