Мое требование - мне нужно создать службу, и после ее создания она должна отображаться в состоянии «Работает».
Для этого я создаю службу и затем запускаю службу, как показано ниже, используя скрипт NSIS:
Когда я запускаю приведенный ниже скрипт установщика, появляется окно с сообщением «Failed to Start». Но если я вижу службу, она отображается как «Работает»
;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", который мы делаем несколько раз (Install Service и Start Service)?
Можно ли удалить службу в разделе «Удаление»?
Можно ли использовать команду «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