Skip to content

Commit 3e3c87b

Browse files
authored
Update readme to include windows-targets and windows-bindgen (#2399)
1 parent 6672c6d commit 3e3c87b

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

crates/libs/bindgen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66
license = "MIT OR Apache-2.0"
77
description = "Code gen support for the windows and windows-sys crates"
88
repository = "https://github.com/microsoft/windows-rs"
9+
readme = "../../../docs/readme.md"
910

1011
[package.metadata.docs.rs]
1112
default-target = "x86_64-pc-windows-msvc"

crates/libs/bindgen/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*!
2+
Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
3+
*/
4+
15
mod classes;
26
mod com_methods;
37
mod constants;
@@ -13,13 +17,16 @@ mod iterators;
1317
mod method_names;
1418
mod structs;
1519
mod winrt_methods;
16-
pub use gen::*;
1720
use metadata::reader::*;
1821
use method_names::*;
1922
use std::collections::*;
2023
use std::fmt::Write;
2124
use tokens::*;
2225

26+
#[doc(hidden)]
27+
pub use gen::*;
28+
29+
#[doc(hidden)]
2330
pub fn namespace(gen: &Gen, tree: &Tree) -> String {
2431
let mut tokens = TokenStream::new();
2532

@@ -143,6 +150,7 @@ pub fn namespace(gen: &Gen, tree: &Tree) -> String {
143150
tokens.into_string()
144151
}
145152

153+
#[doc(hidden)]
146154
pub fn namespace_impl(gen: &Gen, tree: &Tree) -> String {
147155
let mut types = BTreeMap::<&str, TokenStream>::new();
148156

@@ -174,6 +182,7 @@ pub fn namespace_impl(gen: &Gen, tree: &Tree) -> String {
174182
tokens.into_string()
175183
}
176184

185+
/// Generates bindings for a specific component namespace.
177186
pub fn component(namespace: &str, files: &[File]) -> String {
178187
let reader = &Reader::new(files);
179188
let tree = reader.tree(namespace, &Default::default());
@@ -185,6 +194,7 @@ pub fn component(namespace: &str, files: &[File]) -> String {
185194
bindings
186195
}
187196

197+
/// Generates standalone bindings for Windows APIs.
188198
pub fn standalone(names: &[&str]) -> String {
189199
let files = &File::with_default(&[]).unwrap();
190200
let reader = &Reader::new(files);

crates/libs/targets/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2018"
77
license = "MIT OR Apache-2.0"
88
description = "Import libs for Windows"
99
repository = "https://github.com/microsoft/windows-rs"
10+
readme = "../../../docs/readme.md"
1011

1112
[target.i686-pc-windows-msvc.dependencies]
1213
windows_i686_msvc = { path = "../../targets/i686_msvc", version = "0.42.2" }

crates/libs/targets/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
/*!
2+
Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
3+
*/
4+
15
#![no_std]

docs/readme.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ features = [
2020
]
2121
```
2222

23-
Make use of any Windows APIs as needed.
23+
Make use of any Windows APIs as needed:
2424

2525
```rust,no_run
2626
use windows::{
@@ -67,7 +67,7 @@ features = [
6767
]
6868
```
6969

70-
Make use of any Windows APIs as needed.
70+
Make use of any Windows APIs as needed:
7171

7272
```rust,no_run
7373
use windows_sys::{
@@ -86,3 +86,46 @@ fn main() {
8686
}
8787
}
8888
```
89+
90+
## windows-bindgen
91+
92+
Even with a [choice between the windows and windows-sys crates](https://kennykerr.ca/rust-getting-started/windows-or-windows-sys.html), some developers may prefer to use completely standalone bindings. The [windows-bindgen](https://crates.io/crates/windows-bindgen) crate lets you generate entirely standalone bindings for Windows APIs with a single function call that you can run from a test to automate the generation of bindings. This can help to reduce your dependencies while continuing to provide a sustainable path forward for any future API requirements you might have, or just to refresh your bindings from time to time to pick up any bug fixes automatically from Microsoft.
93+
94+
Start by adding the following to your Cargo.toml file:
95+
96+
```toml
97+
[dependencies.windows-targets]
98+
version = "0.46.0"
99+
100+
[dev-dependencies.windows-bindgen]
101+
version = "0.46.0"
102+
```
103+
104+
The `windows-bindgen` crate is only needed for generating bindings and is thus a dev dependency only. The [windows-targets](https://crates.io/crates/windows-targets) crate is a dependency shared by the `windows` and `windows-sys` crates and only contains import libs for supported targets. This will ensure that you can link against any Windows API functions you may need.
105+
106+
Write a test to generate bindings as follows:
107+
108+
```rust,no_run
109+
#[test]
110+
fn gen_bindings() {
111+
let apis = [
112+
"Windows.Win32.System.SystemInformation.GetTickCount",
113+
];
114+
115+
let bindings = windows_bindgen::standalone(&apis);
116+
std::fs::write("src/bindings.rs", bindings).unwrap();
117+
}
118+
```
119+
120+
Make use of any Windows APIs as needed.
121+
122+
```rust,no_run,ignore
123+
mod bindings;
124+
use bindings::*;
125+
126+
fn main() {
127+
unsafe {
128+
println!("{}", GetTickCount());
129+
}
130+
}
131+
```

0 commit comments

Comments
 (0)