35 releases (breaking)

0.28.0 Nov 17, 2021
0.26.0 Nov 10, 2021
0.18.0 Jul 25, 2021
0.7.0 Mar 29, 2021
0.0.0 Jan 15, 2019

#191 in Windows APIs

Download history 7190/week @ 2021-08-09 7737/week @ 2021-08-16 8185/week @ 2021-08-23 8892/week @ 2021-08-30 10764/week @ 2021-09-06 9728/week @ 2021-09-13 10809/week @ 2021-09-20 11538/week @ 2021-09-27 11137/week @ 2021-10-04 13206/week @ 2021-10-11 13511/week @ 2021-10-18 14277/week @ 2021-10-25 14775/week @ 2021-11-01 17589/week @ 2021-11-08 21793/week @ 2021-11-15 19974/week @ 2021-11-22

52,325 downloads per month
Used in 268 crates (174 directly)

MIT/Apache

220MB
3.5M SLoC

crates.io docs.rs build

Rust for Windows

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.

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

[dependencies.windows]
version = "0.28.0"
features = [
    "alloc",
    "Data_Xml_Dom",
    "Win32_Foundation",
    "Win32_Security",
    "Win32_System_Threading",
    "Win32_UI_WindowsAndMessaging",
]

Make use of any Windows APIs as needed.

use windows::{
    core::*, Data::Xml::Dom::*, Win32::Foundation::*, Win32::System::Threading::*,
    Win32::UI::WindowsAndMessaging::*,
};

fn main() -> 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(())
}

windows-sys

The windows-sys crate is a zero-overhead fallback for the most demanding situations and primarily where the absolute best compile time is essential. It only includes function declarations (externs), structs, and constants. No convenience helpers, traits, or wrappers are provided.

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

[dependencies.windows-sys]
version = "0.28.0"
features = [
    "Win32_Foundation",
    "Win32_Security",
    "Win32_System_Threading",
    "Win32_UI_WindowsAndMessaging",
]

Make use of any Windows APIs as needed.

use windows_sys::{Win32::Foundation::*, Win32::System::Threading::*, Win32::UI::WindowsAndMessaging::*};

fn main() {
    unsafe {
        let event = CreateEventW(std::ptr::null_mut(), 1, 0, std::ptr::null_mut());
        SetEvent(event);
        WaitForSingleObject(event, 0);
        CloseHandle(event);

        MessageBoxA(0, b"Text\0".as_ptr() as _, b"Caption\0".as_ptr() as _, MB_OK);
    }
}

Dependencies

~0–2.4MB