Win32 guide

Win32 & WinRT with Rust — windows-rs guide for Windows

The windows crate (maintained by Microsoft) provides safe Rust bindings to all Win32 and WinRT APIs. Use windows for typed high-level access or windows-sys for raw unsafe bindings with no overhead.

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.
NeedUse
WinRT APIs (UWP, modern Windows)windows
Classic Win32 APIswindows or windows-sys
Minimal binary, no COMwindows-sys
COM-based APIs (Shell, DirectX setup)windows

Add windows-rs to your project

Cargo.toml
[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.

Call a Win32 API from Rust

src/main.rs — MessageBoxW
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(())
}

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.