Page 1 of 1

Inno setup features how to use inno setup to check if a program is already installed

#1 aniri  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Posted 22 February 2010 - 04:59 AM

Inno Setup is a free installer for Windows programs. It can be used to create an installer for your program.

The main features a generated installer has are:

  • it creates a single exe file you can use to install your application on other computers
  • you can add customizable setup types
  • you can specify uninstall options
  • you can choose what shortcuts to be created when installing the program
  • you can create registry entries
  • you can run other programs before, during or after the install
  • you can add advanced install and uninstall options using the integrated pascal scripting engine


I will not go into the details of creating a simple installer. Inno setup also has a Script Wizard that can be used to generate the script for the installer.

Here’s an example of a generated script:


[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={{E80CC92C-D4F2-4E90-988E-0134482AFB87}
AppName=My Program
AppVerName=My Program 1.5
AppPublisher=My Company, Inc.
AppPublisherURL=http://www.example.com/
AppSupportURL=http://www.example.com/
AppUpdatesURL=http://www.example.com/
DefaultDirName={pf}\My Program
DefaultGroupName=My Program
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\Inno Setup 5\Examples\MyProg.exe"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"
Name: "{commondesktop}\My Program"; Filename: "{app}\MyProg.exe"; Tasks: desktopicon

[Run]
Filename: "{app}\MyProg.exe"; Description: "{cm:LaunchProgram,My Program}"; Flags: nowait postinstall skipifsilent



This can be used to install a program on your computer, in the folder specified by the user.

Some of the programs you make might require some other applications to be installed to be able to run. What I am going to show in this tutorial is how to use inno setup to write an installer script that checks if a program is installed on the computer and, if not, it installs the necessary program before resuming the installer.

The script I wrote checks if the 3.5 version of .NET framework is installed.

For this, I added a code section in the script and wrote a function to do the checking.

The function looks like this:

 
[CustomMessages]
dotnetmissing=This application needs Microsoft .Net Version 3.5. Do you like to install it now?
[Code]
function InitializeSetup(): Boolean;
var
ErrorCode: Integer;
ResultCode: Integer;
netFrameWorkInstalled : Boolean;
isInstalled: Cardinal;
begin
result := true;

// Check for the .Net 3.5 framework
isInstalled := 0;
netFrameworkInstalled := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', 'Install', isInstalled);

if ((netFrameworkInstalled)  and (isInstalled <> 1)) then netFrameworkInstalled := false;

if netFrameworkInstalled = false then
begin
if (MsgBox(ExpandConstant('{cm:dotnetmissing}'),
mbConfirmation, MB_YESNO) = idYes) then
begin
if Exec(ExpandConstant('dotNetFx35setup.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
// check if .net was installed properly
isInstalled := 0;
netFrameworkInstalled := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', 'Install', isInstalled);
if ((netFrameworkInstalled)  and (isInstalled <> 1)) then netFrameworkInstalled := false;

if netFrameworkInstalled = false then
// not installed
begin
MsgBox('.NET 3.5 was not installed properly. Please restart the installer.', mbConfirmation, MB_OK);
result := false;
end
// installed
else begin
MsgBox('.NET 3.5 was installed properly.', mbConfirmation, MB_OK);
result := true;
end;
end
else begin
MsgBox('An error occurer while installing .NET 3.5. Please restart the installer.', mbConfirmation, MB_OK);
result := false;
end;

end
else begin
result := false;
end;
end;
end;



The InitializeSetup function is called before running the setup. If it returns true, the setup continues, if not, the setup ends.

First, we check if .NET 3.5 is installed. For this, we check the appropriate registry key:

netFrameworkInstalled := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', 'Install', isInstalled);



The RegQueryDWordValue function has the following prototype:

function RegQueryDWordValue(const RootKey: Integer; const SubKeyName, ValueName: String; var ResultDWord: Cardinal): Boolean;



It queries the specified registry value and returns the data in ResultDWord.

If .NET is installed, the InitializeSetup function returns true, and the setup continues.

If .NET is not installed, a message box is shown to the user to let them know they need to install .NET 3.5 (a custom message is printed in the message box and the user can choose whether to install .NET or not):

MsgBox(ExpandConstant('{cm:dotnetmissing}'),  mbConfirmation, MB_YESNO)



If the user chooses to install the .NET framework, we will run the installer for .NET:

Exec(ExpandConstant('dotNetFx35setup.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)



The Exec function is used to execute another program.

After the function finishes execution, we check if .NET was installed properly (we check the registry key again). If the key was set, it means there were no problems while installing .NET and we resume our installation. If there were problems while installing .NET, we show the appropriate message to the user and cancel the setup.

Is This A Good Question/Topic? 0
  • +

Replies To: Inno setup features

#2 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Posted 29 February 2012 - 08:03 PM

ma'am i have a script but how can i modify you're code. in my script it always run microsoft .net framework even i already installed it. when i copied you're code. i received an error from inno setup

this is my script. can i modify it. instead of downloading it run the microsoft .net framework to install? sorry for my bad english

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

#define MyAppName "Personnel Information System"
#define MyAppVersion "1.0"
#define MyAppPublisher ""
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "Personnel Information System.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={{BB0F279F-54AC-4FF7-9B90-2CD78A8C89FD}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputDir=C:\Documents and Settings\Audrey\Desktop\
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

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

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked; OnlyBelowVersion: 0,6.1

[Files]
Source: "C:\Documents and Settings\Audrey\Desktop\final\ISNHS sourcecode\ISNHS sourcecode\Personnel Information System\bin\Release\Personnel Information System.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Documents and Settings\Audrey\Desktop\final\ISNHS sourcecode\ISNHS sourcecode\Personnel Information System\bin\Release\DevComponents.DotNetBar2.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Documents and Settings\Audrey\Desktop\final\ISNHS sourcecode\ISNHS sourcecode\Personnel Information System\bin\Debug\master.rpt"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Documents and Settings\Audrey\Desktop\final\ISNHS sourcecode\ISNHS sourcecode\Personnel Information System\bin\Debug\pisdb.accdb"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
Source: support\dotnetfx35.exe; DestDir: {tmp}; Flags: deleteafterinstall
Source: support\CRRedist2008_x86.msi; DestDir: {tmp}; Flags: deleteafterinstall
Source: support\AccessRuntime.exe; DestDir: {tmp}; Flags: deleteafterinstall
[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: quicklaunchicon




[Run]
Filename: {tmp}\dotnetfx35.exe; Parameters: "/q"; WorkingDir: {tmp}; StatusMsg: Installing DotNET Framework 3.5
Filename: {tmp}\CRRedist2008_x86.msi; Parameters: "/q"; WorkingDir: {tmp}; StatusMsg: Installing Crystal Report Redistributable
Filename: {tmp}\AccessRuntime.exe; Parameters: "/q"; WorkingDir: {tmp}; StatusMsg: Installing Access Runtime
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1