NSIS打包注册成Windows服务,安装后能自动启动

需要用到SimpleSC::InstallService这个功能。Section 安装服务 SimpleSC::ExistsService "服务名" Pop $0 ${If} $0 == 0

需要用到SimpleSC::InstallService这个功能。

Section 安装服务
  SimpleSC::ExistsService "服务名"
  Pop $0
  ${If} $0 == 0
    Push $0
    SimpleSC::ServiceIsRunning "服务名"
    Pop $0
    Pop $1
    ${If} $0 == 0
      Push $0
      ${If} $1 == 1
        SimpleSC::StopService "服务名" 1 30
        Pop $0
        ${If} $0 == 0
          DetailPrint "删除 服务名..."
          SimpleSC::RemoveService "服务名"
        ${ElseIf} $0 != 0
            Push $0
            SimpleSC::GetErrorMessage
            Pop $0
            MessageBox MB_OK|MB_ICONSTOP "服务关闭失败($0)"
	    ${EndIf}
	  ${ElseIf} $1 == 0
	    DetailPrint "Removing 服务名..."
	    SimpleSC::RemoveService "服务名"
	  ${EndIf}
	${ElseIf} $0 != 0
	  Push $0
	  SimpleSC::GetErrorMessage
	  Pop $0
	  MessageBox MB_OK|MB_ICONSTOP "检测服务是否正在运行失败($0)"
    ${EndIf}
  ${EndIf}
  
  DetailPrint "Installing 服务名Client service..."
  SimpleSC::InstallService "服务名" "服务名" "16" "2" "$INSTDIR\服务.exe" "Dhcp" "" ""
  SimpleSC::SetServiceDescription "服务名" "服务说明。"
  Pop $0
  ${If} $0 != 0
    Push $0
    SimpleSC::GetErrorMessage
    Pop $0
    MessageBox MB_OK|MB_ICONSTOP "服务注册失败($0),请手动注册"
  ${EndIf}
SectionEnd


Section 卸载服务
  SimpleSC::ExistsService "服务名"
  Pop $0
  ${If} $0 == 0
    Push $0
    SimpleSC::ServiceIsRunning "服务名"
    Pop $0
    Pop $1
    ${If} $0 == 0
      Push $0
      ${If} $1 == 1
        SimpleSC::StopService "服务名" 1 30
        ${If} $0 == 0
          DetailPrint "Removing 服务名..."
          SimpleSC::RemoveService "服务名"
        ${ElseIf} $0 != 0
            Push $0
            SimpleSC::GetErrorMessage
            Pop $0
            MessageBox MB_OK|MB_ICONSTOP "服务关闭失败($0)"
	    ${EndIf}
	  ${ElseIf} $1 == 0
	    DetailPrint "Removing 服务名..."
	    SimpleSC::RemoveService "服务名"
	  ${EndIf}
	${ElseIf} $0 != 0
	  Push $0
	  SimpleSC::GetErrorMessage
	  Pop $0
	  MessageBox MB_OK|MB_ICONSTOP "检测服务是否正在运行失败($0)"
    ${EndIf}
  ${EndIf}
SectionEnd
(152)
打赏 支付红包 支付红包 微信打赏 微信打赏
上一篇 2023-05-02 20:32:08
下一篇 2023-05-23 15:53:33

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信