pause_continue/
pause_continue.rs

1/// This is an example program that demonstrates how to pause and resume a given service.
2///
3/// Run in command prompt as admin:
4///
5/// `pause_continue.exe SERVICE_NAME`
6///
7/// Replace the `SERVICE_NAME` placeholder above with the name of the service that the program
8/// should manipulate. By default the program manipulates a WMI system service (Winmgmt) when the
9/// first argument is omitted.
10
11#[cfg(windows)]
12fn main() -> windows_service::Result<()> {
13    use std::env;
14    use windows_service::{
15        service::ServiceAccess,
16        service_manager::{ServiceManager, ServiceManagerAccess},
17    };
18
19    let service_name = env::args().nth(1).unwrap_or("Winmgmt".to_owned());
20
21    let manager_access = ServiceManagerAccess::CONNECT;
22    let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
23
24    let service = service_manager.open_service(&service_name, ServiceAccess::PAUSE_CONTINUE)?;
25
26    println!("Pause {}", service_name);
27    let paused_state = service.pause()?;
28    println!("{:?}", paused_state.current_state);
29
30    println!("Resume {}", service_name);
31    let resumed_state = service.resume()?;
32    println!("{:?}", resumed_state.current_state);
33
34    Ok(())
35}
36
37#[cfg(not(windows))]
38fn main() {
39    panic!("This program is only intended to run on Windows.");
40}