Posts Tagged ‘windows service’

Scripts for installing, starting, stopping and uninstalling a windows service

July 21st, 2017

These 5 files has served me for 10 years or more for updating a windows service in a small project I have.

The code is crude but serves its purpose. It is not hard to make the code nicer.

The thing here is that I have 1 service in 2 instances, one for production and one for QA on the same machine. Hence they need different names.

The bat file I normally run is StopUninstallInstallStartService.bat because it does the whole turn around. Between every step it pauses and allows me to check the output and between Uninstall and Install also swap the binaries.

I have a similar single file solution here.

InstallService.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@echo off
rem This bat file installs a service and sets its name.
if dummy==dummy%1 (
goto help
)
rem *** Install the service.
sc create %1 binPath= %CD%\MyService.exe DisplayName= "MyService [%1]" start= delayed-auto
sc failure %1 reset= 0 actions= restart/30/restart/30/restart/3000
rem C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe MyService.exe
goto end
: help
echo Parameter is missing.
echo Syntax: InstallService [Servicename]
:end

StartService.bat

1
2
3
4
5
6
7
8
9
10
11
12
@echo off
rem This bat file starts a service by its name.
if dummy==dummy%1 (
goto help
)
rem *** Start the service.
sc start %1
goto end
: help
echo Parameter is missing.
echo Syntax: StartService [Servicename]
:end

StopService.bat

1
2
3
4
5
6
7
8
9
10
11
12
@echo off
rem This bat file stops a service by its name.
if dummy==dummy%1 (
goto help
)
rem *** Stop the service.
sc stop %1
goto end
: help
echo Parameter is missing.
echo Syntax: StopService [Servicename]
:end

UninstallService.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
@echo off
rem This file uninstalls a service by its name.
if dummy==dummy%1 (
goto help
)
rem *** Uninstall the service.
rem sc delete %1
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe /uninstall MyService.exe
goto end
: help
echo Parameter is missing.
echo Syntax: UninstallService [Servicename]
:end

StopUninstallInstallStartService.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@echo off
rem This file stops, uninstalls, installs and starts a service; with a pause in between for manual intervention.
if dummy==dummy%1 (
goto help
)
echo *** Stop the service
call StopService %1
pause
echo *** Uninstall the service.
call DeleteService %1
pause
echo *** Install the service.
call InstallService %1
pause
echo *** Start the service.
call StartService %1
pause
goto end
:help
echo Parameter is missing.
echo Syntax: StopUninstallInstallStartService [Servicename]
:end

Stop, uninstall, install and start a windows service

July 19th, 2017

I have one bat file that does all this for me.
It also has a pause inserted so I can visually check that everything looks ok.
The pause between uninstall and install is important as that is where I exchange the DLL when updating.
Yes, the code is ugly. But it has served a project for 10+ years with the only update with new dotnet versions.

I have  a similar solution here. It uses several files but can also name the service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
rem *** Stop the service.

net stop "My Service Name"

pause

rem *** Uninstall the service.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe /uninstall MyService.exe

pause

rem *** Install the service.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe MyService.exe

pause

rem *** Start the service.

net start "My Service Name"

pause

Exception: Error 1083: The executable program that this service is configured to run in does not implement the service.

September 30th, 2010

In short: verify that the service name is correct.  Totally correct.

Longer:

Debugging windows services written in dotnet can be hard.  They have no GUI and have to be installed by a semi secret process.  Then they have to be started by a system call.  And if the start fails it is not necessarily possible to uninstall the service without rebooting.  The CL.exe doesn’t work and hacking the registry doesn’t work.

Today I spent hours tracking down a bug that surfaced with a “Error 1083: The executable program that this service is configured to run in does not implement the service.” when starting the service.  In the end I found out that the ServiceName was incorrect.

To be honest I got some clues, in one place it said MyMailer instead of MyMailerService.  I couldn’t be sure if this was correct or not since I had inherited the project and my search turned out nothing so I dropped it.

By (almost) chance I checked into the automagically created MyMail.designer.cs file and noticed this.ServiceName=”MyMailer”.  Which, it turned out, was the culprit.

I learned a trick on the way.  The service has 30 (I believe) seconds to start.  If it doesn’t fulfill it is shot down by Windows.  When I started the process it failed and I got a question whether I wanted to debug it.  Unfortunately it takes more thatn 30 s to open my VS2010.  So I started VS as administrator (necessary in Win7) and loaded the project.  When I then started the service I got the question whether to attach and I did.  Two seconds of work and I got a highlighted row at the exact point.