Struct windows_service::service_manager::ServiceManager
source · pub struct ServiceManager { /* private fields */ }Expand description
Service manager.
Implementations§
source§impl ServiceManager
impl ServiceManager
sourcepub fn local_computer(
database: Option<impl AsRef<OsStr>>,
request_access: ServiceManagerAccess
) -> Result<Self>
pub fn local_computer( database: Option<impl AsRef<OsStr>>, request_access: ServiceManagerAccess ) -> Result<Self>
Connect to local services database.
Arguments
database- The name of database to connect to. PassNoneto connect to active database.request_access- Desired access permissions.
Examples found in repository?
examples/service_config.rs (line 12)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> windows_service::Result<()> {
use std::env;
use windows_service::{
service::ServiceAccess,
service_manager::{ServiceManager, ServiceManagerAccess},
};
let service_name = env::args().nth(1).unwrap_or("netlogon".to_owned());
let manager_access = ServiceManagerAccess::CONNECT;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service = service_manager.open_service(service_name, ServiceAccess::QUERY_CONFIG)?;
let config = service.query_config()?;
println!("{:#?}", config);
Ok(())
}More examples
examples/pause_continue.rs (line 22)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() -> windows_service::Result<()> {
use std::env;
use windows_service::{
service::ServiceAccess,
service_manager::{ServiceManager, ServiceManagerAccess},
};
let service_name = env::args().nth(1).unwrap_or("Winmgmt".to_owned());
let manager_access = ServiceManagerAccess::CONNECT;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service = service_manager.open_service(&service_name, ServiceAccess::PAUSE_CONTINUE)?;
println!("Pause {}", service_name);
let paused_state = service.pause()?;
println!("{:?}", paused_state.current_state);
println!("Resume {}", service_name);
let resumed_state = service.resume()?;
println!("{:?}", resumed_state.current_state);
Ok(())
}examples/install_service.rs (line 10)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() -> windows_service::Result<()> {
use std::ffi::OsString;
use windows_service::{
service::{ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType, ServiceType},
service_manager::{ServiceManager, ServiceManagerAccess},
};
let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
// This example installs the service defined in `examples/ping_service.rs`.
// In the real world code you would set the executable path to point to your own binary
// that implements windows service.
let service_binary_path = ::std::env::current_exe()
.unwrap()
.with_file_name("ping_service.exe");
let service_info = ServiceInfo {
name: OsString::from("ping_service"),
display_name: OsString::from("Ping service"),
service_type: ServiceType::OWN_PROCESS,
start_type: ServiceStartType::OnDemand,
error_control: ServiceErrorControl::Normal,
executable_path: service_binary_path,
launch_arguments: vec![],
dependencies: vec![],
account_name: None, // run as System
account_password: None,
};
let service = service_manager.create_service(&service_info, ServiceAccess::CHANGE_CONFIG)?;
service.set_description("Windows service example from windows-service-rs")?;
Ok(())
}examples/uninstall_service.rs (line 15)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() -> windows_service::Result<()> {
use std::{
thread::sleep,
time::{Duration, Instant},
};
use windows_service::{
service::{ServiceAccess, ServiceState},
service_manager::{ServiceManager, ServiceManagerAccess},
};
use windows_sys::Win32::Foundation::ERROR_SERVICE_DOES_NOT_EXIST;
let manager_access = ServiceManagerAccess::CONNECT;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service_access = ServiceAccess::QUERY_STATUS | ServiceAccess::STOP | ServiceAccess::DELETE;
let service = service_manager.open_service("ping_service", service_access)?;
// The service will be marked for deletion as long as this function call succeeds.
// However, it will not be deleted from the database until it is stopped and all open handles to it are closed.
service.delete()?;
// Our handle to it is not closed yet. So we can still query it.
if service.query_status()?.current_state != ServiceState::Stopped {
// If the service cannot be stopped, it will be deleted when the system restarts.
service.stop()?;
}
// Explicitly close our open handle to the service. This is automatically called when `service` goes out of scope.
drop(service);
// Win32 API does not give us a way to wait for service deletion.
// To check if the service is deleted from the database, we have to poll it ourselves.
let start = Instant::now();
let timeout = Duration::from_secs(5);
while start.elapsed() < timeout {
if let Err(windows_service::Error::Winapi(e)) =
service_manager.open_service("ping_service", ServiceAccess::QUERY_STATUS)
{
if e.raw_os_error() == Some(ERROR_SERVICE_DOES_NOT_EXIST as i32) {
println!("ping_service is deleted.");
return Ok(());
}
}
sleep(Duration::from_secs(1));
}
println!("ping_service is marked for deletion.");
Ok(())
}examples/service_failure_actions.rs (line 17)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
fn main() -> windows_service::Result<()> {
use std::ffi::OsString;
use std::time::Duration;
use windows_service::{
service::{
ServiceAccess, ServiceAction, ServiceActionType, ServiceErrorControl,
ServiceFailureActions, ServiceFailureResetPeriod, ServiceInfo, ServiceStartType,
ServiceType,
},
service_manager::{ServiceManager, ServiceManagerAccess},
};
const SERVICE_NAME: &str = "service_failure_actions_example";
let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service_binary_path = ::std::env::current_exe()
.unwrap()
.with_file_name("service_failure_actions.exe");
let service_info = ServiceInfo {
name: OsString::from(SERVICE_NAME),
display_name: OsString::from("Service Failure Actions Example"),
service_type: ServiceType::OWN_PROCESS,
start_type: ServiceStartType::OnDemand,
error_control: ServiceErrorControl::Normal,
executable_path: service_binary_path,
launch_arguments: vec![],
dependencies: vec![],
account_name: None, // run as System
account_password: None,
};
let service_access = ServiceAccess::QUERY_CONFIG
| ServiceAccess::CHANGE_CONFIG
| ServiceAccess::START
| ServiceAccess::DELETE;
println!("Create or open the service {}", SERVICE_NAME);
let service = service_manager
.create_service(&service_info, service_access)
.or(service_manager.open_service(SERVICE_NAME, service_access))?;
let actions = vec![
ServiceAction {
action_type: ServiceActionType::Restart,
delay: Duration::from_secs(5),
},
ServiceAction {
action_type: ServiceActionType::RunCommand,
delay: Duration::from_secs(10),
},
ServiceAction {
action_type: ServiceActionType::None,
delay: Duration::default(),
},
];
println!("Update failure actions");
let failure_actions = ServiceFailureActions {
reset_period: ServiceFailureResetPeriod::After(Duration::from_secs(86400 * 2)),
reboot_msg: None,
command: Some(OsString::from("ping 127.0.0.1")),
actions: Some(actions),
};
service.update_failure_actions(failure_actions)?;
println!("Query failure actions");
let updated_failure_actions = service.get_failure_actions()?;
println!("{:#?}", updated_failure_actions);
println!("Enable failure actions on non-crash failures");
service.set_failure_actions_on_non_crash_failures(true)?;
println!("Query failure actions on non-crash failures enabled");
let failure_actions_flag = service.get_failure_actions_on_non_crash_failures()?;
println!(
"Failure actions on non-crash failures enabled: {}",
failure_actions_flag
);
println!("Delete the service {}", SERVICE_NAME);
service.delete()?;
Ok(())
}sourcepub fn remote_computer(
machine: impl AsRef<OsStr>,
database: Option<impl AsRef<OsStr>>,
request_access: ServiceManagerAccess
) -> Result<Self>
pub fn remote_computer( machine: impl AsRef<OsStr>, database: Option<impl AsRef<OsStr>>, request_access: ServiceManagerAccess ) -> Result<Self>
Connect to remote services database.
Arguments
machine- The name of remote machine.database- The name of database to connect to. PassNoneto connect to active database.request_access- desired access permissions.
sourcepub fn create_service(
&self,
service_info: &ServiceInfo,
service_access: ServiceAccess
) -> Result<Service>
pub fn create_service( &self, service_info: &ServiceInfo, service_access: ServiceAccess ) -> Result<Service>
Create a service.
Arguments
service_info- The service information that will be saved to the system services registry.service_access- Desired access permissions for the returnedServiceinstance.
Example
use std::ffi::OsString;
use std::path::PathBuf;
use windows_service::service::{
ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType, ServiceType,
};
use windows_service::service_manager::{ServiceManager, ServiceManagerAccess};
fn main() -> windows_service::Result<()> {
let manager =
ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::CREATE_SERVICE)?;
let my_service_info = ServiceInfo {
name: OsString::from("my_service"),
display_name: OsString::from("My service"),
service_type: ServiceType::OWN_PROCESS,
start_type: ServiceStartType::OnDemand,
error_control: ServiceErrorControl::Normal,
executable_path: PathBuf::from(r"C:\path\to\my\service.exe"),
launch_arguments: vec![],
dependencies: vec![],
account_name: None, // run as System
account_password: None,
};
let my_service = manager.create_service(&my_service_info, ServiceAccess::QUERY_STATUS)?;
Ok(())
}Examples found in repository?
examples/install_service.rs (line 31)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() -> windows_service::Result<()> {
use std::ffi::OsString;
use windows_service::{
service::{ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType, ServiceType},
service_manager::{ServiceManager, ServiceManagerAccess},
};
let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
// This example installs the service defined in `examples/ping_service.rs`.
// In the real world code you would set the executable path to point to your own binary
// that implements windows service.
let service_binary_path = ::std::env::current_exe()
.unwrap()
.with_file_name("ping_service.exe");
let service_info = ServiceInfo {
name: OsString::from("ping_service"),
display_name: OsString::from("Ping service"),
service_type: ServiceType::OWN_PROCESS,
start_type: ServiceStartType::OnDemand,
error_control: ServiceErrorControl::Normal,
executable_path: service_binary_path,
launch_arguments: vec![],
dependencies: vec![],
account_name: None, // run as System
account_password: None,
};
let service = service_manager.create_service(&service_info, ServiceAccess::CHANGE_CONFIG)?;
service.set_description("Windows service example from windows-service-rs")?;
Ok(())
}More examples
examples/service_failure_actions.rs (line 43)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
fn main() -> windows_service::Result<()> {
use std::ffi::OsString;
use std::time::Duration;
use windows_service::{
service::{
ServiceAccess, ServiceAction, ServiceActionType, ServiceErrorControl,
ServiceFailureActions, ServiceFailureResetPeriod, ServiceInfo, ServiceStartType,
ServiceType,
},
service_manager::{ServiceManager, ServiceManagerAccess},
};
const SERVICE_NAME: &str = "service_failure_actions_example";
let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service_binary_path = ::std::env::current_exe()
.unwrap()
.with_file_name("service_failure_actions.exe");
let service_info = ServiceInfo {
name: OsString::from(SERVICE_NAME),
display_name: OsString::from("Service Failure Actions Example"),
service_type: ServiceType::OWN_PROCESS,
start_type: ServiceStartType::OnDemand,
error_control: ServiceErrorControl::Normal,
executable_path: service_binary_path,
launch_arguments: vec![],
dependencies: vec![],
account_name: None, // run as System
account_password: None,
};
let service_access = ServiceAccess::QUERY_CONFIG
| ServiceAccess::CHANGE_CONFIG
| ServiceAccess::START
| ServiceAccess::DELETE;
println!("Create or open the service {}", SERVICE_NAME);
let service = service_manager
.create_service(&service_info, service_access)
.or(service_manager.open_service(SERVICE_NAME, service_access))?;
let actions = vec![
ServiceAction {
action_type: ServiceActionType::Restart,
delay: Duration::from_secs(5),
},
ServiceAction {
action_type: ServiceActionType::RunCommand,
delay: Duration::from_secs(10),
},
ServiceAction {
action_type: ServiceActionType::None,
delay: Duration::default(),
},
];
println!("Update failure actions");
let failure_actions = ServiceFailureActions {
reset_period: ServiceFailureResetPeriod::After(Duration::from_secs(86400 * 2)),
reboot_msg: None,
command: Some(OsString::from("ping 127.0.0.1")),
actions: Some(actions),
};
service.update_failure_actions(failure_actions)?;
println!("Query failure actions");
let updated_failure_actions = service.get_failure_actions()?;
println!("{:#?}", updated_failure_actions);
println!("Enable failure actions on non-crash failures");
service.set_failure_actions_on_non_crash_failures(true)?;
println!("Query failure actions on non-crash failures enabled");
let failure_actions_flag = service.get_failure_actions_on_non_crash_failures()?;
println!(
"Failure actions on non-crash failures enabled: {}",
failure_actions_flag
);
println!("Delete the service {}", SERVICE_NAME);
service.delete()?;
Ok(())
}sourcepub fn open_service(
&self,
name: impl AsRef<OsStr>,
request_access: ServiceAccess
) -> Result<Service>
pub fn open_service( &self, name: impl AsRef<OsStr>, request_access: ServiceAccess ) -> Result<Service>
Open an existing service.
Arguments
name- The service name.request_access- Desired permissions for the returnedServiceinstance.
Example
use windows_service::service::ServiceAccess;
use windows_service::service_manager::{ServiceManager, ServiceManagerAccess};
let manager = ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::CONNECT)?;
let my_service = manager.open_service("my_service", ServiceAccess::QUERY_STATUS)?;Examples found in repository?
examples/service_config.rs (line 14)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> windows_service::Result<()> {
use std::env;
use windows_service::{
service::ServiceAccess,
service_manager::{ServiceManager, ServiceManagerAccess},
};
let service_name = env::args().nth(1).unwrap_or("netlogon".to_owned());
let manager_access = ServiceManagerAccess::CONNECT;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service = service_manager.open_service(service_name, ServiceAccess::QUERY_CONFIG)?;
let config = service.query_config()?;
println!("{:#?}", config);
Ok(())
}More examples
examples/pause_continue.rs (line 24)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() -> windows_service::Result<()> {
use std::env;
use windows_service::{
service::ServiceAccess,
service_manager::{ServiceManager, ServiceManagerAccess},
};
let service_name = env::args().nth(1).unwrap_or("Winmgmt".to_owned());
let manager_access = ServiceManagerAccess::CONNECT;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service = service_manager.open_service(&service_name, ServiceAccess::PAUSE_CONTINUE)?;
println!("Pause {}", service_name);
let paused_state = service.pause()?;
println!("{:?}", paused_state.current_state);
println!("Resume {}", service_name);
let resumed_state = service.resume()?;
println!("{:?}", resumed_state.current_state);
Ok(())
}examples/uninstall_service.rs (line 18)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() -> windows_service::Result<()> {
use std::{
thread::sleep,
time::{Duration, Instant},
};
use windows_service::{
service::{ServiceAccess, ServiceState},
service_manager::{ServiceManager, ServiceManagerAccess},
};
use windows_sys::Win32::Foundation::ERROR_SERVICE_DOES_NOT_EXIST;
let manager_access = ServiceManagerAccess::CONNECT;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service_access = ServiceAccess::QUERY_STATUS | ServiceAccess::STOP | ServiceAccess::DELETE;
let service = service_manager.open_service("ping_service", service_access)?;
// The service will be marked for deletion as long as this function call succeeds.
// However, it will not be deleted from the database until it is stopped and all open handles to it are closed.
service.delete()?;
// Our handle to it is not closed yet. So we can still query it.
if service.query_status()?.current_state != ServiceState::Stopped {
// If the service cannot be stopped, it will be deleted when the system restarts.
service.stop()?;
}
// Explicitly close our open handle to the service. This is automatically called when `service` goes out of scope.
drop(service);
// Win32 API does not give us a way to wait for service deletion.
// To check if the service is deleted from the database, we have to poll it ourselves.
let start = Instant::now();
let timeout = Duration::from_secs(5);
while start.elapsed() < timeout {
if let Err(windows_service::Error::Winapi(e)) =
service_manager.open_service("ping_service", ServiceAccess::QUERY_STATUS)
{
if e.raw_os_error() == Some(ERROR_SERVICE_DOES_NOT_EXIST as i32) {
println!("ping_service is deleted.");
return Ok(());
}
}
sleep(Duration::from_secs(1));
}
println!("ping_service is marked for deletion.");
Ok(())
}examples/service_failure_actions.rs (line 44)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
fn main() -> windows_service::Result<()> {
use std::ffi::OsString;
use std::time::Duration;
use windows_service::{
service::{
ServiceAccess, ServiceAction, ServiceActionType, ServiceErrorControl,
ServiceFailureActions, ServiceFailureResetPeriod, ServiceInfo, ServiceStartType,
ServiceType,
},
service_manager::{ServiceManager, ServiceManagerAccess},
};
const SERVICE_NAME: &str = "service_failure_actions_example";
let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
let service_binary_path = ::std::env::current_exe()
.unwrap()
.with_file_name("service_failure_actions.exe");
let service_info = ServiceInfo {
name: OsString::from(SERVICE_NAME),
display_name: OsString::from("Service Failure Actions Example"),
service_type: ServiceType::OWN_PROCESS,
start_type: ServiceStartType::OnDemand,
error_control: ServiceErrorControl::Normal,
executable_path: service_binary_path,
launch_arguments: vec![],
dependencies: vec![],
account_name: None, // run as System
account_password: None,
};
let service_access = ServiceAccess::QUERY_CONFIG
| ServiceAccess::CHANGE_CONFIG
| ServiceAccess::START
| ServiceAccess::DELETE;
println!("Create or open the service {}", SERVICE_NAME);
let service = service_manager
.create_service(&service_info, service_access)
.or(service_manager.open_service(SERVICE_NAME, service_access))?;
let actions = vec![
ServiceAction {
action_type: ServiceActionType::Restart,
delay: Duration::from_secs(5),
},
ServiceAction {
action_type: ServiceActionType::RunCommand,
delay: Duration::from_secs(10),
},
ServiceAction {
action_type: ServiceActionType::None,
delay: Duration::default(),
},
];
println!("Update failure actions");
let failure_actions = ServiceFailureActions {
reset_period: ServiceFailureResetPeriod::After(Duration::from_secs(86400 * 2)),
reboot_msg: None,
command: Some(OsString::from("ping 127.0.0.1")),
actions: Some(actions),
};
service.update_failure_actions(failure_actions)?;
println!("Query failure actions");
let updated_failure_actions = service.get_failure_actions()?;
println!("{:#?}", updated_failure_actions);
println!("Enable failure actions on non-crash failures");
service.set_failure_actions_on_non_crash_failures(true)?;
println!("Query failure actions on non-crash failures enabled");
let failure_actions_flag = service.get_failure_actions_on_non_crash_failures()?;
println!(
"Failure actions on non-crash failures enabled: {}",
failure_actions_flag
);
println!("Delete the service {}", SERVICE_NAME);
service.delete()?;
Ok(())
}sourcepub fn service_name_from_display_name(
&self,
display_name: impl AsRef<OsStr>
) -> Result<OsString>
pub fn service_name_from_display_name( &self, display_name: impl AsRef<OsStr> ) -> Result<OsString>
Return the service name given a service display name.
Arguments
name- A service display name.
Example
use windows_service::service_manager::{ServiceManager, ServiceManagerAccess};
let manager = ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::CONNECT)?;
let my_service_name = manager.service_name_from_display_name("My Service Display Name")?;