14 breaking releases

0.59.0 Jul 30, 2024
0.52.0 Nov 15, 2023
0.48.0 Mar 31, 2023
0.42.0 Sep 27, 2022
0.28.0 Nov 17, 2021

#7 in Windows APIs

Download history 3358478/week @ 2024-08-23 3172480/week @ 2024-08-30 3441085/week @ 2024-09-06 3172149/week @ 2024-09-13 3501657/week @ 2024-09-20 3375135/week @ 2024-09-27 3721320/week @ 2024-10-04 3498207/week @ 2024-10-11 3677037/week @ 2024-10-18 3690832/week @ 2024-10-25 3712693/week @ 2024-11-01 3762741/week @ 2024-11-08 3774562/week @ 2024-11-15 3354261/week @ 2024-11-22 3461377/week @ 2024-11-29 3207243/week @ 2024-12-06

14,508,397 downloads per month
Used in 58,936 crates (502 directly)

MIT/Apache

17MB
287K 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.58"
features = [
    "Data_Xml_Dom",
    "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.59"
features = [
    "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 as _, s!("Ansi"), s!("Caption"), MB_OK);
        MessageBoxW(0 as _, w!("Wide"), w!("Caption"), MB_OK);
    }
}

Dependencies