What is Windows Service?

GOAL

To understand the Windows service and how to register an application as a service.

What is Windows service?

Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. 

from Introduction to Windows Service Applications

These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.

from Introduction to Windows Service Applications

These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.

from Introduction to Windows Service Applications

You can see Windows service applications registered in your PC. Right-click “Start” and click “Computer Management.”

Open Services and you can see the list of Windows service application.

Windows service application contains updater, time setter and security software because these need to be executed automatically. And these applications should be executed without user login and user authority.

Status: Running or not

Srartup Type: Manual, Manual (Trigger Start), Automatic, Automatic (Trigger Start), Automatic (Delayed Start) and so on.

Features of Windows Service

  • It can be started automatically
  • It works in background
  • It works without user interface
  • It can work with any user

What is SCM?

SCM, Service Control Manager is a service for controlling services, which starts, stops, and monitors the status of services. SCM is started by the Winlogon process (winlogon.exe) when Windows starts and starts processes which have “Automatic” startup type.

SCM database

SCM has “SCM database” that is a database of registered services. You can see the SCM database with Register Editor. Press Windows key + R and run “regedit”.

Then you can find the registry “Services” in your computer.

How SCM starts and stops services

How to start

1. SCM starts the service process

2. StartServiceCtrlDispatcher that is the function to connect service to SCM is called in the service process

Then the calling thread, the main thread, is connected to the SCM and the control won’t returned until a services runnning in the process have terminated.

3. The main thread call RegisterServiceCtrlHandlerEx function to handle extended service control requests and create the service thread to execute ServiceMain() function.

4. The service thread starts ServiceMain function when accepting the control request. ServiceMain function is the main function of each service program.

5. The status of the started service is changed to “Running”.

How to end

1. SCM send stop request to the main thread.

2. The main thread stop the service thread and the service thread is removed.

3. The status of the stopped service is changed to “Stopped”.

4. StartServiceCtrlDispatcher is returned.

How to install the service application

Use command below to install with installUtil.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installUtil.exe <Path to the application>

Use command below to uninstall.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installUtil.exe /u <Path to the application>

You can use sc command too to install the service.

sc create SVC1 binPath= <Path to the application> DisplayName= "" description= "" start= auto

Sc command is used to check the status of services, create, delete, and manage them.

What is the difference between installUtil and sc?

Refer to some web sites such as the following.

installing windows service with SC.exe or InstallUtil.exe – there is difference but which?

installing windows service with SC.exe or InstallUtil.exe – there is difference but which?