【问题标题】:Installation is failing when installing the application using NSIS to create and control the service使用 NSIS 安装应用程序以创建和控制服务时安装失败
【发布时间】:2021-01-17 15:26:11
【问题描述】:

我的要求是,在使用 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. 编写此代码 sn-p 或任何其他位置(如 .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


    【解决方案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 而不是使用偏移量跳转。

    【讨论】:

    • 感谢您的意见。我会尝试你的建议。
    • 嗨@Anders,我使用了您发布的代码sn-p。我仍然收到服务失败消息:“InstallService failed, error 1073”
    • 每次加载服务都需要改变“name_of_service”参数值吗?有什么可以覆盖的选项吗?
    • 此外,StartService 和 StopService 功能无需任何脚本更改(如 SimpleSC::StartService "mainserv" "")即可正常工作。 NSIS 是否默认为我们提供此功能而无需更改任何脚本?
    • 服务的名称(内部名称)作为键存储在注册表中,因此您无法安装具有相同内部名称的其他服务。每个服务 .exe 应该有一个名称。
    猜你喜欢
    • 2015-03-21
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多