当我使用NSIS启动服务时,它会显示消息“无法启动”,但是该服务在服务中却显示为“正在运行”

时间:2018-11-14 09:15:58

标签: nsis

我的要求是我需要创建一个服务,一旦创建,它应显示为“正在运行”状态。 为此,我正在创建服务,然后使用NSIS脚本如下所示启动服务:

当我运行以下安装程序脚本时,出现显示“无法启动”的消息框。但是,如果我看到该服务,它将显示为“正在运行”

;InstallServices:

SimpleSC::InstallService "mainserv62" "UPS Service Test62" "16" "2" "$INSTDIR\mainserv.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
${If} $0 <> 0
MessageBox MB_ICONSTOP "InstallService failed, error $0"
Abort
${Else}
MessageBox MB_OK "mainserv has installed"
${EndIf}

SimpleSC::SetServiceDescription "mainserv62" "PowerChute Personal Edition service for managing battery backup power events."

; Start a service using NSIS Simple Service Plugin

SimpleSC::StartService "mainserv62" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
${If} $0 <> 0
MessageBox MB_ICONSTOP "Failed to Start, error $0"
Abort
${Else}
MessageBox MB_OK "Service Started"
${EndIf}

上述启动服务脚本中的If和Else语句是否错误?

还是由于我们多次执行“ Pop $ 0”(安装服务和启动服务)?

我可以在“卸载”部分删除服务吗?

我可以在“卸载”部分中使用“ RMDir / r / REBOOTOK $ INSTDIR”命令吗?

下面是我的完整代码:

;--------------------------------
; The stuff to install
Section "ServiceTestNew (required)"

  SectionIn RO


  ; Set output path to the installation directory. Here is the path C:\Program Files\ServiceTestNew
  SetOutPath $INSTDIR
  File E:\Code\PCPE\mainserv\Release\mainserv.exe
  File E:\prakash\Testmainserv\drvutil.dll
  File E:\prakash\Testmainserv\UpsControl.dll
  File E:\prakash\Testmainserv\UpsDevice.dll
  File E:\prakash\Testmainserv\rdp.dll
  File E:\prakash\Testmainserv\pdcdll.dll

  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\ServiceTestNew "Install_Dir" "$INSTDIR"
  WriteRegStr HKLM SOFTWARE\ServiceTestNew\Dialog "AppDataCollectionDlg" "0"
  WriteRegStr HKLM SOFTWARE\ServiceTestNew\Service "Image" "mainserv.exe"


  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTestNew" "DisplayName" "ServiceTestNew"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTestNew" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTestNew" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTestNew" "NoRepair" 1
  WriteUninstaller "uninstall.exe"

;InstallServices:

SimpleSC::InstallService "mainserv62" "UPS Service Test62" "16" "2" "$INSTDIR\mainserv.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
${If} $0 <> 0
MessageBox MB_ICONSTOP "InstallService failed, error $0"
Abort
${Else}
MessageBox MB_OK "mainserv has installed"
${EndIf}

SimpleSC::SetServiceDescription "mainserv62" "PowerChute Personal Edition service for managing battery backup power events."

; Start a service using NSIS Simple Service Plugin

SimpleSC::StartService "mainserv62" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
${If} $0 <> 0
MessageBox MB_ICONSTOP "Failed to Start, error $0"
Abort
${Else}
MessageBox MB_OK "Service Started"
${EndIf}

SectionEnd

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"

  ;CreateDirectory "$INSTDIR\ServiceTestNew"
  CreateShortcut "$INSTDIR\ServiceTestNew\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortcut "$INSTDIR\ServiceTestNew\ServiceTestNew (MakeNSISW).lnk" "$INSTDIR\ServiceTestNew.nsi" "" "$INSTDIR\ServiceTestNew.nsi" 0

SectionEnd

;--------------------------------

; Uninstaller

Section "Uninstall"

  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ServiceTestNew"
  DeleteRegKey HKLM SOFTWARE\ServiceTestNew

  ; Remove files and uninstaller
  Delete $INSTDIR\ServiceTestNew.nsi
  Delete $INSTDIR\uninstall.exe

  ; Remove shortcuts, if any
  Delete "$INSTDIR\ServiceTestNew\*.*"

  ; Remove directories used
  RMDir "$INSTDIR\ServiceTestNew"
  RMDir "$INSTDIR"

  ; To Uninstall the Service

   SimpleSC::ExistsService "mainserv62"

    ; Stop the service using NSIS Simple Service Plugin
    SimpleSC::RemoveService "mainserv62"
    Pop $0 ; returns an errorcode (<>0) otherwise success (0)
    ${If} $0 <> 0
    MessageBox MB_ICONSTOP "Unable to Remove the service, error $0"
    Abort
    ${Else}
    MessageBox MB_OK "Service removed successfully"
    ${EndIf}


 RMDir /r /REBOOTOK $INSTDIR
SectionEnd

1 个答案:

答案 0 :(得分:0)

SetServiceDescription之后缺少Pop。更重要的是,StartService缺少了第三个(超时)参数。

您可能需要先停止该服务,然后才能将其删除。

要确保您拥有正确数量的Pops,可以执行以下操作:

Section
Push "Hello popping World"

; all other code in the section

Pop $0
MessageBox Mb_Ok $0 ; This should display your message
SectionEnd