winservice

package module
v0.0.0-...-a81a6e5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

WinService Manager (Go)

A lightweight Go library to manage Windows services, including creation, deletion, start/stop, auto-restart configuration, visibility control (hide/unhide), and service status query.

✨ Features

  • Create and delete Windows services
  • Start and stop services
  • Set service auto-recovery policies
  • Hide/unhide services via SDDL
  • Query service status
  • Detect if service exists
  • Written in pure Go, using golang.org/x/sys/windows

🛠 Installation

go get github.com/Firstnsnd/winservice

🚀 Usage

⚠ Requires administrator privileges.

Create, Start and Hide Service

package main

import "github.com/Firstnsnd/winservice"

func main() {
    name := "MyHiddenService"
    exe := "C:\\path\\to\\my-service.exe"

    err := winservice.CreateService(name, exe, true)
    if err != nil {
        panic(err)
    }

    _ = winservice.SetServiceHidden(name)
    _ = winservice.StartService(name)
}
Stop, Unhide and Delete Service
_ = winservice.StopService("MyHiddenService")
_ = winservice.SetServiceUnHidden("MyHiddenService")
_ = winservice.DeleteService("MyHiddenService")
Check Service Existence and Status
exists, _ := winservice.ServiceExists("MyHiddenService")
status, _ := winservice.QueryServiceStatus("MyHiddenService")
Set Recovery Policy
_ = winservice.SetRecoveryActions("MyHiddenService")

🧪 Testing

Run the unit test (requires admin permission):

go test -v ./...

Or specifically:

go test -run TestFullServiceLifecycle

🧩 Error Handling

All exported functions return wrapped errors. You can use errors.Is for specific error types:

err := winservice.StartService("svc")
if errors.Is(err, winservice.ErrServiceStartFail) {
	log.Println("Start failed with known reason")
}

📁 Project Structure


winservice/
├── service.go         # Core service operations
├── sddl.go            # Service hiding via SDDL
├── utils.go           # Some helper functions
└── service_test.go    # Full lifecycle test

✅ Requirements

  • Windows OS
  • Go 1.16+

📄 License

Apache License 2.0

Documentation

Index

Constants

View Source
const (
	SERVICE_QUERY_STATUS     uint32 = 0x0004
	SERVICE_QUERY_CONFIG     uint32 = 0x0001
	SERVICE_CHANGE_CONFIG    uint32 = 0x0002
	SERVICE_START            uint32 = 0x0010
	SERVICE_STOP             uint32 = 0x0020
	SERVICE_ALL_ACCESS       uint32 = 0xF01FF
	SERVICE_STOPPED          uint32 = 0x00000001
	SERVICE_START_PENDING    uint32 = 0x00000002
	SERVICE_STOP_PENDING     uint32 = 0x00000003
	SERVICE_RUNNING          uint32 = 0x00000004
	SERVICE_CONTINUE_PENDING uint32 = 0x00000005
	SERVICE_PAUSE_PENDING    uint32 = 0x00000006
	SERVICE_PAUSED           uint32 = 0x00000007
)
View Source
const (
	DACL_SECURITY_INFORMATION = 0x00000004
)

DACL_SECURITY_INFORMATION Security descriptor information flags

Variables

View Source
var (
	ErrServiceManagerConnectFail   = errors.New("service manager connect fail")
	ErrServiceOpenFail             = errors.New("service open fail")
	ErrServiceCreateFail           = errors.New("service create fail")
	ErrServiceStartFail            = errors.New("service start fail")
	ErrServiceStopFail             = errors.New("service stop fail")
	ErrServiceDeleteFail           = errors.New("service delete fail")
	ErrServiceStatusGetFail        = errors.New("service status get fail")
	ErrServiceStopTimeout          = errors.New("service stop timeout")
	ErrServiceAlreadyExists        = errors.New("service is already exist")
	ErrServiceRecoveryStrategyFail = errors.New("service recovery strategy set fail")
	ErrServiceSecuritySetFail      = errors.New("service security set fail")
)

Common errors for service operations

Functions

func CreateService

func CreateService(serviceName, binPath string, autostart bool) error

CreateService registers a new Windows service

func DeleteService

func DeleteService(serviceName string) error

func IsAdmin

func IsAdmin() bool

IsAdmin 判断当前进程是否拥有管理员权限

func IsServiceHidden

func IsServiceHidden(serviceName string) (bool, error)

IsServiceHidden check service is hidden

func QueryServiceStatus

func QueryServiceStatus(serviceName string) (uint32, error)

QueryServiceStatus returns the current state of a service

func ServiceExists

func ServiceExists(serviceName string) (bool, error)

ServiceExists checks if a service with the given name exists

func SetServiceHidden

func SetServiceHidden(serviceName string) error

SetServiceHidden makes a service hidden from regular users

func SetServiceUnHidden

func SetServiceUnHidden(serviceName string) error

SetServiceUnHidden makes a service visible to regular users

func StartService

func StartService(serviceName string) error

StartService starts an existing Windows service

func StopService

func StopService(serviceName string) error

StopService stops a running Windows service with timeout

Types

type SERVICE_STATUS

type SERVICE_STATUS struct {
	ServiceType             uint32
	ServiceState            uint32
	ControlsAccepted        uint32
	Win32ExitCode           uint32
	ServiceSpecificExitCode uint32
	CheckPoint              uint32
	WaitHint                uint32
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL