Blog Post

Uninstalling windows service using NSIS

Thursday, March 17, 2016 4:31 PM

Today I needed to create part of my installer to uninstall installation and is able to uninstall already installed and running service.

I have found nice library to help me with that: Simple Service Plugin

The url to get it may be found here: Simple_Service_Plugin

 

 Now I have created small macro that helps me to do that

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

 

The full example is in my Gist here:https://gist.github.com/cpoDesign/3a2af312b319da9e4fc6