使用NSIS创建和控制服务的应用程序安装时安装失败

时间:2018-11-02 09:05:27

标签: nsis

我的要求是,在使用NSIS安装应用程序时,应该安装并控制该服务(启动和停止)

为此,我首先下载了“ NSIS_Simple_Service_Plugin_1.30”,并将SimpleSC.dll放置在“ x86-ansi”目录中。

我在“部分”下编写了以下代码

;InstallServices:
SimpleSC::InstallService "mainserv" "UPS Service Testtwo" "16" "2" "E:\Code\PCPE\mainserv\Release\mainserv.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
 IntCmp $0 0 +3
    MessageBox MB_OK|MB_ICONSTOP "mainserv installation failed: could not create service."
    Abort
    SimpleSC::SetServiceDescription "mainserv" "PowerChute Personal Edition service for managing battery backup power events."
    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 "mainserv" ""
    Pop $0 ; returns an errorcode (<>0) otherwise success (0)
    IntCmp $0 0 +3
    MessageBox MB_OK|MB_ICONSTOP "mainserv installation failed: could not start service."
    Abort

我正在测试安装程序时,它会从我保存的MessageBox中显示消息“ mainserv安装失败:无法创建服务”

  1. 编写此代码段或诸如.OnInit之类的其他位置是否在正确的位置(“节”)?

  2. 在服务名称字段中安装服务时,是否还需要提供“ mainserv”或“ mainserv.exe”

SimpleSc :: InstallService“ mainserv”

SimpleSc :: InstallService“ mainserv.exe”

哪个是正确的?

下面是完整的代码:

; 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:\Code\PCPE\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 "AppDataCollectionDlg" "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 "mainserv" "UPS Service Testtwo" "16" "2" "E:\Code\PCPE\mainserv\Release\mainserv.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
 IntCmp $0 0 +3
    MessageBox MB_OK|MB_ICONSTOP "mainserv installation failed: could not create service."
    Abort

    SimpleSC::SetServiceDescription "mainserv" "PowerChute Personal Edition service for managing battery backup power events."
    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 "mainserv" ""
    Pop $0 ; returns an errorcode (<>0) otherwise success (0)
    IntCmp $0 0 +3
    MessageBox MB_OK|MB_ICONSTOP "mainserv 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 "mainserv"
    Pop $0 ; returns an errorcode (<>0) otherwise success (0)
    IntCmp $0 0 +1 

    ; Stop the service using NSIS Simple Service Plugin
    SimpleSC::StopService "mainserv"
    Pop $0 ; returns an errorcode (<>0) otherwise success (0)
    IntCmp $0 0 +3
    MessageBox MB_OK|MB_ICONSTOP "mainserv uninstallation failed: could not stop service."
    Abort

    ; Stop the service using NSIS Simple Service Plugin
    SimpleSC::RemoveService "mainserv"
    Pop $0 ; returns an errorcode (<>0) otherwise success (0)
    IntCmp $0 0 +3
    MessageBox MB_OK|MB_ICONSTOP "mainserv uninstallation failed: could not remove service."
    Abort

SectionEnd
  1. 检查“卸载”部分中的“ ExistsService”,“ StopService”和“ RemoveService”是否正确?

请帮助我解决问题,并提供您安装和使用该服务的想法。

1 个答案:

答案 0 :(得分:1)

第一个参数是服务的内部名称。它不需要.exe后缀。

您的问题可能是“ E:\ Code \ PCPE \ mainserv \ Release \ mainserv.exe”,这是计算机上的路径。它必须是最终用户计算机上已安装服务的路径。如果您在消息框中包含错误代码,这也将有所帮助,这样您就可以确定错误实际上是什么:

!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

其余代码看起来都不错,但我建议您使用LogicLib.nsh而不是使用偏移量跳转。