Search code examples
nsis

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.

  1. Is it the correct place ("section") to write this code snippet or any other place like .OnInit?

  2. 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
  1. 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.


Solution

  • 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.