Making installer/reinstaller in NSIS - nsis

I need this routine - if RegKey with Installation Path is presented, we must to stop executed application (if executed), start Uninstaller silently (if uninstaller is not found - just delete all files in the directory) and set INSTDIR to value from Registry.
If RegKEy with Installation Path is not exists - start normal installation.
; Try to read Install Path from Reg
ReadRegStr $0 HKCU "Software\TelnetTray" "Path"
; Uninstall first if app installed
${If} $0 != ""
!define INSTDIR $0
; Try to stop app if executed
Stop "$INSTDIR\app.exe"
; Try to uninstall
${If} ${FIleExists} "$INSTDIR\uninst.exe"
ExecWait "$INSTDIR\uninst.exe /S"
; Or just delete all files in INSTDIR
${Else}
Delete "$INSTDIR\*"
${EndIf}
${EndIf}

You almost got it
; Try to read Install Path from Reg
ReadRegStr $0 HKCU "Software\TelnetTray" "Path"
; Uninstall first if app installed
${If} $0 != ""
StrCpy $INSTDIR $0
; Try to stop app if executed
${nsProcess::CloseProcess} "$INSTDIR\app.exe" $R0
; Try to uninstall
${If} ${FIleExists} "$INSTDIR\uninst.exe"
ExecWait "$INSTDIR\uninst.exe /S"
; Or just delete all files in INSTDIR
${Else}
RMDir /r "$INSTDIR"
${EndIf}
${EndIf}

Related

Installation is failing when installing the application using NSIS to create and control the service

My requirement is, when installing the application using NSIS the service should be installed and controlled (Start and Stop)
For this, first i downloaded "NSIS_Simple_Service_Plugin_1.30" and placed SimpleSC.dll in the "x86-ansi" directory.
I wrote the below code Under "Section"
;InstallServices:
SimpleSC::InstallService "testserv" "Test Service Testtwo" "16" "2" "E:\Source\Release\testserv.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
IntCmp $0 0 +3
MessageBox MB_OK|MB_ICONSTOP "testserv installation failed: could not create service."
Abort
SimpleSC::SetServiceDescription "testserv" "Test Project service."
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
; We don't care about the result.
; Start a service using NSIS Simple Service Plugin
SimpleSC::StartService "testserv" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
IntCmp $0 0 +3
MessageBox MB_OK|MB_ICONSTOP "testserv installation failed: could not start service."
Abort
When i am testing the installer, it is showing the message "testserv installation failed: could not create service" from the MessageBox i kept.
Is it the correct place ("section") to write this code snippet or any other place like .OnInit?
And also while installing the service at the service name field, do we need to give "testserv" or "testserv.exe"
SimpleSc::InstallService "testserv"
or
SimpleSc::TestInstallService "testserv.exe"
which one is correct?
Below is the complete code:
; ServiceTest.nsi
;
;
; It will install ServiceTest.nsi into a directory that the user selects.
;--------------------------------
; The name of the installer in the path C:\Program Files\Registry
Name "ServiceTest"
; The file to write in the path E:\Source\NULLSOFT\src
OutFile "ServiceTest.exe"
; The default installation directory in the path C:\Program Files\ServiceTest
InstallDir $PROGRAMFILES\ServiceTest
; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically) It shows the path the path C:\Program Files\ServiceTest
InstallDirRegKey HKLM "Software\ServiceTest" "Install_Dir"
; Request application privileges for Windows Vista
; Below line is to check the administrative permissions
RequestExecutionLevel admin
; Below is the include file to check the conditions (If and else)
!include LogicLib.nsh
;--------------------------------
; Pages
Page components
Page directory
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles
;--------------------------------
;--------------------------------
;Installer Functions
Function .onInit
UserInfo::GetAccountType
pop $0
${If} $0 != "admin"
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${Else}
MessageBox MB_OK "User is having Administrative Permissions"
${EndIf}
FunctionEnd
;--------------------------------
; The stuff to install
Section "ServiceTest (required)"
SectionIn RO
; Set output path to the installation directory. Here is the path C:\Program Files\ServiceTest
SetOutPath $INSTDIR
; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\ServiceTest "Install_Dir" "$INSTDIR"
WriteRegStr HKLM SOFTWARE\ServiceTest\Dialog "TestDlg" "0"
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTest" "DisplayName" "ServiceTest"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTest" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTest" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTest" "NoRepair" 1
WriteUninstaller "uninstall.exe"
;InstallServices:
SimpleSC::InstallService "testserv" "Test Service Testtwo" "16" "2" "E:\Source\testserv\Release\testserv.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
IntCmp $0 0 +3
MessageBox MB_OK|MB_ICONSTOP "testserv installation failed: could not create service."
Abort
SimpleSC::SetServiceDescription "testserv" "Test service."
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
; We don't care about the result.
; Start a service using NSIS Simple Service Plugin
SimpleSC::StartService "tetserv" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
IntCmp $0 0 +3
MessageBox MB_OK|MB_ICONSTOP "testserv installation failed: could not start service."
Abort
SectionEnd
; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"
CreateDirectory "$INSTDIR\ServiceTest"
CreateShortcut "$INSTDIR\ServiceTest\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
CreateShortcut "$INSTDIR\ServiceTest\ServiceTest (MakeNSISW).lnk" "$INSTDIR\ServiceTest.nsi" "" "$INSTDIR\ServiceTest.nsi" 0
SectionEnd
;--------------------------------
; Uninstaller
Section "Uninstall"
; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTest"
DeleteRegKey HKLM SOFTWARE\ServiceTest
; Remove files and uninstaller
Delete $INSTDIR\ServiceTest.nsi
Delete $INSTDIR\uninstall.exe
; Remove shortcuts, if any
Delete "$INSTDIR\ServiceTest\*.*"
; Remove directories used
RMDir "$INSTDIR\ServiceTest"
RMDir "$INSTDIR"
; To Uninstall the Service
; Stop the service using NSIS Simple Service Plugin
SimpleSC::ExistsService "testserv"
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
IntCmp $0 0 +1
; Stop the service using NSIS Simple Service Plugin
SimpleSC::StopService "tesserv"
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
IntCmp $0 0 +3
MessageBox MB_OK|MB_ICONSTOP "testserv uninstallation failed: could not stop service."
Abort
; Stop the service using NSIS Simple Service Plugin
SimpleSC::RemoveService "testserv"
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
IntCmp $0 0 +3
MessageBox MB_OK|MB_ICONSTOP "testserv uninstallation failed: could not remove service."
Abort
SectionEnd
Is it the correct way to check "ExistsService", "StopService", and "RemoveService" in "Uninstall" section?
Please help me to resolve the issue and provide your thoughts to install and use the service.
The first parameter is the internal name of the service. It does not need a .exe suffix.
You problem is probably "E:\Code\PCPE\mainserv\Release\mainserv.exe" which is a path on your computer. It needs to be the path to the installed service on the end-users machine. It also helps if you include the error code in your message box so you can know for sure what the error actually is:
!include LogicLib.nsh
Section
SetOutPath $InstDir
File "E:\Code\PCPE\mainserv\Release\mainserv.exe"
SimpleSC::InstallService "mainserv" "UPS Service Testtwo" "16" "2" '"$InstDir\mainserv.exe"' "" "" ""
Pop $0
${If} $0 <> 0
MessageBox MB_ICONSTOP "InstallService failed, error $0"
Abort
${EndIf}
...
SectionEnd
The rest of your code looks OK but I would recommend that you use LogicLib.nsh instead of jumps with offset.

NSIS roll-back installer if ExecWait command gets a specific return code

I'm currently using the following script to install drivers along with my application:
!macro customInstall
ExecWait '"$INSTDIR\resources\DPInst.exe" /sw'
!macroend
However, if DPInst returns >= 0x80010000, this means one or more of the driver installs has failed so I need to roll-back the installation and quit. Any idea how I would do this?
ExecWait can store the process exit code in the 2nd parameter. Not much you can do to roll it back, it is best just to do it early in the install phase:
!include LogicLib.nsh
Section
SetOutPath "$instdir\resources"
File "whatever\DPInst.exe"
ExecWait '"$INSTDIR\resources\DPInst.exe" /sw' $0
${If} $0 U>= 0x80010000
Delete "$INSTDIR\resources\DPInst.exe"
RMDir $instdir\resources
RMDir $instdir
MessageBox mb_iconstop "Error blah blah"
Abort
${EndIf}
SectionEnd

NSIS uninstaller privileges according to installer

I have an NSIS installer for an application, which can be run as normal user. But if the user wants to install into the "Program Files" directory, it can still be accomplished by starting the installer with administrator privileges.
Now I have the problem, that the uninstaller is started with user privileges by default, even if the installation took place as administrator. This causes the uninstallation to silently fail. Even worse: It even states that the uninstall process was successful without being able to delete any files.
My question is: Is it possible to create an uninstaller during the installation, which requires (or better: requests itself with) the same privileges as the installation process?
You would have to implement this check yourself. You can check if you are admin in the installer with the UserInfo plugin and then store the result in a .ini, the registry or append the info to the uninstaller.exe:
InstallDir $temp\instdir
Section
UserInfo::GetAccountType
Pop $0
StrCmp $0 "Admin" 0 +2
StrCpy $0 1
IntOp $0 $0 & 1 ; $0 is now 1 if admin or 0 if not
SetOutPath $InstDir
WriteUninstaller "$InstDir\Uninstall.exe"
FileOpen $1 "$InstDir\Uninstall.exe" a
FileSeek $1 0 END
FileWriteByte $1 $0
FileClose $1
SectionEnd
Section Uninstall
FileOpen $1 "$ExePath" r
FileSeek $1 -1 END
FileReadByte $1 $0
FileClose $1
DetailPrint "Installer was admin: $0"
SectionEnd

NSIS uninstaller doesn't delete files/folders

I'm writing a NSIS installer for one of the apps that the company I work for uses internally, the install process works fine, with no problems all the REG keys are created, and so are the files folders and services, that the App uses. For some reason I can't understand, the uninstall process doesn't work's.
The services created by the app are deleted and so are the Registry keys, the most simple part, the files themselves, I can't delete them through the uninstaller!
#Includes
!include "x64.nsh"
#Defines and Installer Properties
Outfile "ESTvnc Installer.exe"
Name ESTvnc
Icon "${NSISDIR}\contrib\graphics\icons\VNCON.ico"
#Detect OS Version
Function .onInit
StrCpy $instdir $PROGRAMFILES
${If} ${RunningX64}
StrCpy $instdir $PROGRAMFILES32
${EndIf}
FunctionEnd
section
SetShellVarContext all
CreateDirectory $instdir\EST\ESTvnc
setOutPath $instdir\EST\ESTvnc
File /r installfiles\*
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ESTvnc\" \
"DisplayName" "ESTvnc"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ESTvnc"\
"UninstallString" "$instdir\EST\ESTvnc\uninstaller.exe"
writeUninstaller $instdir\EST\ESTvnc\uninstaller.exe
ExecWait '"$instdir\EST\estvnc\estvnc.exe" -install'
sectionEnd
section "Uninstall"
SetShellVarContext all
SimpleSC::StopService "ESTVNC" 1 30
pop $0
SimpleSC::StopService "ESTVNCSR" 1 30
pop $0
SimpleSC::RemoveService "ESTVNC"
SimpleSC::RemoveService "ESTVNCSR"
RMDir /r "$instdir\EST\ESTvnc"
Delete $instdir\EST\ESTvnc\uninstaller.exe
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ESTvnc"
sectionEnd
In the uninstaller, $instdir is the directory the uninstaller is in!
Either place the uninstaller in $instdir and delete $instdir\EST\ESTvnc or if you want to keep it in $instdir\EST\ESTvnc, delete $instdir...

how can i make my installer to check for invalid folder installation?

.Actually i have installed different version of the software in the same computer so want to check the folder for both the version when user proceed for third version of installation. i am trying to use loop but the the installer is not working.Thanks in advance.
I have already 2 versions of same software installed in the same computer .I want when i will install the third version the script should check the path and it should not allow user to install in the same path in which previous two versions are already installed
Function .onVerifyInstDir
nsArray::Set array 1.6
nsArray::Set array 1.7
nsArray::Set array 1.8
nsArray::Length array
Pop $R0
;DetailPrint `array length: $R0`
StrCpy $R1 0
${DoWhile} $R1 < $R0
nsArray::Get array $R1
Pop $R2
StrCpy $tempregistry "$(^Name)_$R2"
;DetailPrint `MyArray2[$R1] is $R2`
ReadRegStr $path HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal\$tempregistry""InstallLocation" ;fetching installed location
${If} $path == ""
${Else}
strCmp $path $INSTDIR 0 pathGood
;Abort
IntOp $R1 $R1 + 1
${EndIf}
${Loop}
PathGood:
FunctionEnd
Here are the steps
Store ur product name or ur registry entry in a variable.
Store the path to be installed in a variable eg $path
Inside a loop check for the registry entry.
ReadRegStr $pdt HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\productname
If found,retrieve the value of the installed location and store it in a variable;
eg $path1
ReadRegStr $path1 $pdt "installed location"
else proceed
${if} $path == $path1
messagebox mb_ok "Another version already installed in the path"
${else}
proceed
${endif}

Resources