Nsis服务删除

时间:2013-07-25 12:07:01

标签: tomcat service nsis

您好我的NSIS卸载程序有问题。

我安装了软件并创建了一项服务。服务名称由用户命名(例如troll

所以我有服务troll它正在运行并且工作一切都很好。

如果我想要卸载软件,我必须删除此服务;我已尝试使用插件simpleSCnsSCM进行多种操作,没有任何帮助。

如果我为前写道:

  • nsSCM::Remove "troll"服务已删除
  • 但是当我写nsSCM::Remove "$0" - >没有任何事情发生时

我的错误在哪里?从堆栈获取服务名称?我不知道服务的名称,因为它是自定义的,任何帮助都会有所帮助:D

2 个答案:

答案 0 :(得分:0)

安装程序中的变量内容不会在卸载程序中自动恢复。您必须在注册表中编写服务名称或在安装程序中编写.INI文件,然后在卸载程序中读取此值。

答案 1 :(得分:0)

我没有任何声誉,否则这本来就是评论。无论如何,你说服务的名称是“用户命名

您是说最终用户指定服务名称还是只是最终用户的用户名?

如果是后者,你可以在没有任何插件的情况下使用它,不包括ExecDos:

Var User

!define SVC `$User`
!define SC  `$SYSDIR\sc.exe`

System::Call "advapi32::GetUserName(t.r0,*i${NSIS_MAX_STRLEN})i" 
StrCpy $User $0
ClearErrors
ExecDos::Exec /TOSTACK `"${SC}" stop "${SVC}"`
Sleep 50
ExecDos::Exec /TOSTACK `"${SC}" delete "${SVC}"`

如果这是第一个选项,那么只要用户指定服务的名称,您就需要在安装程序中执行类似的操作。希望这不是太复杂。

;= VARIABLES 
;= ################
Var Bit
Var UserService

;= DEFINES
;= ################
!define SVC         `$UserService`
!define SCKEY       SYSTEM\CurrentControlSet\services\${SVC}
!define HKLM        HKLM\${SCKEY}
!define SC          `$SYSDIR\sc.exe`
!define GETCURRPROC `kernel32::GetCurrentProcess()i.s`
!define WOW         `kernel32::IsWow64Process(is,*i.r0)`

;= MACROS
;= ################
;=#
;= Service::Example
; ${Service::CMD} "ServiceName" /DISABLEFSR $0 $1
;       ::CMD       = Either QueryConfig, Stop, or Remove. 
;       ServiceName = The service name to be handled
;       /DISABLEFSR = Disables file-system redirection if running on x64. Use "" to skip.
;       $0          = Return after call
;       $1          =  ''     ''    ''
!define Service::QueryConfig `!insertmacro _Service::QueryConfig`
!macro _Service::QueryConfig _SVC _FSR _ERR1 _ERR2
    ReadEnvStr $R0 COMSPEC
    StrCmpS $Bit 64 0 +4
    StrCmp "${_FSR}" /DISABLEFSR 0 +3
    ExecDos::Exec /TOSTACK /DISABLEFSR `"$R0" /c "${SC} qc "${_SVC}" | FIND "BINARY_PATH_NAME""`
    Goto +2
    ExecDos::Exec /TOSTACK `"$R0" /c "${SC} qc "${_SVC}" | FIND "BINARY_PATH_NAME""`
    Pop ${_ERR1}
    Pop ${_ERR2}
    StrCmpS ${_ERR1} 0 0 +4
    StrCpy $R1 ${_ERR2} "" 29
    WriteINIStr "$EXEDIR\InstallData.ini" "Service" Path "$R1"
    ReadRegStr $R1 HKLM `${SVCKEY}` ImagePath
    WriteINIStr "$EXEDIR\InstallData.ini" "Service" Path "$R1"
    WriteINIStr "$EXEDIR\InstallData.ini" "Service" Name "${_SVC}"
!macroend
!define Service::Stop `!insertmacro _Service::Stop`
!macro _Service::Stop _SVC _FSR _ERR1 _ERR2
    StrCmpS $Bit 64 0 +4
    StrCmp "${_FSR}" /DISABLEFSR 0 +3
    ExecDos::Exec /TOSTACK /DISABLEFSR `"${SC}" stop "${_SVC}"`
    Goto +2
    ExecDos::Exec /TOSTACK `"${SC}" stop "${_SVC}"`
    Pop ${_ERR1}
    Pop ${_ERR2}
!macroend
!define Service::Remove `!insertmacro _Service::Remove`
!macro _Service::Remove _SVC _FSR _ERR1 _ERR2
    StrCmpS $Bit 64 0 +4
    StrCmp "${_FSR}" /DISABLEFSR 0 +3
    ExecDos::Exec /TOSTACK /DISABLEFSR `"${SC}" delete "${_SVC}"`
    Goto +2
    ExecDos::Exec /TOSTACK `"${SC}" delete "${_SVC}"`
    Pop ${_ERR1}
    Pop ${_ERR2}
!macroend

Function ".onInit" 
    System::Call `${GETCURRPROC}`
    System::Call `${WOW}`
    StrCmpS $0 0 +3
    StrCpy $Bit 64
    Goto +2
    StrCpy $Bit 32
FunctionEnd 

;=#
;= I don't know the code you're using but let's assume 
;= after the end-user specifies the service's name you
;= copy the name to the variable $UserService. This is
;= somewhere in the install process.


StrCpy $UserService $0
;#=#
;# This will write to an INI file | $EXEDIR\InstallData.ini
;#      [Service]
;#      Path=X:\Path\to\service\exe
;#      Name=ServiceName 
;#=#
${Service::QueryConfig} ${SVC} /DISABLEFSR $0 $1

;=#
;= Then in your uninstaller you could simply implement
;= this into your code for easy removal. I usually use
;= this method in my projects and it works pretty good
;= without the need for any extra plugins. 
ReadINIStr $0 "$EXEDIR\InstallData.ini" "Service" Name
${Service::Stop} "$0" /DISABLEFSR $0 $1
Sleep 50
${Service::Remove} "$0" /DISABLEFSR $0 $1

/ DISABLEFSR 参数只应在x64计算机上使用。但是,如果你陷入困境,那么宏中的故障保护会为你躲避这个子弹。男孩,那是很多,是吗?幸运的是,这对你没有任何帮助。大声笑。希望这对某人有所帮助。