#service #windows #control #delete #status

windows_service_controller

A library to control windows service

2 releases

0.1.3 Apr 3, 2024
0.1.2 Apr 2, 2024
0.1.1 Mar 27, 2024

#15 in #delete

Download history 78/week @ 2024-03-21 248/week @ 2024-03-28 83/week @ 2024-04-04

409 downloads per month

MIT license

25KB
456 lines

windows_service_controllerwindows_service_controller on crates.iowindows_service_controller on docs.rs

Make it easier to control windows service.

Current features:

  • Basic operations:

    • Open an existing service

    • Create a new service

    • Delete a service

    • Query the status of service

    • Edit config of service

Usage

# Cargo.toml
[dependencies]
windows_service_controller = "0.1"

Open an existing service

use windows_service_controller::WindowsService;
use windows_service_controller::dword::service_access;

fn open_service() {
        let service = WindowsService::open("WSearch",
            Some(service_access::GENERIC_READ), None);
        match service {
            Ok(s) => {
                println!("{:?}", s.config)
            }
            Err(e) => {
                println!("{}", e)
            }
        }
    }

Create a new service

use windows_service_controller::WindowsService;
use windows_service_controller::dword::{sc_manager_access,
    service_access,service_type,service_start_type,service_error_control};

fn create_service() {
        let service = WindowsService::new(
            "Lers",
            None,
            Some(sc_manager_access::GENERIC_WRITE),
            Some(service_access::GENERIC_WRITE),
            service_type::SERVICE_WIN32_OWN_PROCESS,
            service_start_type::SERVICE_DEMAND_START,
            service_error_control::SERVICE_ERROR_NORMAL,
            "Path to Binary",
            None,
        );
        match service {
            Ok(s) => {
                println!("{:?}", s.config)
            }
            Err(e) => {
                println!("{}", e)
            }
        }
    }

Delete a service

use windows_service_controller::WindowsService;

fn delete_service() {
        let service = WindowsService::open("Lers", None, None);
        match service {
            Ok(s) => match s.delete_service() {
                Ok(_) => {
                    println!("succeed")
                }
                Err(e) => {
                    println!("{}", e);
                }
            },
            Err(e) => {
                println!("{}", e);
            }
        }
    }

Edit service config

use windows_service_controller::{WindowsService,PWSTR};

fn update_service_config() {
        let service = WindowsService::open("Lers", None, None);
        match service {
            Ok(mut s) => {
                s.config.lpDisplayName = PWSTR!("lers test");
                match s.update_service_config(None) {
                    Ok(_) => {
                        println!("succeed")
                    }
                    Err(e) => {
                        println!("{}", e);
                    }
                }
            }
            Err(e) => {
                println!("{}", e);
            }
        }
    }

BUG: "lpServiceStartName" can't be edit.

Dependencies

~141MB
~2.5M SLoC