13 breaking releases

0.52.0 Nov 15, 2023
0.48.0 Mar 31, 2023
0.45.0 Jan 21, 2023
0.42.0 Sep 27, 2022
0.28.0 Nov 17, 2021

#7 in Windows APIs

Download history 1875412/week @ 2024-01-02 1953765/week @ 2024-01-09 2041423/week @ 2024-01-16 2025959/week @ 2024-01-23 2174226/week @ 2024-01-30 2152062/week @ 2024-02-06 2082050/week @ 2024-02-13 2276303/week @ 2024-02-20 2310453/week @ 2024-02-27 2174282/week @ 2024-03-05 2129727/week @ 2024-03-12 2212357/week @ 2024-03-19 2192080/week @ 2024-03-26 2300508/week @ 2024-04-02 2304594/week @ 2024-04-09 1958946/week @ 2024-04-16

9,151,777 downloads per month
Used in 48,060 crates (349 directly)

MIT/Apache

22MB
385K SLoC

Rust for Windows

The windows and windows-sys crates let 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.52"
features = [
    "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(h!("<html>hello world</html>"))?;

    let root = doc.DocumentElement()?;
    assert!(root.NodeName()? == "html");
    assert!(root.InnerText()? == "hello world");

    unsafe {
        let event = CreateEventW(None, true, false, None)?;
        SetEvent(event)?;
        WaitForSingleObject(event, 0);
        CloseHandle(event)?;

        MessageBoxA(None, s!("Ansi"), s!("Caption"), MB_OK);
        MessageBoxW(None, w!("Wide"), w!("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.52"
features = [
    "Win32_Foundation",
    "Win32_Security",
    "Win32_System_Threading",
    "Win32_UI_WindowsAndMessaging",
]

Make use of any Windows APIs as needed:

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

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

        MessageBoxA(0, s!("Ansi"), s!("Caption"), MB_OK);
        MessageBoxW(0, w!("Wide"), w!("Caption"), MB_OK);
    }
}

Dependencies