![](/img/trans.png)
[英]when installing the software, if the specific condition is false How to roll back the software installation process using NSIS?
[英]Installation is failing when installing the application using NSIS to create and control the service
我的要求是,在使用 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 安裝失敗:無法創建服務” 。
編寫此代碼片段或其他任何地方(如 .OnInit)是否正確(“部分”)?
並且在服務名稱字段安裝服務時,我們是否需要提供“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
請幫助我解決問題並提供您對安裝和使用服務的想法。
第一個參數是服務的內部名稱。 它不需要 .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 而不是帶有偏移量的跳轉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.