Skip to content

Instantly share code, notes, and snippets.

@cpoDesign
Last active November 17, 2021 17:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpoDesign/3a2af312b319da9e4fc6 to your computer and use it in GitHub Desktop.
Save cpoDesign/3a2af312b319da9e4fc6 to your computer and use it in GitHub Desktop.
Example of stopping and uninstalling service using nsis
!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