Inno Setup: Create application valid for one year - inno-setup

I test my software with new code.
const MY_EXPIRY_DATE_STR = '20131112'; //Date format: yyyymmdd
function InitializeSetup(): Boolean;
var
ErrorCode: Integer;
begin
//If current date exceeds MY_EXPIRY_DATE_STR then return false and exit Installer.
result := CompareStr(GetDateTimeString('yyyymmdd', #0,#0), MY_EXPIRY_DATE_STR) <= 0;
if not result then
begin
MsgBox('Now it''s forbidden to install this program', mbError, MB_OK);
end
if (MsgBox('Autocad will compulsory closed,so please save your drawings and then press OK', mbConfirmation, MB_OK) = IDOK) then
begin
ShellExec('open', 'taskkill.exe', '/f /im acad.exe','', SW_HIDE, ewNoWait, ErrorCode);
ShellExec('open', 'tskill.exe', ' ACAD', '', SW_HIDE, ewNoWait, ErrorCode);
Result := True;
end
else
begin
Result := False;
end;
end;
The issue is the setup show the error message (Now it's forbidden to install this program) but it continue install. I want it exit the installer.

You're forgetting to return from the function when your expiry condition is met.
This
if not result then
begin
MsgBox('Now it''s forbidden to install this program', mbError, MB_OK);
end
should be:
if not Result then
begin
MsgBox('Now it''s forbidden to install this program', mbError, MB_OK);
Exit;
end;
Without the Exit, the following statements execute with the possibility of setting Result to 'True' again.
Notice also the formatting. If you had it right, there is a good chance that you wouldn't be asking this question.

Related

How to verify if program is installed in system with Inno Setup Pascal Script?

I want to verify if user has Docker installed in their system.
If it is installed proceed further otherwise display an error message.
Previously I was looking at the registry group in Windows but it's not the correct way.
I want to check if cmd gives correct output for command docker.
function GetHKLM: Integer;
begin
if IsWin64 then
Result := HKLM64
else
Result := HKLM32;
end;
function GetHKU: Integer;
begin
if IsWin64 then
Result := HKCU64
else
Result := HKCU32;
end;
function InitializeSetup: Boolean;
begin
// Opening the setup installer initially
Result := True;
//if the docker is present in the machine registry return True else checking user registry
if not RegKeyExists(GetHKLM, 'SOFTWARE\Docker Inc.') then
begin
if not RegKeyExists(GetHKU, 'Software\Docker Inc.') then
begin
// return False to prevent installation to continue
Result := False;
// Display that you need to install docker.
SuppressibleMsgBox('<Docker not found!>', mbCriticalError, MB_OK, IDOK);
end;
end;
end;
How do I do this with just cmd? Instead of checking registry.. How can I run the command line and verify the output?
for etc:
function checkdocker() :Boolean;
var
dockerfound: string;
begin
Result :=
ShellExecute(application.handle, 'docker', nil, nil, SW_MAXIMIZE)
end;
function InitializeSetup: Boolean;
begin
Result := True;
if not checkdocker then;
SuppressibleMsgBox('<Docker not found!>', mbCriticalError, MB_OK, IDOK);
else
#continue
end;
To answer your literal question: Just use Exec and check the result code:
function CheckDocker: Boolean;
var
ResultCode: Integer;
begin
Result :=
Exec('docker', '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and
(ResultCode = 0);
if Result then Log('Succeeded executing docker')
else Log('Failed to execute docker');
end;
(based on How to get an output of an Exec'ed program in Inno Setup?)
Though there's more efficient way to check if docker.exe executable is in a search path. Use FileSearch. See How can I check SQLCMD.EXE if it is installed on client in Inno Setup.

How to detect if I need to prompt user to restart PC after installing VC Redist package(s)

I had an issue on someone’s computer yesterday where my installer did not complete as expected. I connected to their PC to investigate.
It had to do with the VC Redist files. My installer detects if these are needed and if required, downloads and installs them.
I use the command line switches so that the user is not prompted at all and the I use the /norestart flag as my installer has to complete.
The problem was that VC Redist needed them to restart the PC. I found this out by running my installer, it downloaded the Redist setup and attempted to install. My setup aborted. So I tried to manually run the VC Redist Setup and that is where I saw it say the pc needed a reboot.
So, how can I either test if the Redist requires a pc restart and then get my installer to convey this to the user, or, how can I just prompt the user to user if either of the VC Redist setups had been spawned?
I did find this:
https://johnkoerner.com/install/windows-installer-error-codes/
And I noticed the last entry:
ERROR_SUCCESS_REBOOT_REQUIRED 3010
A restart is required to complete the install. This message is
indicative of a success. This does not include installs where the
ForceReboot action is run.
0xBC2 0x80070BC2
This is how I spawn the installers at the moment:
procedure CurStepChanged(CurStep: TSetupStep);
var
ResultCode: integer;
strLogFilePathName, strLogFileName, strNewFilePathName: string;
begin
if (CurStep = ssDone) then
begin
strLogFilePathName := ExpandConstant('{log}');
strLogFileName := ExtractFileName(strLogFilePathName);
strNewFilePathName := ExpandConstant('{#CommonDataDir}\Installation Logs\') + strLogFileName;
FileCopy(strLogFilePathName, strNewFilePathName, false);
end
else if (CurStep = ssPostInstall) then
begin
if (dotNetNeeded) then
begin
(* See here for command line switches:
https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ee942965(v=vs.100)
Do I use passive or showfinalerror? *)
if Exec(ExpandConstant(dotnetRedistPath), '/passive', '',
SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin
{ handle success if necessary; ResultCode contains the exit code }
if not (ResultCode = 0) then begin
(* Microsoft present an array of options for this. But since
The interface was visible I think it is safe to just say
that the installation was not completed. *)
MsgBox(ExpandConstant('{cm:InstallFailed,Microsoft .NET Framework 4.6.2}'), mbInformation, MB_OK);
Abort();
end;
end
else begin
{ The execution failed for some reason }
MsgBox(SysErrorMessage(ResultCode), mbInformation, MB_OK);
Abort();
end;
end;
if (bVcRedist64BitNeeded) then
begin
if Exec(ExpandConstant(vcRedist64BitPath), '/install /passive /norestart', '',
SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin
{ handle success if necessary; ResultCode contains the exit code }
if not (ResultCode = 0) then begin
MsgBox(ExpandConstant('{cm:InstallFailed,Visual Studio x64 Redistributable}'), mbInformation, MB_OK);
Abort();
end;
end
else begin
{ The execution failed for some reason }
MsgBox(SysErrorMessage(ResultCode), mbInformation, MB_OK);
Abort();
end;
end;
if (bVcRedist32BitNeeded) then
begin
if Exec(ExpandConstant(vcRedist32BitPath), '/install /passive /norestart', '',
SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin
{ Handle success if necessary; ResultCode contains the exit code }
if not (ResultCode = 0) then begin
MsgBox(ExpandConstant('{cm:InstallFailed,Visual Studio x86 Redistributable}'), mbInformation, MB_OK);
Abort();
end;
end
else begin
{ The execution failed for some reason }
MsgBox(SysErrorMessage(ResultCode), mbInformation, MB_OK);
Abort();
end;
end;
end;
end;

Verify permissions to run Inno Setup installer online

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;

Inno Setup, show a msgbox on the folder selection step

I am trying to display a message box only when the user reach the folder selection page, here are the actual code that display the message box at the beginning of the setup:
[code]
var ApplicationPath: string;
function GetAppPath(Param: String): String;
begin
RegQueryStringValue(HKLM, 'SOFTWARE\XXX\XXX', 'Install Dir', ApplicationPath)
if ApplicationPath = '' then
begin
MsgBox('Install folder non found', mbError, MB_OK);
result:=ApplicationPath;
end
else
MsgBox('Install folder found in "' + ApplicationPath + '". NOTE: close the program before proceeding.', mbInformation, MB_OK);
result:=ApplicationPath;
end;
end.
I need something like:
If (PageId = wpSelectDir) then...
[run the above code]
but really I don't know how, thanks for your help.
The ideal event for this is the CurPageChanged. You can use it this way to run a code, when the select directory page is shown:
[Code]
procedure CurPageChanged(CurPageID: Integer);
var
AppPath: string;
begin
if (CurPageID = wpSelectDir) then
begin
// this will query the string value in registry; if that succeed and the
// value is read, then the message box about success is shown, otherwise
// the error message box about failure is shown
if RegQueryStringValue(HKLM, 'SOFTWARE\XXX\XXX', 'Install Dir', AppPath) then
MsgBox('Installation folder found...', mbInformation, MB_OK)
else
MsgBox('Installation folder not found...', mbError, MB_OK);
end;
end;

Inno Setup - Display MessageBox to run additional file

I am new to Inno Setup and having difficulty find this answer...
I have included a DirectX9 setup file in the installer, but I want to display a MessageBox to the user to ask "Do you want to install DirectX9?" that is done before the regular installation of my game... if he says yes, then I want to run this additional file that I included, but otherwise just proceed to install the game.
The following code will run just before the installation begins. It asks for confirmation from the user and then runs "InstallDirectX.exe" (which must be available to the installer).
[Code]
function PrepareToInstall(var NeedsRestart: Boolean): String;
var
ResultCode: integer;
begin
if (msgbox('Do you want to install DirectX ?', mbConfirmation, MB_YESNO) = IDYES) then
begin
if Exec(ExpandConstant('InstallDirectX.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
// handle success if necessary; ResultCode contains the exit code
MsgBox('Everything is proceeding according to plan', mbInformation, MB_OK);
end
else
begin
// handle failure if necessary; ResultCode contains the error code
MsgBox('Something went horribly wrong', mbError, MB_OK);
end;
end;
end;
If you want to display message box when installation finished, you can use this code:
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
ResultCode: Integer;
begin
if CurStep = ssPostInstall then
begin
if (msgbox('Do you want to install DirectX ?', mbConfirmation, MB_YESNO) = IDYES) then
begin
if Exec(ExpandConstant('{src}\dxwebsetup.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
MsgBox('Installing DirectX completed', mbInformation, MB_OK);
end
else
begin
MsgBox('Installing Error', mbError, MB_OK);
end;
end;
end;
end;

Resources