An open API service indexing awesome lists of open source software.

https://github.com/willibrandon/windowsservice

Pure Go Windows service implementation with zero dependencies
https://github.com/willibrandon/windowsservice

go golang scm service service-manager syscall win32 windows windows-api windows-service

Last synced: 6 months ago
JSON representation

Pure Go Windows service implementation with zero dependencies

Awesome Lists containing this project

README

          

# windowsservice

[![Test](https://github.com/willibrandon/windowsservice/actions/workflows/test.yml/badge.svg)](https://github.com/willibrandon/windowsservice/actions/workflows/test.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/willibrandon/windowsservice.svg)](https://pkg.go.dev/github.com/willibrandon/windowsservice)

Pure Go Windows service implementation with zero dependencies.

## Installation

```bash
go get github.com/willibrandon/windowsservice
```

## Usage

```go
package main

import "github.com/willibrandon/windowsservice"

func main() {
start := func() {
// Your service logic here
for {
doWork()
}
}

stop := func() {
// Cleanup
}

windowsservice.Run(start, stop)
}
```

## Running

### Console Mode

```bash
go run .
```

### Service Mode

```powershell
# Build
go build -o myservice.exe

# Install
sc create MyService binPath= "C:\path\to\myservice.exe"

# Start
sc start MyService

# Stop
sc stop MyService

# Delete
sc delete MyService
```

## Testing

Requires Administrator privileges:

```bash
go test -v ./...
```

## Implementation Details

The service uses direct Windows syscalls to `advapi32.dll` and `kernel32.dll` for SCM integration. No CGO or external dependencies required.

### Service Detection

Automatically detects execution context:
- Console: Runs directly for debugging
- Service: Integrates with Windows Service Control Manager

### Status Reporting

Properly reports service states:
- `SERVICE_RUNNING`
- `SERVICE_STOP_PENDING`
- `SERVICE_STOPPED`

## Example

See [examples/heartbeat](examples/heartbeat) for a complete example that writes heartbeat timestamps to demonstrate service lifecycle.

## License

[MIT](LICENSE)