Saturday, August 13, 2011

NSIS - How to Write the Uninstaller

If you know how to create an installer using NSIS, you'll probably want to know how to create the uninstaller too :)

So you have to write a section named "Uninstall" inside your .nsi script file.

section "Uninstall"  
  SetShellVarContext all
  Delete "$DESKTOP\Your Application.lnk"
  RMDir /r "$SMPROGRAMS\YourApp"
  RMDir /r "$INSTDIR"
  Delete $INSTDIR\uninstall.exe
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\YourApp"
  
sectionEnd

In line 2 it sets ShellVarContext to all (all user's shell folder will be used). If not the default is current user's shell folder.  Next line deletes the desktop shortcut. Next 3 lines delete the installation folders. If you entered any registry keys during the installation then you can delete them here (line 7).

While having this "Uninstall" section in  your nsis script, you have to invoke it from your "Install" section. Then only the installation will write the uninstaller.   Here is a possible association of a installer section to the above uninstaller section.

Section "GENERAL"

  SetShellVarContext all
  #Installer body---------(creating desktop/start menu short cuts, granting permissions, etc)
  
  #Writing registry keys
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\YourApp" "DisplayName" "Your Application"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\YourApp" "UninstallString" "$INSTDIR\uninstall.exe"

  # create the uninstaller
  writeUninstaller "$INSTDIR\uninstall.exe"

SectionEnd





Friday, July 15, 2011

NSIS - How to Embed Other Installers in Your Installer

Sometimes you may want to embed some other installer in your installer. As an example if you need Java as a prerequisite then you can embed Java installation inside your installer. Then it will run the Java installation before installing your application, when you run your installer. 

So let's write a section to embed JRE installer in your installer
Section "jre" SECJRE
  File "jre-6u14-windows-i586.exe"

  DetailPrint "Starting the JRE installation"   
  ExecWait "$TEMP\jre-6u14-windows-i586.exe" 
SectionEnd

In line 2 it is checking the existence of the file "jre-6u14-windows-i586.exe". At the point of compiling NSIS script, this exe should exist in the stated relative path (in this case it should be inside the same folder as the nsis script file). In line 5 it will execute the jre installer and your installation program will be suspended until JRE installation is completed. After the JRE setup is complete it will resume the installation of your application. In this way, you can embed any number of installers inside your installer.

This can be improved in many ways. Well, before installing JRE, you can check whether JRE is already installed in the system. In that case you can consider the version too. If it is not installed then you can use the above script to install JRE. Another improvement is, instead of installing a precise JRE you can download the latest version from Internet and then install. This nsis example shows a similar thing.

Thursday, April 28, 2011

NSIS - How to Detect Whether Java is Installed in the System

While writing my installer I wanted to install JRE (embed JRE installer in my installer) only if Java is not installed already. So I had to detect whether Java is already installed.

My previous post on NSIS points to a quick start guide (in Harshani's blog) for NSIS, including how to install NSIS and how to write a simple installer. So at this point you should have installed NSIS and been somewhat familiar with NSIS scripting (sections, etc..). So here I am posting the piece of script I used to detect whether Java is installed in the system.

Section "find java" FINDJAVA

  StrCpy $1 "SOFTWARE\JavaSoft\Java Runtime Environment"
  StrCpy $2 0
  ReadRegStr $2 HKLM "$1" "CurrentVersion"
  StrCmp $2 "" DetectTry2
  ReadRegStr $5 HKLM "$1\$2" "JavaHome"
  StrCmp $5 "" DetectTry2
  goto done

  DetectTry2:
  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Development Kit" "CurrentVersion"
  StrCmp $2 "" NoJava
  ReadRegStr $5 HKLM "SOFTWARE\JavaSoft\Java Development Kit\$2" "JavaHome"
  StrCmp $5 "" NoJava done

  done:
  ;All done. 

  NoJava:
  ;Write the script to install Java here

SectionEnd


In 3rd line of the script, it copies the path "Software\JavaSoft\Java Runtime Environment" to the variable 1. It is the Windows registry entry in which we should look for Java. In line 5 it reads the value of registry attribute "Current Version" which belongs to "SOFRWARE\JavaSoft\Java Runtime Environment", to the variable 2. ReadRegStr is the command used to read the registry. Here, "HKLM" means that it should look for this key under "HKEY_LOCAL_MACHINE" registry main key. 

In line 6 the command StrCmp compares the value of var 2 with "empty". If it is empty then that means there is no JRE installed in the system. So it passes the control to label "DetectTry2" which tries to detect if there is any Jdk installed. Line 7 is executed only if there is a value for JRE version. If so it tries to find the value for JavaHome attribute in key "Software\JavaSoft\Java Runtime Environment\1.x.x". In line 8 it checks whether JavaHome is null and if so, then again it jumps to label "DetectTry2". If it is not null that means there is a JRE installed and line 9 passes it to label "done".

Now let's see what "DetectTry2" label does. It is executed only if there is no JRE installed. In line 12 it reads the value of  "CurrentVersion" attribute of key "SOFTWARE\JavaSoft\Java Development Kit". In line 13 it checks whether the value it got is empty. If so it will hit the label "NoJava" means that neither JRE nor JDK is installed. If it is not empty then line 14 will check whether JavaHome is empty like before. Again, if JavaHome is empty it jumps to "NoJava". If not it jumps to "done".

If we ever hit "done" then that means either JRE or JDK is installed in the system. So there is nothing to do there. If we hit NoJava then we are going to install Java. I will post the script for that in a later post, NSIS - How to embed some other installer in your own installer.

This is valid for any other software like .NET framework, MS Office, etc as long as you know the correct registry entries to look in. 

Friday, April 15, 2011

Nullsoft Scriptable Install System (NSIS) - A Really Good Windows Installation System

I wanted to write an installation for IFRI data entry application which is currently being used to enter forest data to their database. The existing installation had some inconsistencies like users had to install the other required software themselves, some permission issues etc. So I had to search for a pretty stable installation system with all those features I wanted.

While searching I found this Harshani's blog about NSIS and thought to give it a try and  decided to write IFRI installer using NSIS. First it was not pretty simple and straight forward. I had to learn NSIS scripting language , tested examples in NSIS site. NSIS documentation was pretty helpful too. So finally I was able to write an installer for IFRI with all the features they expected and NSIS helped me a lot on that. I think it wouldn't be this easy and stable if I used another installation system other than NSIS.

So I'll post some tips and tricks I found  in NSIS scripting later. However Harshani's blog post about NSIS (stated above) is a nice post and shows how to build a simple installer. It will be a good first step on NSIS.
Related Posts with Thumbnails