Project 5: A Basic Windows Service

A proper Windows Service that, once installed and started, writes a timestamp to a log file every 30 seconds. The executable will also handle command-line arguments to install and uninstall itself.

Quick Reference

Attribute Value
Primary Language C++
Alternative Languages C
Difficulty Level 2: Intermediate
Time Estimate 1-2 weeks
Knowledge Area Windows Services / OS Integration
Tooling Service Control Manager (SCM)
Prerequisites Project 2, understanding of function pointers.

What You Will Build

A proper Windows Service that, once installed and started, writes a timestamp to a log file every 30 seconds. The executable will also handle command-line arguments to install and uninstall itself.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Implementing ServiceMain → maps to the entry point for the service, where initialization happens
  • Implementing a Service Control Handler → maps to responding to SCM requests like START, STOP, and PAUSE
  • Communicating status to the SCM → maps to using SetServiceStatus to report state changes
  • Installing and Uninstalling the service → maps to using CreateService and DeleteService APIs
  • Debugging a service → maps to the challenge of debugging non-interactive processes

Key Concepts

Real-World Outcome

# Install the service
> ./my_service.exe install

# Open services.msc and see "My Timestamp Service" listed
# Start it from services.msc or the command line
> sc start MyTimestampService

# Check the log file
> type C:\logs\my_service.log
Service started. Timestamp: 2025-12-21 14:30:00
Service is running. Timestamp: 2025-12-21 14:30:30
Service is running. Timestamp: 2025-12-21 14:31:00
...

# Stop and uninstall the service
> sc stop MyTimestampService
> ./my_service.exe uninstall

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_WINDOWS_SYSTEMS_PROGRAMMING_CPP.md
  • “Windows System Programming, 4th Edition” by Johnson M. Hart