Showing posts with label exe creater. Show all posts
Showing posts with label exe creater. Show all posts

Friday, June 10, 2011

Creating Windows Installers using NSIS.

NSIS (Nullsoft Scriptable Install System) is a professional open source system to create Windows installers.


I created an installer for my outlook addin (.dll file) with this super cool install system.
Basically, we create a script file (.nsi file) and compile it using NSIS software and the output will be an executable file. Just click to run. Makes life so simpler :)
Lets begin knowing more about .nsi script and i will be explaining taking my outlook addin as an example.
Semicolons are used to comment out lines.


; The name of the installer
Name "Outlook Connector"
; The output filename
OutFile "Outlook Connector.exe"
; The default installation directory
InstallDir "$PROGRAMFILES\Outlook Connector"
; Request application privileges for Windows Vista
RequestExecutionLevel admin
;Set of wizard pages during installation (Remember the next, next, next buttons we click to install a software)
Page license
Page components
Page directory
Page instfiles
;Set of wizard pages during uninstallation 
UninstPage uninstConfirm
UninstPage instfiles

; The installation section which checks whether outlook 2007 is installed and if yes then installs addin
Section "IsOutlook2007Installed" 
   ; Set output path to the installation directory.
   SetOutPath $INSTDIR
   ;check if outlook is installed
   Push $R0
   ClearErrors
   ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE" ""  
   IfErrors +1 +4
     MessageBox MB_OK "Outlook not installed!!"
     Quit  ;Abort installation as outlook not installed


   Delete "$INSTDIR\*.*"
   CreateDirectory "C:\TEMP"
   CreateDirectory "$INSTDIR\Release"
   CreateDirectory "$INSTDIR\Release\Resources"
   File /r "files\*.*"


   ;check if vsto is installed (vsto is required for my addin to work)
   ReadRegStr $R0 HKCR ".vsto" "Content Type"  
   ;$R0 is a variable which holds the value of .vsto in the registry path HKCR (HKEY_CLASSES_ROOT)
   StrCmp $R0 "application/x-ms-vsto" +2 +1 ; +2 +1 means go 2 lines forward if vsto installed and 1 line forward if not.
   ExecWait "$INSTDIR\vstor30.exe" ; executes the vsto30.exe saved in the installation directory
   ExecWait '"msiexec" /i "$INSTDIR\o2007pia.msi"'  ; executes o2007pia.msi - prerequisite for my addin
   ;you can package as many executable files as you want.


   ;write addin in registry
   WriteRegStr HKCU "Software\Microsoft\Office\Outlook\Addins\Outlook Connector" "Description" "Outlook Connector - Outlook add-in created with Visual Studio Tools for Office"
   WriteRegStr HKCU "Software\Microsoft\Office\Outlook\Addins\Outlook Connector" "FriendlyName" "Outlook Connector"
   WriteRegDWORD HKCU "Software\Microsoft\Office\Outlook\Addins\Outlook Connector" "LoadBehavior" 3
   WriteRegStr HKCU "Software\Microsoft\Office\Outlook\Addins\Outlook Connector" "Manifest" "$INSTDIR\OutlookArchiveAddIn.vsto|vstolocal"
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Run" "EmailArchive" "$INSTDIR\Release\EmailArchive.exe"


   ; Write the uninstall keys for Windows
   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Outlook Connector" "DisplayName" "Outlook Connector"
   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Outlook Connector" "UninstallString" '"$INSTDIR\uninstall.exe"'
   WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Outlook Connector" "NoModify" 1
   WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Outlook Connector" "NoRepair" 1
   WriteUninstaller "uninstall.exe"
   Pop $R0
SectionEnd ; end the section


; Uninstaller
Section "Uninstall"
  ; Remove registry keys
  DeleteRegKey HKCU "Software\Microsoft\Office\Outlook\Addins\Outlook Connector"
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Outlook Connector"
  DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "EmailArchive"
  ; Remove files and uninstaller
  Delete "$INSTDIR\Release\Resources\*.*"
  Delete "$INSTDIR\Release\*.*"
  Delete "$INSTDIR\*.*"
  ; Remove directories used
   RMDir "$INSTDIR\Release\Resources"
   RMDir "$INSTDIR\Release"
   RMDir "$INSTDIR"
SectionEnd

After Script Compiled
After Clicking Test Installer
After Installation
We have successfully compiled the script, Created the .exe file and also have verified the installation.
There is more. Explore and have fun. 
Download link : http://nsis.sourceforge.net/download
Documentation : http://nsis.sourceforge.net/Docs