windows_service_controller

Crates.iowindows_service_controller
lib.rswindows_service_controller
version0.1.4
sourcesrc
created_at2024-03-27 07:25:41.851249
updated_at2024-10-29 13:00:59.683021
descriptionA library to control windows service
homepagehttps://github.com/1412150209/rc_windows_service_controller
repositoryhttps://github.com/1412150209/rc_windows_service_controller
max_upload_size
id1187581
size36,631
lers梦魔 (1412150209)

documentation

README

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);
        }
    }
}

Commit count: 30

cargo fmt