Archive: Beginner Installer Help - Looks at Custom Pages, and Writing to Registry


Beginner Installer Help - Looks at Custom Pages, and Writing to Registry
I realise this was written in a hasty manner, and I apologise for that. I'd like to update it where necessary over time, as I understand more about NSIS.
If anyone feels they need to correct or insert any info then please post and I'll get to it, or simply write it as an extension of this.

Most of what I've learnt is thanks to Cheryll, RedWine, and AfrowUK. Thanks guys (means girls too ofc!) :)


===================================================
(Based from my experience alone, which is very limited :shock: , and some help I received.) There is no surety whatsoever in believing this is the correct way of doing things, and even more folly is to believe that all of it is 100% correct. keke.

Basic NSIS Guide to creating a simple installer Manual can be found here.


1) Download the latest HM NIS Edit from Sourceforge.

2) Run the Script Wizard, inputting your application's details where appropriate. If you find something you are unsure of, leave the default unchanged. When you reach the 'Application Files' section, you'll want to separate your application's main folder of files into ones that are going into different directories. Sub-folders wouldn't matter here, so for example, your application and its files would go in one section appropriately titled, and the system files that the app runs would go under another section aimed at installing to Windows\System folder. The rest of the options are very simple, so when ready go ahead and finish the script wizard.

3) Peruse the script, and try to make sense of the general layout. Typically !defines go first which are referred to at different parts of the installer by name. A define that looks like this for example:

!define PRODUCT_NAME "My application"


Is going to add your application name to the PRODUCT_NAME variable, and can be referred to as:

${PRODUCT_NAME}


After !defines you'll generally find MUI instructions which refer to the MUI.nsh file (This MUI.nsh file needs to be included via the !include "MUI.nsh" call, usually at the start of the script), which holds useful lines of code that call certain installer pages. Next are some simple install options that you just checked or entered while in the wizard. Then we have the Sections, which include setting up the Start menu .lnk's, and partially representing the 'Application Files' section that you worked through in the wizard.
The uninstall sections come next with consistently mostly self-explaining commands that will initiate deletes, message boxes and registry edits, and others. Obviously, when dealing with these uninstaller commands, we are editing the script of the uninstaller executable.

Try to order your sections and commands in the order that you want them to happen, (duh? wasn't obvious to me in the beginning though xD ) this means that your first page (perhaps your welcome page) would go in front of all other page commands.

---

A little about Registry and Custom Pages
Adding values to registry via user inputted variables can be relatively simple. In creating my first installer, I used a custom page that asked the user for a couple of details. The Custom Page was defined as a function in the script, and when the Function ended, the values left on the page were added to the registry. These are the elements that make up the custom page and it's details:

1) First in the script there are the variables that are declared, this can be anywhere before they are used. They look as simple as this:

var Username
var Password


2) Then we make the custom page's aesthetics by clicking the "New Install Options File" under the File menu in HM NIS Edit. On this sheet we can simply click a form element from the right menu's icons and click on the sheet where we'd like the element to appear. Make sure you note down, or name the "FieldNum" value in each of your element's appropriately, because that will be our handle when taking the input from that element in the install script. Also, to set a default just enter what you'd like into the "State" box. You'll find both of these and the other values on the right after selecting a placed element.
Once you are happy with the page, save it as a .ini file and it's probably a good idea to put the .ini file in the same directory as your install script (.nsi).

3) Next we must start the function that will be our custom page with an initialisation command:


Function .onInit
!insertmacro MUI_INSTALLOPTIONS_EXTRACT_AS "login.ini" "login"
Functionend


Where login.ini is the name of your ini file. :shock:

After which, we announce the page and set it's titles with something like this:


LangString CUSTOM_TITLE ${LANG_ENGLISH} "Enter your Login Details."
LangString CUSTOM_SUBTITLE ${LANG_ENGLISH} " "
Page custom LoginPage


4) Our last group of code is the function itself, which displays the page, gets the values we want from the input boxes and does whatever it wants with them. It'll look something like this:


Function CustomPage

!insertmacro MUI_HEADER_TEXT "$(CUSTOM_TITLE)" "$(CUSTOM_SUBTITLE)"
# Display the page.
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "Custom"

# Get the user entered values.
!insertmacro MUI_INSTALLOPTIONS_READ $Username "Custom" "Field 1" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $Password "Custom" "Field 2" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $Server "Custom" "Field 3" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $Autoupdate "Custom" "Field 4" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $AUpdatePath "Custom" "Field 5" "State"

WriteRegStr HKCU "Software\VB and VBA Program Settings\${PRODUCT_NAME}\Main" "MySQLUser" $Username ;As per install instruction file.
WriteRegStr HKCU "Software\VB and VBA Program Settings\${PRODUCT_NAME}\Main" "MySQLPass" $Password
WriteRegStr HKCU "Software\VB and VBA Program Settings\${PRODUCT_NAME}\Main" "MySQLADDR" $Server
WriteRegStr HKCU "Software\VB and VBA Program Settings\${PRODUCT_NAME}\Main" "AutoUpdatePath" $AUpdatePath

${If} $Autoupdate = 1 ;control is selected
WriteRegStr HKCU "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\${PRODUCT_NAME}\Main" "AutoUpdateEnable"="1"
${EndIf}
${If} $Autoupdate = 0 ;control isn't selected ... ._.
WriteRegStr HKCU "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\${PRODUCT_NAME}\Main" "AutoUpdateEnable"="0"
${EndIf}

FunctionEnd


We are simply putting the input box values in variables, then putting the variable values into the registry. The last couple of statements use the 'LogicLib.nsh' which extends NSIS to be able to use If's/EndIf's and others. Must be included at beginning of script. What is added to the registry here, is going to stay in the registry unless the keys are deleted in the uninstaller. If you want them to be deleted, then simply delete them using a command similar to this:


DeleteRegValue HKCU "Software\VB and VBA Program Settings\${PRODUCT_NAME}\Main" "MySQLUser"


And that's it!

---

I've noticed before on another thread that you can't get users input within custom page create function, you have to do it within custom page leave function.