# WinServicesLib Package
{: .no_toc }

The **WinServicesLib** built-in package wraps the Windows Service Control Manager so a twinBASIC EXE can run as one or more Windows services. The same EXE typically does double duty as the install / control-panel tool when launched normally and as the service host when launched by the SCM; both modes coexist in a single `Sub Main`. The package handles the SCM handshake, the service-thread dispatch, the control-code routing, and the install / uninstall registry plumbing.

The package is a built-in package shipped with twinBASIC. Add it through Project → References... (**Ctrl-T**) → the **Available Packages** tab.

* TOC
{:toc}

## What a Windows service is

A *Windows service* is a long-running background process supervised by the **Service Control Manager (SCM)**. Services can start before any user logs in, run under dedicated accounts (`LocalSystem`, `LocalService`, `NetworkService`, or any explicit user), and respond to lifecycle commands --- *Start*, *Stop*, *Pause*, *Continue* --- issued from the Services control-panel applet (`services.msc`), the `sc.exe` command-line tool, or programmatic equivalents.

A service-hosting EXE communicates with the SCM through a small set of Win32 entry points: `StartServiceCtrlDispatcherW` to hand the process over to the SCM, `RegisterServiceCtrlHandlerExW` to hook a control-code callback, `SetServiceStatus` to report state transitions, and `CreateServiceW` / `DeleteService` to register / unregister the service in the system database. **WinServicesLib** wraps all of these --- the consumer writes one class per service, declares it through the package's coordinator, and the package handles every Win32 detail.

## Lifecycle

A service-hosting EXE goes through four phases:

1. **Configure** --- at startup, declare every service the EXE knows how to host by calling [**Services.ConfigureNew**](/tB/Packages/WinServicesLib/Services.md#configurenew) and filling the returned [**ServiceManager**](/tB/Packages/WinServicesLib/ServiceManager.md). Configuration is purely in-memory and does not touch the SCM; it builds the map the dispatcher will use *if* the EXE is launched as a service host.
2. **Install** (one-time, elevated) --- register the configured services in the system database via [**ServiceManager.Install**](/tB/Packages/WinServicesLib/ServiceManager.md#install) or [**Services.InstallAll**](/tB/Packages/WinServicesLib/Services.md#installall). This writes registry entries under `HKLM\SYSTEM\CurrentControlSet\Services\<Name>` pointing at the EXE and requires administrator rights. Usually run from an installer.
3. **Run as a service** (when the SCM launches the EXE) --- the EXE's `Sub Main` detects it was launched as a service host (typically by inspecting `Command` for a known argument like `"-startService"`) and calls [**Services.RunServiceDispatcher**](/tB/Packages/WinServicesLib/Services.md#runservicedispatcher). This blocks the main thread inside `StartServiceCtrlDispatcherW` until the SCM signals shutdown. The SCM spawns a separate service thread per service and calls into the package's dispatcher trampoline; the trampoline reports `StartPending`, then invokes the user's [**ITbService.EntryPoint**](/tB/Packages/WinServicesLib/ITbService.md#entrypoint) on the service thread.
4. **Run normally** (when a user launches the EXE) --- the EXE's `Sub Main` does *not* see the service-host argument and proceeds to whatever UI / CLI logic the EXE provides for installation, status display, or interactive testing. The same configured [**ServiceManager**](/tB/Packages/WinServicesLib/ServiceManager.md) instances are still reachable through [**Services.GetConfiguredService**](/tB/Packages/WinServicesLib/Services.md#getconfiguredservice) and the [**For Each**](/tB/Packages/WinServicesLib/Services.md#_newenum) enumerator, which is what enables a single-EXE install-and-host design.

The canonical `Sub Main` skeleton:

```tb check_build projname=winservices-overview
Module Startup
    Public Sub Main()
        With Services.ConfigureNew
            .Name             = "MyService"
            .Description      = "An example twinBASIC service"
            .Type             = tbServiceTypeOwnProcess
            .InstallStartMode = tbServiceStartOnDemand
            .InstallCmdLine   = """" & App.ModulePath & """ -startService"
            .InstanceCreator  = New ServiceCreator(Of MyService)
        End With

        If InStr(Command, "-startService") > 0 Then
            Services.RunServiceDispatcher       ' blocks until the SCM signals shutdown
        Else
            MainForm.Show                       ' control-panel / install UI
        End If
    End Sub
End Module
```

The `-startService` discriminator is the conventional way for the EXE to know which mode it is in. The `InstallCmdLine` field embeds this argument so the SCM passes it back when launching the service; the user-launched path sees no such argument and falls through to the UI branch.

## The two-thread split
{: #two-thread-split }

When the SCM launches the EXE as a service host, twinBASIC's runtime runs **two threads** for each service:

- The **service thread** --- the SCM-spawned thread that runs the user's [**ITbService.EntryPoint**](/tB/Packages/WinServicesLib/ITbService.md#entrypoint). This is where the service does its actual work. The thread is created by `StartServiceCtrlDispatcherW`'s machinery; it is *not* the main thread of the EXE.
- The **dispatcher thread** --- the EXE's main thread, which is what the SCM invokes when it has a control code to deliver (*Stop*, *Pause*, *Continue*, …). The package routes the control through `RegisterServiceCtrlHandlerExW` to a trampoline that calls the user's [**ITbService.ChangeState**](/tB/Packages/WinServicesLib/ITbService.md#changestate).

The two methods therefore run *concurrently*: while [**EntryPoint**](/tB/Packages/WinServicesLib/ITbService.md#entrypoint) is doing the service's work on the service thread, [**ChangeState**](/tB/Packages/WinServicesLib/ITbService.md#changestate) waits idle on the dispatcher thread, and the SCM wakes it on demand to deliver a control code. The two methods must coordinate through shared `Public` flags on the service class --- `IsStopping`, `IsPaused`, and similar --- because the package cannot stop the service thread except through the user's own code path.

```tb check_build projname=winservices-overview
Class MyService
    Implements ITbService

    Public IsStopping As Boolean

    Sub EntryPoint(ByVal ServiceManager As ServiceManager) _
            Implements ITbService.EntryPoint
        ServiceManager.ReportStatus vbServiceStatusRunning
        Do Until IsStopping
            ' …do work, then yield with WaitForSingleObject / Sleep / etc.
        Loop
        ServiceManager.ReportStatus vbServiceStatusStopped
    End Sub

    Sub ChangeState(ByVal ServiceManager As ServiceManager, _
                    ByVal dwControl As ServiceControlCodeConstants, _
                    ByVal dwEventType As Long, _
                    ByVal lpEventData As LongPtr) _
            Implements ITbService.ChangeState
        Select Case dwControl
            Case vbServiceControlStop, vbServiceControlShutdown
                ServiceManager.ReportStatus vbServiceStatusStopPending
                IsStopping = True       ' wakes EntryPoint's loop on the other thread
        End Select
    End Sub

    Sub StartupFailed(ByVal ServiceManager As ServiceManager) _
            Implements ITbService.StartupFailed
        ' …optional failure-reporting hook
    End Sub
End Class
```

The shared-flag pattern is the documented coordination mechanism --- there is no built-in cancellation primitive. For services that host an inherently message-loop-driven object (a [**NamedPipeServer**](/tB/Packages/WinNamedPipesLib/NamedPipeServer.md), a window-message handler, …) that object's own *Stop*-signal method is usually called from [**ChangeState**](/tB/Packages/WinServicesLib/ITbService.md#changestate); see [the WinNamedPipesLib service-host idiom](/tB/Packages/WinNamedPipesLib/index.md#service-host-idiom) for a worked example.

## Integration with the sister "winlibs" packages

`WinServicesLib` is most often used together with [**WinEventLogLib**](/tB/Packages/WinEventLogLib/index.md) and [**WinNamedPipesLib**](/tB/Packages/WinNamedPipesLib/index.md) --- Windows services typically need a place to write diagnostic events (the Windows Event Log) and a way to communicate with non-service processes (named pipes). The three packages integrate well:

- **Logging** --- every service class can mix in the [**EventLog**](/tB/Packages/WinEventLogLib/EventLog.md) members through the [composition-delegation idiom](/tB/Packages/WinEventLogLib/index.md#composition-delegation-idiom) (`Implements EventLog(Of EVENTS, CATEGORIES) Via EventLog = New EventLog(...)`), so [**LogSuccess**](/tB/Packages/WinEventLogLib/EventLog.md#logsuccess) / [**LogFailure**](/tB/Packages/WinEventLogLib/EventLog.md#logfailure) read as plain method calls inside `EntryPoint` and `ChangeState`. The events fire under the service account (typically `LocalSystem`), which the Event Viewer renders against the message-table resource embedded in the EXE.
- **IPC** --- a [**NamedPipeServer**](/tB/Packages/WinNamedPipesLib/NamedPipeServer.md) hosted inside the service uses [**ManualMessageLoopEnter**](/tB/Packages/WinNamedPipesLib/NamedPipeServer.md#manualmessageloopenter) as the [**EntryPoint**](/tB/Packages/WinServicesLib/ITbService.md#entrypoint)'s blocking primitive, and [**ManualMessageLoopLeave**](/tB/Packages/WinNamedPipesLib/NamedPipeServer.md#manualmessageloopleave) from [**ChangeState**](/tB/Packages/WinServicesLib/ITbService.md#changestate) becomes the *Stop*-signal mechanism. See [Hosting inside a Windows service](/tB/Packages/WinNamedPipesLib/index.md#service-host-idiom) for the complete pattern, including pause / continue and the dispatcher-thread / service-thread interaction.

## Installation and elevation

[**Install**](/tB/Packages/WinServicesLib/ServiceManager.md#install) and [**Uninstall**](/tB/Packages/WinServicesLib/ServiceManager.md#uninstall) (and their bulk-helpers [**Services.InstallAll**](/tB/Packages/WinServicesLib/Services.md#installall) / [**Services.UninstallAll**](/tB/Packages/WinServicesLib/Services.md#uninstallall)) call `CreateServiceW` / `DeleteService`, which require an SCM handle opened with `SC_MANAGER_CREATE_SERVICE`. Both succeed only when the calling process runs with **administrator rights**. The typical project structure:

- A standalone installer EXE (or installer mode inside the same EXE, gated by a `-install` command-line argument) runs elevated and calls [**Install**](/tB/Packages/WinServicesLib/ServiceManager.md#install) / [**Uninstall**](/tB/Packages/WinServicesLib/ServiceManager.md#uninstall) plus a one-time call to [**EventLog.Register**](/tB/Packages/WinEventLogLib/EventLog.md#register).
- The service-host EXE itself does not need elevation at run-time (the SCM launches it under whatever account the service is configured for).
- The control-panel / interactive UI does not need elevation either --- it can use [**Services.LaunchService**](/tB/Packages/WinServicesLib/Services.md#launchservice) and [**Services.ControlService**](/tB/Packages/WinServicesLib/Services.md#controlservice) freely, as long as the user has the standard *Start* / *Stop* permissions on the relevant service (the default ACL grants this to **LocalSystem**, **Administrators**, and the running interactive user for *interactive* services).

Calling [**Install**](/tB/Packages/WinServicesLib/ServiceManager.md#install) while running inside the twinBASIC IDE will fail with an SCM-access error --- the IDE is rarely elevated. Either run the compiled EXE as administrator, or wrap the call in an `If App.IsInIDE() Then Err.Raise 5, , "Run the compiled EXE as administrator."` guard.

## Classes and interface

- [ITbService](/tB/Packages/WinServicesLib/ITbService.md) -- the interface every service class implements: [**EntryPoint**](/tB/Packages/WinServicesLib/ITbService.md#entrypoint), [**StartupFailed**](/tB/Packages/WinServicesLib/ITbService.md#startupfailed), [**ChangeState**](/tB/Packages/WinServicesLib/ITbService.md#changestate)
  - [ChangeState](/tB/Packages/WinServicesLib/ITbService.md#changestate) -- invoked on the dispatcher thread when the SCM delivers a control code (*Stop*, *Pause*, *Continue*, ...)
  - [EntryPoint](/tB/Packages/WinServicesLib/ITbService.md#entrypoint) -- the service's main routine, invoked on the SCM-spawned service thread once the SCM handshake has completed
  - [StartupFailed](/tB/Packages/WinServicesLib/ITbService.md#startupfailed) -- invoked when the SCM handshake fails before **EntryPoint** can run
- [ServiceCreator](/tB/Packages/WinServicesLib/ServiceCreator.md) -- the generic [**ServiceCreator**](/tB/Packages/WinServicesLib/ServiceCreator.md)`(Of T)` factory the dispatcher uses to instantiate each service class on demand; *T* must implement [**ITbService**](/tB/Packages/WinServicesLib/ITbService.md)
  - [CreateInstance](/tB/Packages/WinServicesLib/ServiceCreator.md#createinstance) -- returns a fresh `New T` cast as [**ITbService**](/tB/Packages/WinServicesLib/ITbService.md) for use by the dispatcher trampoline
- [ServiceManager](/tB/Packages/WinServicesLib/ServiceManager.md) -- one per configured service; holds the fields the SCM cares about (name, description, type, start-mode, command-line, dependencies, ...) plus the [**ReportStatus**](/tB/Packages/WinServicesLib/ServiceManager.md#reportstatus) call the service uses to inform the SCM of state transitions
  - [AutoInitializeCOM](/tB/Packages/WinServicesLib/ServiceManager.md#autoinitializecom) -- controls whether COM is initialized in STA mode on the service thread before **EntryPoint** is called; default **True**
  - [DependentServices](/tB/Packages/WinServicesLib/ServiceManager.md#dependentservices) -- list of service names the SCM must auto-start before starting this service
  - [Description](/tB/Packages/WinServicesLib/ServiceManager.md#description) -- human-readable description text displayed in `services.msc` and `sc.exe query`
  - [Install](/tB/Packages/WinServicesLib/ServiceManager.md#install) -- registers this service in the SCM database; requires administrator rights
  - [InstallStartMode](/tB/Packages/WinServicesLib/ServiceManager.md#installstartmode) -- the SCM start mode (*OnDemand*, *Auto*, *Disabled*, ...) the service is registered with
  - [InstanceCreator](/tB/Packages/WinServicesLib/ServiceManager.md#instancecreator) -- the [**ServiceCreator(Of T)**](/tB/Packages/WinServicesLib/ServiceCreator.md) factory the dispatcher calls to create the **ITbService** instance at service start
  - [Name](/tB/Packages/WinServicesLib/ServiceManager.md#name) -- the service's unique name in the SCM database, used by `services.msc` and `sc.exe`
  - [New](/tB/Packages/WinServicesLib/ServiceManager.md#new) -- initializes a new **ServiceManager** with default configuration values
  - [ReportStatus](/tB/Packages/WinServicesLib/ServiceManager.md#reportstatus) -- informs the SCM of the service's current state; called from inside **EntryPoint** and **ChangeState**
  - [ResyncStatus](/tB/Packages/WinServicesLib/ServiceManager.md#resyncstatus) -- re-applies the cached `SERVICE_STATUS` to the SCM via `SetServiceStatus`
  - [SERVICE_TABLE_ENTRYW](/tB/Packages/WinServicesLib/ServiceManager.md#service_table_entryw) -- internal Win32 UDT pairing a service name with its entry-point address; used by **RunServiceDispatcher**
  - [ServiceEntryPoint](/tB/Packages/WinServicesLib/ServiceManager.md#serviceentrypoint) -- the SCM-invoked entry point that runs on the service thread; called by the package's dispatcher trampoline, not by user code
  - [SupportsPausing](/tB/Packages/WinServicesLib/ServiceManager.md#supportspausing) -- whether the service advertises acceptance of *Pause* / *Continue* control codes to the SCM; default **False**
  - [Type](/tB/Packages/WinServicesLib/ServiceManager.md#type) -- the Win32 service type (*OwnProcess*, *ShareProcess*, ...); default **tbServiceTypeOwnProcess**
  - [Uninstall](/tB/Packages/WinServicesLib/ServiceManager.md#uninstall) -- removes this service from the SCM database; requires administrator rights
- [Services](/tB/Packages/WinServicesLib/Services.md) -- the predeclared singleton coordinator: [**ConfigureNew**](/tB/Packages/WinServicesLib/Services.md#configurenew), [**RunServiceDispatcher**](/tB/Packages/WinServicesLib/Services.md#runservicedispatcher), the bulk install / uninstall helpers, plus the runtime control methods ([**LaunchService**](/tB/Packages/WinServicesLib/Services.md#launchservice), [**ControlService**](/tB/Packages/WinServicesLib/Services.md#controlservice), [**QueryStateOfService**](/tB/Packages/WinServicesLib/Services.md#querystateofservice))
  - [_NewEnum](/tB/Packages/WinServicesLib/Services.md#_newenum) -- `For Each` enumerator across every configured [**ServiceManager**](/tB/Packages/WinServicesLib/ServiceManager.md) in insertion order
  - [ConfigureNew](/tB/Packages/WinServicesLib/Services.md#configurenew) -- allocates a fresh **ServiceManager**, registers it internally, and returns it for the caller to populate
  - [ControlService](/tB/Packages/WinServicesLib/Services.md#controlservice) -- sends an SCM control code (*Stop*, *Pause*, *Continue*, ...) to a running service by name
  - [GetConfiguredService](/tB/Packages/WinServicesLib/Services.md#getconfiguredservice) -- looks up a previously-configured **ServiceManager** by its **Name**
  - [InstallAll](/tB/Packages/WinServicesLib/Services.md#installall) -- calls **Install** on every configured **ServiceManager**; requires administrator rights
  - [LaunchService](/tB/Packages/WinServicesLib/Services.md#launchservice) -- starts an installed service by name and optionally forwards launch arguments to **ServiceManager.LaunchArgs**
  - [QueryStateOfService](/tB/Packages/WinServicesLib/Services.md#querystateofservice) -- returns a fresh **ServiceState** snapshot of an installed service
  - [UninstallAll](/tB/Packages/WinServicesLib/Services.md#uninstallall) -- calls **Uninstall** on every configured **ServiceManager**; requires administrator rights
- [ServiceState](/tB/Packages/WinServicesLib/ServiceState.md) -- a read-only state snapshot returned by [**Services.QueryStateOfService**](/tB/Packages/WinServicesLib/Services.md#querystateofservice), giving the SCM-reported state and process ID of an installed service
  - [CheckPoint](/tB/Packages/WinServicesLib/ServiceState.md#checkpoint) -- the SCM-reported `dwCheckPoint` progress counter; auto-incremented by the package while the service is in a pending state
  - [CurrentStateText](/tB/Packages/WinServicesLib/ServiceState.md#currentstatetext) -- human-readable rendering of **CurrentState** (e.g. `RUNNING`, `STOPPING`, `STOPPED`)
  - [ExitCode](/tB/Packages/WinServicesLib/ServiceState.md#exitcode) -- the Win32 exit code the service reported when it last stopped
  - [Flags](/tB/Packages/WinServicesLib/ServiceState.md#flags) -- the SCM-reported `dwServiceFlags` bitmask (e.g. `SERVICE_RUNS_IN_SYSTEM_PROCESS`)
  - [New](/tB/Packages/WinServicesLib/ServiceState.md#new) -- initializes a **ServiceState** by querying the SCM for the named service's current status
  - [ProcessId](/tB/Packages/WinServicesLib/ServiceState.md#processid) -- the OS process ID hosting the service, or **0** if not running
  - [ServiceSpecificExitCode](/tB/Packages/WinServicesLib/ServiceState.md#servicespecificexitcode) -- vendor-defined exit code, meaningful only when **ExitCode** equals `ERROR_SERVICE_SPECIFIC_ERROR` (1066)
  - [Type](/tB/Packages/WinServicesLib/ServiceState.md#type) -- the SCM-reported service type as a [**ServiceTypeConstants**](/tB/Packages/WinServicesLib/Enumerations/ServiceTypeConstants.md) value
  - [WaitHint](/tB/Packages/WinServicesLib/ServiceState.md#waithint) -- estimated upper-bound milliseconds for the current pending state transition, as last reported by the service

## Enumerations

- [Enumerations](/tB/Packages/WinServicesLib/Enumerations/index.md) -- four user-facing enumerations: [**ServiceTypeConstants**](/tB/Packages/WinServicesLib/Enumerations/ServiceTypeConstants.md), [**ServiceStartConstants**](/tB/Packages/WinServicesLib/Enumerations/ServiceStartConstants.md), [**ServiceControlCodeConstants**](/tB/Packages/WinServicesLib/Enumerations/ServiceControlCodeConstants.md), [**ServiceStatusConstants**](/tB/Packages/WinServicesLib/Enumerations/ServiceStatusConstants.md)
