The Big Picture: The Hotel Manager

In the hotel from Section 1, someone decides who starts work, who is on duty, and what to do when a worker does not turn up. That is the manager. In Windows this manager is the Service Control Manager, or SCM.

        You / admin tools / other programs
        (services.msc, sc.exe, PowerShell, apps)
                       │  "start Spooler", "stop W32Time", "what is running?"
                       ▼
              ┌────────────────────┐
              │ SCM  (services.exe)│  ◄── reads the service list (registry)
              └──────────┬─────────┘
          starts / stops / watches
        ┌───────────┬────┴──────┬───────────┐
        ▼           ▼           ▼           ▼
    Spooler      W32Time     Dhcp        ...hundreds more

2.1 SCM Role (services.exe)

What it is: The SCM is not a separate program you can see by that name. It lives inside the process services.exe, found in C:\Windows\System32. There is always exactly one services.exe running.

How it starts. In Chapter 6 you saw the boot order: the kernel starts, then the Session Manager (smss.exe), then wininit.exe. It is wininit.exe that launches services.exe (along with the security process lsass.exe). So the SCM is one of the first user-mode programs of Windows, and it then starts all the automatic services.

Firmware ─► Boot Manager ─► Kernel ─► smss.exe ─► wininit.exe
                                                     ├─► services.exe (the SCM)
                                                     └─► lsass.exe
                                       services.exe ─► svchost.exe, other service programs

What the SCM does all day:

JobPlain meaning
Start services at bootGoes through the list and starts every Automatic one, in the right order (Section 4)
Start and stop on requestObeys a request from you or from a program, if allowed (Section 6)
Keep a statusRemembers whether each service is stopped, starting, running and so on
Pass control messagesTells a running service "stop", "pause" or "shut down"
Check securityVerifies the caller is allowed to do what they ask
Take recovery actionIf a service crashes, restarts it or does what the service's recovery settings say (Section 7)
Log eventsWrites service news to the System event log (Section 8)

You can see it. In Task Manager, Details tab, find services.exe. It runs under the account SYSTEM. It cannot be ended normally; ending it would crash Windows.

Why does this matter? Because everything about services goes through this one manager, the rules are the same for every service. That makes Windows easier to run and to secure.

2.2 Service Database

What it is: The SCM must know which services exist and how each should run. It keeps this in a service database. On Windows, that database is part of the registry (the settings store you met in Chapter 8).

Where: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

Under it, each service has its own key (folder), named with its service name. For example ...\Services\Spooler.

Inside that key are values (named settings). These are the important ones:

ValueWhat it holdsExample
ImagePathThe program file (and options) that runs the serviceC:\Windows\System32\spoolsv.exe
DisplayNameFriendly namePrint Spooler
DescriptionOne-line explanation"This service spools print jobs..."
StartStartup type, a number (Section 3)2 = Automatic
TypeWhat kind of service it is0x10 own process, 0x20 shared process
ObjectNameThe account it runs under (Section 5)LocalSystem
DependOnServiceServices that must start first (Section 4)RPCSS, http
FailureActionsWhat to do if it fails (Section 7)binary data
ErrorControlHow serious a start failure is for boot1 = normal

Sub-keys. Some services also have a Parameters sub-key (extra settings; for shared services this holds ServiceDll, Section 9) and a Triggers sub-key (Section 3.5).

The word "CurrentControlSet". It is a shortcut. The real data sits in ControlSet001, ControlSet002 and so on, and CurrentControlSet points to the one in use. Always read it through CurrentControlSet.

Read-only look in regedit. Press Win + R, type regedit, press Enter, then go to the path above and click a service. Only look. Do not edit values: a wrong Start or ImagePath can stop Windows booting.

A note about drivers. The same Services area also lists kernel drivers (they have Type values like 1). The SCM and the kernel use it for both. Chapter 13 covers drivers.

Real-world analogy: The service database is the staff roster: every employee, their job, when they start, who they report to, and what to do if they fail to show.

2.3 SCM API (Start/Stop/Query)

What it is: An API (Application Programming Interface) is a set of functions that programs can call to ask Windows to do things. The SCM API is the set of functions to control services. Every tool you use (services.msc, sc, PowerShell, Task Manager) uses it behind the scenes.

The main function families (names from the Windows programming interface, shown so you recognise them):

FunctionPlain meaning
OpenSCManager"Let me talk to the manager"
OpenService"Give me a handle to this one service" (a handle is a ticket for later requests)
StartServiceStart it
ControlServiceSend a control: stop, pause, continue
QueryServiceStatusAsk: is it running?
QueryServiceConfigAsk: how is it set up?
ChangeServiceConfigChange its settings (startup type and so on)
CreateService / DeleteServiceInstall or remove a service

Your tools, matched to the API:

ToolWhat it doesNeeds admin?
services.mscWindows list with buttonsViewing: no. Changing: yes
sc queryShow service stateNo
sc qc <name>Show a service's configurationNo
sc start / sc stopStart or stopYes (usually)
sc configChange settingsYes
Get-ServicePowerShell listNo
Start-Service, Stop-Service, Set-ServicePowerShell controlYes (usually)
Get-CimInstance Win32_ServiceRich details (path, account, PID)No
tasklist /svcWhich services live in which processNo

Try this (safe, read-only):

sc query type= service state= all
sc qc Spooler

Note the strange space: in sc, the equal sign is stuck to the word and then a space follows: type= service. Without that space the command fails.

Get-Service | Select-Object -First 10
Get-CimInstance Win32_Service -Filter "Name='Spooler'" | Select-Object Name, State, StartMode, StartName, PathName, ProcessId

What State can be: Stopped, Running, Paused, or the in-between states Start Pending and Stop Pending (Section 9).

Permission check. Each time a tool opens a service, the SCM checks the caller's identity against the service's security descriptor (Section 6). That is why a normal user can list services but not stop most of them.

Summary

ConceptMeaning
SCMThe service manager, living inside services.exe
Launched bywininit.exe during boot (after the kernel and smss.exe)
Service databaseRegistry key HKLM\SYSTEM\CurrentControlSet\Services, one key per service
Key valuesImagePath, Start, Type, ObjectName, DependOnService, FailureActions
SCM APIFunctions to open, start, control, query and configure services
Your toolsservices.msc, sc, PowerShell, Task Manager; all talk to the SCM