简体   繁体   English

如何使用 NSIS 脚本将应用程序安装为 Windows 服务

[英]How to install application as windows service using NSIS script

How to install application as windows service using NSIS script?如何使用 NSIS 脚本将应用程序安装为 Windows 服务?

I used this command in the script Exec '"sc.exe" but after installation i couldn't find any service in windows services related to it so help me thanks.我在脚本Exec '"sc.exe"使用了这个命令,但安装后我在 Windows 服务中找不到任何与它相关的服务,所以请帮助我,谢谢。

Maybe that the NSIS Simple Service plugin can help you.也许 NSIS简单服务插件可以帮助您。 The syntax is as simple as语法很简单

SimpleSC::InstallService "MyService" "My Service Display Name" "16" "2" "C:\MyPath\MyService.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)

Here the example install the service as ServiceType own process + StartType automatic + NoDependencies + Logon as System Account.这里的示例将服务安装为 ServiceType 自己的进程 + StartType 自动 + NoDependencies + 作为系统帐户登录。 Please refer to the accompanying help for the meaning of the magic numbers.请参阅随附的帮助以了解幻数的含义。

The wiki shows the 5 other methods to handle services with NSIS. wiki 展示了使用 NSIS 处理服务的其他 5 种方法

There are multiple plugins out there as stated on NSIS website如 NSIS 网站上所述,有多个插件

For me it seemed to be unnecessary complicated, so I ended up using sc tool directly.对我来说这似乎是不必要的复杂,所以我最终直接使用了sc工具。 A command is quite simple:一个命令很简单:

!define appName "theApp.exe"
!define displayName "My Awesome Service"
!define serviceName "MyAwesomeService"

ExecWait 'sc create ${serviceName} error= "severe" displayname= "${displayName}" type= "own" start= "auto" binpath= "$INSTDIR\${appName}"'

A full list of sc create arguments available here 此处提供sc create参数的完整列表

Below is the scripts which first stops service, uninstalls previous version, remove form registry and then installs fresh copy.下面是首先停止服务、卸载以前版本、删除表单注册表然后安装新副本的脚本。

Section "Mobile Interface" 

  SimpleSC::StopService "MobileInterface" "1" "60"
  SimpleSC::RemoveService "MobileInterface"
  DeleteRegKey /ifempty HKLM "MobileInterface"
  RMDIR /r "$INSTDIR\MobileInterface\"

  SetOutPath "$INSTDIR\MobileInterface"
   # define what to install and place it in the output path
 File "D:\NCS.Sentinel\NCS.Sentinel.MobileWebSvc\bin\Release\"

 SimpleSC::InstallService "MobileInterface" "MobileInterface" "16" "2" "$INSTDIR\MobileInterface\NCS.Sentinel.MobileWebSvc.exe" "" "" ""
 Pop $0 ; returns an errorcode (<>0) otherwise success (0)
 SimpleSC::StartService "MobileInterface" "" "100"

#WriteRegStr HKLM "D:\NCS.Sentinel\NCS.Sentinel.MobileWebSvc\bin\Release\NCS.Sentinel.MobileWebSvc.exe" 

  WriteUninstaller "$INSTDIR\Uninstall.exe"

  ; Store installation folder
  ;WriteRegStr HKCU "Software\Mobile Interface" "" $INSTDIR



SectionEnd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM