#service #windows #control #delete #status #query #edit

windows_service_controller

A library to control windows service

3 releases

0.1.4 Oct 29, 2024
0.1.3 Apr 3, 2024
0.1.2 Apr 2, 2024
0.1.1 Mar 27, 2024

#63 in Windows APIs

Download history 18/week @ 2024-09-22 10/week @ 2024-09-29 118/week @ 2024-10-27 3/week @ 2024-11-03

121 downloads per month

MIT license

32KB
576 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.

Start service

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

Stop service

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

Dependencies

~128MB
~2M SLoC