Creating the installer with NSIS
Recently to write yourself a small program to do a setup program. My program was developed with QT, so I wanted to look at the QT Installer Framework. But with the Qt Installer Framework to do the installation program seems to be quite troublesome, study for a while there is no clue, so temporarily give up. Then on the Internet casually search, found a NSIS, see the introduction is relatively simple, try to use the use, feeling can also. This is actually a study note when I studied NSIS.
NSIS (Nullsoft scriptable install system) is an open source Windows system under the installer program. It provides installation, uninstallation, system setup, file decompression and other functions. NSIS describes the behavior and logic of the installer through its scripting language.
NSIS's homepage in: http://nsis.sourceforge.net
The latest version of NSIS can be found here. The version I'm using is 3.0b2.
Hello world!
As mentioned earlier, NSIS describes the behavior and logic of the installer through its scripting language. Let's look at one of the simplest script files: 1.nsi
# name the installer OutFile"Installer.exe" default section start; every NSIS script has at least one section. Section default section end SectionEnd
In the file, all lines starting with # are comments. In addition to these annotations, the script actually did nothing. But it gives the framework of the NSI script.
This script can be compiled using the tools provided by NSIS MAKENSISW. After the compilation succeeds, there will be output like this:
Click Test Installer to run the installation file that you just generated. The results after the run are as follows:
This installer is not going to do anything right now. This installation script is expanded a little bit below.
set the name of the installer Outfile "helloworld.exe" # create a default section. Section # create a popup box, with an OK button and the text "Hello world!" MessageBox MB_OK "Hello world!" SectionEnd
The resulting installation file is renamed Helloworld.exe. After you run the installation file, a dialog box pops up:
Here, our setup program is at least a bit functional.
Text file read/write
The following example will allow our NSIS script to do some file read and write work. This type of operation is often used in the Setup program. For example, the creation of a configuration file type of work is to read and write files.
# Declare name of installer file Outfile "Hello world.exe" # Open Sections Section #CreateA popup box, withAn OK button and someText MessageBox MB_OK"Now We is Creating hello_world.txt at desktop!"#OpenAnOutputFile called"Hello_world.txt", # onThe desktopinch WriteMode. This file does notNeed toexist #beforeScript isCompiled andRun FileOpen $0 "$DESKTOP \hello_world.txt"W #WriteThe string"Hello world!" toTheOutputFile FileWrite $0 "Hello world!"#CloseThe file FileClose $0#ShowSuccess message. MessageBox MB_OK"Hello_world.txt have been created successfully at desktop!"#EndThe SectionSectionend
The script above is simple, where FileOpen, FileWrite, FileClose are three file manipulation functions.
$ A is a file handle.
File copy
The most important feature of the installer is to copy our files to the installation path. So this part of the story should be carefully told.
Let's start with a simple example program that installs a text file under the specified installation path. Here, the installation path is set to desktop. The following example will also give you a way to get the installer to customize the installation path.
Our text file is named Test.txt. This file needs to be placed in a directory with our script so that NSIS can find it. Usually we will set up a folder for each installer project, a folder where the NSIS script and the relevant files of our program are placed.
of the installer "simple installer.exe" totoincase as specified variable InstallDir $DESKTOP default section Section forfile SetOutPath $INSTDIR toandin the output path File test.txt SectionEnd
The uninstall function should also be provided after the program is installed. This part of the feature is added below.
# define Installer name OutFile "Installer.exe" #SetDesktop asInstall directory InstallDir $DESKTOP #default Section Start Section# defineOutputPath Setoutpath $INSTDIR # Specify file to Go inch OutputPath File test.txt # define Uninstaller name Writeuninstaller $INSTDIR \uninstaller.exe #-------#default Section EndSectionend #CreateA Section toDefine what the uninstaller does. # the Section'll always be named"Uninstall" Section "Uninstall"# alwaysDeleteUninstaller First Delete$INSTDIR \uninstaller.exe # NowDeleteInstalled fileDelete$INSTDIR \test.txt Sectionend
This part of the code is also very simple, but I do not understand why the first to delete themselves, and then delete the other files. The two DELETE statements to change the location, the operation is also quite normal. The principle here also invites the Master to guide.
Start Menu Add entry
The following code adds a shortcut to our Uninstall.exe in the application entry in the Start menu. For simplicity, our installation path is still on the desktop.
# define name of installer OutFile "Installer.exe" # define installation directory InstallDir $DESKTOP # for R EmovingStartMenu ShortcutinchWindows7RequestexecutionlevelUser#Start default Section Section#SetThe installation directory asThe destination forThe following actions Setoutpath $INSTDIR #CreateThe uninstaller Writeuninstaller"$INSTDIR \uninstall.exe"#CreateA shortcut named"New Shortcut" inchTheStartMenu Programs Directory # Point the new shortcut atThe program Uninstaller CreateShortcut"$SMPROGRAMS \new shortcut.lnk" "$INSTDIR \uninstall.exe"Sectionend # Uninstaller Section Start Section "Uninstall"# First,DeleteThe uninstallerDelete "$INSTDIR \uninstall.exe"#Second, remove the link fromTheStartMenuDelete "$SMPROGRAMS \new shortcut.lnk"# Uninstaller Section EndSectionend
The purpose of the installation file generated after this script compiles is to add a new Shortcut.lnk to the application that puts a uninstall.exe on the desktop and then starts the menu. This new Shortcut.lnk points to Uninstall.exe.
Need to say one more word
Start Menu shortcut in Windows 7 RequestExecutionLevel user
If you forget to write, the user is required to do permission confirmation each time you uninstall. In fact, this is also very good, more reminders also no harm.
Read the Registration Form
Registry operations are also the work that Setup often needs to do.
The following script determines whether the JRE environment is installed in the system by reading the relevant fields in the registry.
# Name the installer OutFile "Installer.exe" #default section Span class= "Hljs-operator" >start section # Span class= "Hljs-keyword" >read the value from the registry into the $0 Register Readre Gstr $0 HKLM "Software\javasoft\java Runtime Environment " CurrentVersion # Print the results in a popup message box message Box MB_OK "version: $ A" # default section end sectionend
With this knowledge, you can write a simple setup program. In fact, the NSIS function is much more than that. When I use those features later, I write a blog to record.
In addition, it is important to prepare a "clean" computer. So clean, refers to this computer is best to install only the operating system. This makes it easier to determine if the installer has dropped some files. Of course, the virtual machine is the most suitable thing to do.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Creating the installer with NSIS