Crates
windows-rs vs windows-sys
- windows
- High-level typed bindings. WinRT types, COM support, method-style APIs. Recommended for most use cases.
- windows-sys
- Raw unsafe FFI bindings, no overhead. Function pointers, no COM. Best for minimal binary size.
- windows-rs
- The GitHub repository containing both crates. Often used to refer to the windows crate.
| Need | Use |
|---|---|
| WinRT APIs (UWP, modern Windows) | windows |
| Classic Win32 APIs | windows or windows-sys |
| Minimal binary, no COM | windows-sys |
| COM-based APIs (Shell, DirectX setup) | windows |
Cargo.toml
Add windows-rs to your project
[dependencies]
windows = { version = "0.57", features = [
# Win32 categories you need, e.g.:
"Win32_Foundation",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
"Win32_Storage_FileSystem",
] }
Enable only the feature flags you use. windows-rs has thousands of features — enabling unnecessary ones increases compile time. Each feature maps to a Win32 header namespace.
Example
Call a Win32 API from Rust
use windows::core::w;
use windows::Win32::Foundation::HWND;
use windows::Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_OK};
fn main() -> windows::core::Result<()> {
unsafe {
MessageBoxW(HWND::default(), w!("Hello!"), w!("Rust"), MB_OK);
}
Ok(())
}
FAQ
Win32 questions
What is windows-rs and who maintains it?
windows-rs is the official Rust bindings for Windows APIs maintained by Microsoft. It lives at github.com/microsoft/windows-rs. Microsoft generates the bindings directly from the Windows metadata (.winmd) files, so they are always complete and up to date.
Do I need unsafe to use windows-rs?
Most Win32 API calls require an unsafe block because the Win32 ABI predates Rust's safety model. WinRT APIs via windows-rs are often safe. The windows crate wraps many common patterns to minimise unsafe surface, but calling raw Win32 functions will always require unsafe.