首页 > 解决方案 > 使用 NSIS 安装应用程序以创建和控制服务时安装失败

问题描述

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

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

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

;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

当我测试安装程序时,它显示来自我保留的 MessageBox 的消息“testserv 安装失败:无法创建服务”

  1. 编写此代码片段或任何其他位置(如 .OnInit)是正确的位置(“部分”)吗?

  2. 而且在服务名称字段安装服务时,我们是否需要提供“testserv”或“testserv.exe”

SimpleSc::InstallService "testserv"

或者

SimpleSc::TestInstallService "testserv.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:\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. 在“卸载”部分检查“ExistsService”、“StopService”和“RemoveService”是否正确?

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

标签: nsis

解决方案


第一个参数是服务的内部名称。它不需要 .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 而不是使用偏移量跳转。


推荐阅读