Last active
August 12, 2024 08:15
-
-
Save cpoDesign/3a2af312b319da9e4fc6 to your computer and use it in GitHub Desktop.
Example of stopping and uninstalling service using nsis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!define APPNAME "App Name" | |
!define COMPANYNAME "Company Name" | |
!define DESCRIPTION "A short description goes here" | |
# These three must be integers | |
!define VERSIONMAJOR 1 | |
!define VERSIONMINOR 1 | |
!define VERSIONBUILD 1 | |
# These will be displayed by the "Click here for support information" link in "Add/Remove Programs" | |
# It is possible to use "mailto:" links in here to open the email client | |
!define HELPURL "http://..." # "Support Information" link | |
!define UPDATEURL "http://..." # "Product Updates" link | |
!define ABOUTURL "http://..." # "Publisher" link | |
# This is the size (in kB) of all the files copied into "Program Files" | |
!define INSTALLSIZE 7233 | |
RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on) | |
InstallDir "$PROGRAMFILES\${COMPANYNAME}\${APPNAME}" | |
# rtf or txt file - remember if it is txt, it must be in the DOS text format (\r\n) | |
; LicenseData "license.rtf" | |
# This will be in the installer/uninstaller's title bar | |
Name "${COMPANYNAME} - ${APPNAME}" | |
;Icon "logo.ico" | |
outFile "sample-installer.exe" | |
!include LogicLib.nsh | |
# Just three pages - license agreement, install location, and installation | |
page license | |
page directory | |
Page instfiles | |
!macro VerifyUserIsAdmin | |
UserInfo::GetAccountType | |
pop $0 | |
${If} $0 != "admin" ;Require admin rights on NT4+ | |
messageBox mb_iconstop "Administrator rights required!" | |
setErrorLevel 740 ;ERROR_ELEVATION_REQUIRED | |
quit | |
${EndIf} | |
!macroend | |
function .onInit | |
setShellVarContext all | |
!insertmacro VerifyUserIsAdmin | |
functionEnd | |
!macro stopAndUninstallService serviceName | |
DetailPrint "Hello ${serviceName}" | |
stopService: | |
SimpleSC::ExistsService "${serviceName}" | |
Pop $0 | |
${If} $0 == 0 | |
SimpleSC::ServiceIsRunning "${serviceName}" | |
Pop $0 | |
Pop $1 | |
${If} $1 == 0 | |
goto RemoveService | |
${Else} | |
SimpleSC::StopService "${serviceName}" 1 30 | |
${EndIf} | |
SimpleSC::ServiceIsStopped "${serviceName}" | |
Pop $0 | |
Pop $1 | |
${If} $1 == 1 | |
goto stopService | |
${Else} | |
goto RemoveService | |
${EndIf} | |
RemoveService: | |
SimpleSC::RemoveService "${serviceName}" | |
${Else} | |
DetailPrint "$0 does not have service '${serviceName}'" | |
${EndIf} | |
!macroEnd | |
section "install" | |
!insertmacro stopAndUninstallService "DemoServiceName" | |
sectionEnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment