23 releases (breaking)

new 0.18.0 Jul 25, 2021
0.16.0 Jul 7, 2021
0.7.0 Mar 29, 2021
0.0.1 May 22, 2020
0.0.0 Jan 15, 2019
Download history 1335/week @ 2021-04-08 1553/week @ 2021-04-15 2089/week @ 2021-04-22 4969/week @ 2021-04-29 5657/week @ 2021-05-06 5281/week @ 2021-05-13 5498/week @ 2021-05-20 4612/week @ 2021-05-27 5483/week @ 2021-06-03 5835/week @ 2021-06-10 6048/week @ 2021-06-17 7015/week @ 2021-06-24 6573/week @ 2021-07-01 7239/week @ 2021-07-08 6288/week @ 2021-07-15 6752/week @ 2021-07-22

24,745 downloads per month
Used in 145 crates (121 directly)

MIT/Apache

265KB
6K SLoC

crates.io docs.rs build

Rust for the Windows SDK

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.

Getting started

Start by adding the following to your Cargo.toml file:

[dependencies]
windows = "0.18.0"

[build-dependencies]
windows = "0.18.0"

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.

mod bindings {
    windows::include_bindings!();
}

use bindings::{
    Windows::Data::Xml::Dom::*,
    Windows::Win32::Foundation::CloseHandle,
    Windows::Win32::System::Threading::{CreateEventW, SetEvent, WaitForSingleObject},
    Windows::Win32::UI::WindowsAndMessaging::{MessageBoxA, MB_OK},
};

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 bindings crate rather than simply a module. This will allow Cargo to cache the results and build your project far more quickly.

There is an experimental documentation generator for the Windows API. The documentation is published here. This can be useful to figure out how the various Windows APIs map to Rust modules and which use paths you need to use from within the build macro.

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.

Dependencies

~0–1MB