#service #windows-system #hassle #called #user

winservice

Run a Windows system service without hassle

2 releases

Uses old Rust 2015

0.1.1 Oct 2, 2016
0.1.0 Feb 26, 2016

#155 in Windows APIs

23 downloads per month

Apache-2.0

12KB
169 lines

winservice

A very small Rust library to easily set up and run a Windows system service.

Documentation

Example Usage

Cargo.toml:

[dependencies]
winservice = "0.1.1"

main.rs:

#![no_main]
#![feature(link_args)]
#![link_args = "-Wl,--subsystem,windows"]

use std::os::raw::{c_char, c_int, c_void};
use std::sync::mpsc::Receiver;

#[macro_use]
extern crate winservice;

#[allow(non_snake_case)]
#[no_mangle]
pub extern "system" fn WinMain(hInstance : *const c_void, hPrevInstance : *const c_void,
    lpCmdLine : *const c_char, nCmdShow : c_int) -> c_int
{
    Service!("myService", service_main)
}

fn service_main(args : Vec<String>, end : Receiver<()>) -> u32 {
    loop {
        // Do some work
        if let Ok(_) = end.try_recv() { break; } }
 0 }

lib.rs:

Run a Windows system service without hassle.

This crate exports two methods, but they should not be called directly from user code! Instead use the provided Service! macro. The exports are necessary because there is no way to provide the ServiceMain callback with a custom user pointer which could be used to extract its context. Funnily, this is possible for the ControlHandlerEx callback, so at least we only have to use this dirty trick once.

No runtime deps