Win32-services: Windows service applications

[ bsd3, library, system ] [ Propose Tags ] [ Report a vulnerability ]

This package provides a partial binding to the Win32 System Services API. It makes it easy to write Windows service applications using Haskell. This version supports both 32-bit and 64-bit versions of GHC.

The binding is partial. Here are a few ways in which it differs from the official API:

  • Only services running within their own process are supported. These are processes of the WIN32_OWN_PROCESS type.

  • In cases where multiple versions of the same function exist (for compatibility), this binding only offers one of them.

  • None of the extended control codes are supported. Handlers you write will automatically report this to the operating system when such controls are received.

  • Only facilities for writing services are supported; not controlling them.

Effort has been made to simplify using the API without hiding what is happening behind the scenes. Users are encouraged to read Microsoft's documentation under 'Dev Center - Desktop > Docs > Desktop app development documentation > System Services > Services'. The official example has been ported to Haskell. This can be found in the examples directory of the source tree.

Simple Example and Usage

module Main where

import Control.Concurrent.MVar
import System.Win32.Services

main = do
    mStop <- newEmptyMVar
    startServiceCtrlDispatcher "Test" 3000 (handler mStop) $ \_ _ h -> do
        setServiceStatus h running
        takeMVar mStop
        setServiceStatus h stopped

handler mStop hStatus Stop = do
    setServiceStatus hStatus stopPending
    putMVar mStop ()
    return True
handler _ _ Interrogate = return True
handler _ _ _           = return False

running = ServiceStatus Win32OwnProcess Running [AcceptStop] nO_ERROR 0 0 0
stopped = ServiceStatus Win32OwnProcess Stopped [] nO_ERROR 0 0 0
stopPending = ServiceStatus Win32OwnProcess StopPending [AcceptStop] nO_ERROR 0 0 0
C:programmingtest>ghc --make -threaded Main.hs
[1 of 1] Compiling Main             ( Main.hs, Main.o )
Linking Main.exe ...
<linker warnings omitted>
C:\programming\test>copy Main.exe c:\svc\Test.exe
1 file(s) copied.

Execute the following from an elevated command prompt to register the service:

C:\svc>sc create Test binPath= c:\svc\Test.exe
[SC] CreateService SUCCESS

The service can now be started and stopped from the services console.

Installation Notes:

Depending on which version of GHC you are using you may also need to add a extra-lib-dirs directive. In GHC version 8.4.3 it works out of the box with the libadvapi32.a file in ...\mingw\x86_64-w64-mingw32\lib. For older version you need to use a version from a Windows SDK. Your .cabal file will then need to be modified before installing. A simple `cabal install Win32-services` may not work. For example, If you are building on Windows 8 64-bit with the Windows 8 SDK the 'extra-lib-dirs' field will need to be changed to read as follows:

Extra-Lib-Dirs: "C:\\Program Files (x86)\\Windows Kits\\8.0\\Lib\\win8\\um\\x86"

If building with stack an option is to set it in stack.yaml. For example with the Windows 10 SDK installed with Build Tools for Visual Studio 2017:

extra-lib-dirs:
- C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\um\x64

Modules

[Index] [Quick Jump]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1, 0.2, 0.2.1, 0.2.2, 0.2.2.1, 0.2.3, 0.2.3.1, 0.2.4, 0.2.5, 0.2.5.1, 0.3, 0.4, 0.4.0.1 (info)
Change log changelog
Dependencies base (>=4.5 && <5.0), Win32 (>=2.2 && <2.15), Win32-errors (>=0.2 && <0.3) [details]
Tested with ghc ==7.6.3, ghc ==7.8.3, ghc ==7.10.1, ghc ==8.2.2, ghc ==8.4.3, ghc ==8.6.5
License BSD-3-Clause
Copyright Copyright 2011-2018 Michael Steele
Author Michael Steele
Maintainer Michael Steele <mikesteele81@gmail.com>
Revised Revision 1 made by gussen at 2024-04-05T10:37:59Z
Category System
Home page http://github.com/mikesteele81/win32-services
Bug tracker http://github.com/mikesteele81/win32-services/issues
Source repo head: git clone git://github.com/mikesteele81/win32-services.git
Uploaded by gussen at 2019-05-15T17:58:00Z
Distributions NixOS:0.4.0.1
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 10366 total (33 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]