Question

I'm preparing to develop my very first Windows installer for a game I'm developing. I've heard NSIS recommended, but my needs are basic and I can't figure out what exactly is required.

I'd like a short NSIS script that can install a directory of files in the "Program Files" directory (requesting permission if necessary), create a simple shortcut in the Start menu, and register an uninstaller in the Add/Remove Programs control panel.

The uninstaller would just delete the directory and remove the shortcut, prompting for reboot if the files are in use.

Finally, I'd like to support upgrades by uninstalling all of the old files and installing new ones, prompting the user to reboot if the old files are in use.

I've seen some sample NSH scripts online that do bits and pieces of this; I'm trying to sling it together myself and it's looking like it's going to be hundreds of lines long. My code smells like the wrong thing.

Is the program I'm describing really supposed to be hundreds of lines of NSH? Surely there's an easier way?

Was it helpful?

Solution

Download Eclipse and afterwards install the EclipseNSIS plugin into Eclipse. That not only gives you a full featured editor for NSIS files (with code completion and all the like), but also has a wizard to create the script for you by answering just some questions (application title, icon to be used, ...).

OTHER TIPS

This should be pretty close (I did not test this script) You should fill in the blanks and replace certain strings...

!define Name "Foo"
Name "${Name}"
Outfile "${Name} setup.exe"
RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
InstallDir "$ProgramFiles\${Name}"

!include LogicLib.nsh
!include MUI.nsh

Function .onInit
SetShellVarContext all
UserInfo::GetAccountType
pop $0
${If} $0 != "admin" ;Require admin rights on NT4+
    MessageBox mb_iconstop "Administrator rights required!"
    SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
    Quit
${EndIf}
FunctionEnd

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"


Section
SetOutPath "$INSTDIR"
WriteUninstaller "$INSTDIR\Uninstall.exe"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Foo By Bar Inc."   "DisplayName" "${Name}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Foo By Bar Inc."   "UninstallString" "$INSTDIR\Uninstall.exe"
;TODO: Install your files with the File command
CreateShortCut "$SMPROGRAMS\${Name}.lnk" "$INSTDIR\Foo.exe"
SectionEnd

Section "Uninstall"
;TODO: Delete your files
Delete "$SMPROGRAMS\${Name}.lnk"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Foo By Bar Inc."
Delete "$INSTDIR\Uninstall.exe"
RMDir "$INSTDIR"
SectionEnd

A great wizard for NSIS is the HM NIS Edit, which can guide you through your first installer, including what you mention above I believe.

http://hmne.sourceforge.net/

If you want to provide an updater in NSIS, I would recommend a few things:

  1. Create a way for your application to check for updates from your server, usually done when it first loads
  2. Download the new installer and run it (from your application, although anyone probably can get it from your website as well).
  3. If your old program is running, you need to shut it down. NSIS has plugins that can find a process and kill it. This is not super 'nice' but can be necessary.

I recommend this process plugin

If you have DLL's in use you can't just delete them. However, in NSIS you can rename them, and then mark them for deletion on the NEXT reboot, like so:

Delete /REBOOTOK file.txt

Then copy your new DLL's into the folder.

For NSIS, there are a ton of plugins, samples and great help, but learning the language takes a little time. Inno is a lot easier to learn, but harder to customize when you get to the nitty gritty. However, I would say the two are comparable overall, and either should work just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top