Introduce the dedicated windows-services crate
#3599
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This update introduces the windows-services crate as a simple and safe way to implement a Windows service in Rust. I previously added a Windows service sample using the
windows-syscrate directly against the service control manager APIs (#3590), but using that approach is quite error-prone and involves a lot of unsafe code. Thewindows-servicescrate provides a super-superServicetype that lets you write a robust Windows service with minimal boilerplate and no unsafe code. Here's a very simple example:After using the service builder to indicate what service commands to support, the closure will be called with various commands as they are sent by the service control manager. That's all there is to it!
This implementation takes an opinionated approach to Windows service development in order to keep things as simple and safe as possible. For example, the service will always run in its own process. There are other ways to implement a service and even different kinds of services, such as a driver service, but this crate is focused on the most common case: a non-driver service that runs in its own process.
Here's a slightly more interesting example using the
windows-threadingcrate introduced in #3595 to manage a dedicated service thread:The service will print some useful testing information to the console if you run it directly: