]> git.proxmox.com Git - rustc.git/blame - vendor/windows-bindgen/readme.md
New upstream version 1.74.1+dfsg1
[rustc.git] / vendor / windows-bindgen / readme.md
CommitLineData
49aad941
FG
1## Rust for Windows
2
781aab86 3The [windows](https://crates.io/crates/windows) and [windows-sys](https://crates.io/crates/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](https://github.com/microsoft/windows-rs/tree/master/crates/libs/bindgen/default) 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](https://github.com/microsoft/cppwinrt) of building language projections for Windows using standard languages and compilers, providing a natural and idiomatic way for Rust developers to call Windows APIs.
49aad941
FG
4
5* [Getting started](https://kennykerr.ca/rust-getting-started/)
781aab86 6* [Samples](https://github.com/microsoft/windows-rs/tree/0.51.1/crates/samples)
49aad941
FG
7* [Releases](https://github.com/microsoft/windows-rs/releases)
8
9Start by adding the following to your Cargo.toml file:
10
11```toml
12[dependencies.windows]
781aab86 13version = "0.51"
49aad941
FG
14features = [
15 "Data_Xml_Dom",
16 "Win32_Foundation",
17 "Win32_Security",
18 "Win32_System_Threading",
19 "Win32_UI_WindowsAndMessaging",
20]
21```
22
23Make use of any Windows APIs as needed:
24
25```rust,no_run
26use windows::{
27 core::*, Data::Xml::Dom::*, Win32::Foundation::*, Win32::System::Threading::*,
28 Win32::UI::WindowsAndMessaging::*,
29};
30
31fn main() -> Result<()> {
32 let doc = XmlDocument::new()?;
33 doc.LoadXml(h!("<html>hello world</html>"))?;
34
35 let root = doc.DocumentElement()?;
36 assert!(root.NodeName()? == "html");
37 assert!(root.InnerText()? == "hello world");
38
39 unsafe {
40 let event = CreateEventW(None, true, false, None)?;
781aab86 41 SetEvent(event)?;
49aad941 42 WaitForSingleObject(event, 0);
781aab86 43 CloseHandle(event)?;
49aad941
FG
44
45 MessageBoxA(None, s!("Ansi"), s!("Caption"), MB_OK);
46 MessageBoxW(None, w!("Wide"), w!("Caption"), MB_OK);
47 }
48
49 Ok(())
50}
51```
52
53## windows-sys
54
55The `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.
56
57Start by adding the following to your Cargo.toml file:
58
59```toml
60[dependencies.windows-sys]
61version = "0.48"
62features = [
63 "Win32_Foundation",
64 "Win32_Security",
65 "Win32_System_Threading",
66 "Win32_UI_WindowsAndMessaging",
67]
68```
69
70Make use of any Windows APIs as needed:
71
72```rust,no_run
73use windows_sys::{
74 core::*, Win32::Foundation::*, Win32::System::Threading::*, Win32::UI::WindowsAndMessaging::*,
75};
76
77fn main() {
78 unsafe {
79 let event = CreateEventW(std::ptr::null(), 1, 0, std::ptr::null());
80 SetEvent(event);
81 WaitForSingleObject(event, 0);
82 CloseHandle(event);
83
84 MessageBoxA(0, s!("Ansi"), s!("Caption"), MB_OK);
85 MessageBoxW(0, w!("Wide"), w!("Caption"), MB_OK);
86 }
87}
88```