Skip to content
⌘ NSIS Forum Archive

Creating and starting Windows service using SimpleSC

2 posts

DivyaArun#

Creating and starting Windows service using SimpleSC

Hi,

I am using SimpleSC::InstallService and StartService to create and start a windows service. However, I have noticed that the window hangs for few seconds ( 3-4s )during this process. It sometimes shows "Not Responding"

I have tried another method using progress bar where the progress bar is displayed when the service installs and starts. There also I have noticed that the progress bar stops for some time.

Below is the code
SimpleSC::ExistsService "AGService"
Pop $0
${If} $0 != 0
DetailPrint "Installing AGWindowsService"
SimpleSC::InstallService "MyService" "AGWindowsService" "16" "2" "$INSTDIR\winservice\MyWindowsService.exe" "" $R8 $R5
SimpleSC::SetServiceDescription "MyService" "This service is used to communicate with My Server"

SimpleSC::SetServiceLogon "MyService" $R8 $R5
Pop $0 ; return error(1)/success(0)
StrCmp $0 +1 Done ; If successful grant the service logon privilege to "MyServiceUser"

SimpleSC::GrantServiceLogonPrivilege $R8
Pop $0 ; return error(1)/success(0)
Done:

DetailPrint "Starting AGWindowsService"
SimpleSC::StartService "AGService" "" 30

SimpleSC::ServiceIsRunning "AGService" 1 30
Pop $0
Pop $1
${if} $0 != 0
# error
MessageBox MB_OK|MB_ICONSTOP "Unable to start service - Reason: $0"
${endif}
${if} $1 == 0
# service not running
MessageBox MB_OK|MB_ICONSTOP "Unable to start service - Reason: $0"
${endif}
${Else}
SimpleSC::RestartService "AGService" "" 30
${EndIf}

Please help!!!
Anders#
Ideally you should only perform these types of tasks in a Section. If you need to do it on a custom page you can try https://nsis.sourceforge.io/BgWorker_plug-in