Skip to content

RagingRedRiot/windows-service-experiment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Super Simple Rust Windows Service

A proof-of-concept Windows service written in Rust, using no third-party service wrapper libraries — only the official windows crate published by Microsoft.

Motivation

The goal was to answer a simple question: What would it actually look like to implement a Windows service in Rust without relying on a library to abstract the Service Control Manager (SCM) API?

Most Rust Windows service examples reach for crates like windows-service. This project deliberately avoids that, calling the raw Win32 service APIs directly to understand exactly what the SCM expects and how the service lifecycle works at the API level.

What it does

The service runs in the background and writes a Unix timestamp to C:\Windows\Temp\service.log once per minute. Simple by design — the point is the plumbing, not the payload.

How it works

Entry point (main)

Builds a null-terminated SERVICE_TABLE_ENTRYW array and passes it to StartServiceCtrlDispatcherW. This hands execution over to the SCM, which then calls service_main on a new thread.

service_main

The function the SCM invokes. It must be #[no_mangle] unsafe extern "system" to satisfy the calling convention and symbol name requirements. It:

  1. Registers service_control_handler with RegisterServiceCtrlHandlerExW
  2. Reports SERVICE_RUNNING to the SCM via SetServiceStatus
  3. Loops in 1-second sleep increments, writing a timestamp every 60 seconds
  4. Reports SERVICE_STOPPED on exit

Sleeping in 1-second increments (rather than one 60-second sleep) keeps the service responsive to stop signals without needing synchronization primitives like condvars or channels.

service_control_handler

Called by the SCM when a control code is received (e.g., from sc stop or the Services UI). On SERVICE_CONTROL_STOP, sets a global AtomicBool that causes the main loop to exit cleanly.

Dependencies

[dependencies]
windows = { version = "0.48.0", features = [
    "Win32_Foundation",
    "Win32_System_Services"
] }

No other dependencies.

Building

Requires a Windows target and the MSVC toolchain:

cargo build --release --target x86_64-pc-windows-msvc

Installing and running

Register the service with the SCM (run as Administrator):

sc create SuperSimpleRustService binPath= "C:\path\to\super_simple_rust_service.exe"
sc start SuperSimpleRustService

Stop and remove it:

sc stop SuperSimpleRustService
sc delete SuperSimpleRustService

About

Proof-of-concept Windows service written in Rust using only the official windows crate, no wrapper libraries.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages