The windows
crate lets you call any Windows API past, present, and future using code generated on the fly directly from the metadata describing the API and right into your Rust package where you can call them as if they were just another Rust module. The Rust language projection follows in the tradition established by C++/WinRT of building language projections for Windows using standard languages and compilers, providing a natural and idiomatic way for Rust developers to call Windows APIs.
Watch the Getting Started video! Microsoft Docs also has content on developing with Rust on Windows. Check out the FAQ for answers to frequently asked questions.
Looking for specific APIs? The default set of APIs are searchable and documented here.
Start by adding the following to your Cargo.toml file:
[dependencies]
windows = "0.20.1"
[build-dependencies]
windows = "0.20.1"
This will allow Cargo to download, build, and cache Windows support as a package. Next, specify which types you need inside of a build.rs
build script and the windows
crate will generate the necessary bindings:
fn main() {
windows::build! {
Windows::Data::Xml::Dom::*,
Windows::Win32::Foundation::CloseHandle,
Windows::Win32::System::Threading::{CreateEventW, SetEvent, WaitForSingleObject},
Windows::Win32::UI::WindowsAndMessaging::MessageBoxA,
};
}
Finally, make use of any Windows APIs as needed.
windows::include_bindings!();
use Windows::Data::Xml::Dom::*;
use Windows::Win32::Foundation::*;
use Windows::Win32::System::Threading::*;
use Windows::Win32::UI::WindowsAndMessaging::*;
fn main() -> windows::Result<()> {
let doc = XmlDocument::new()?;
doc.LoadXml("<html>hello world</html>")?;
let root = doc.DocumentElement()?;
assert!(root.NodeName()? == "html");
assert!(root.InnerText()? == "hello world");
unsafe {
let event = CreateEventW(std::ptr::null_mut(), true, false, None);
SetEvent(event).ok()?;
WaitForSingleObject(event, 0);
CloseHandle(event).ok()?;
MessageBoxA(None, "Text", "Caption", MB_OK);
}
Ok(())
}
To reduce build time, use a dedicated bindings
crate. This will allow Cargo to cache the results and build your project far more quickly. More examples can be found here. Robert Mikhayelyan's Minesweeper is also a great example.
A more in-depth getting started guide can also be found here.