Expand description
§windows-bindgen
Generate a focused set of Rust bindings from Windows metadata (.winmd). Bundled metadata covers
Win32 and WinRT; custom metadata can describe additional APIs. Applications and libraries can use
these bindings instead of depending on the umbrella windows or windows-sys crates.
§Generate bindings
Install rustfmt with rustup component add rustfmt to format generated code. Add the generator
as a build dependency and windows-core for the default typed bindings:
[dependencies]
windows-core = "0.101"
[build-dependencies]
windows-bindgen = "0.101"In build.rs:
use windows_bindgen::*;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
builder().filter("GetTickCount64").write();
}In src/main.rs:
mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
fn main() {
println!("{}", unsafe { bindings::GetTickCount64() });
}Run cargo run on Windows. Without .output(...), bindgen writes bindings.rs in Cargo’s
OUT_DIR. Use explicit output paths outside build scripts and when generating multiple files.
Bindgen does not emit Cargo dependency-tracking directives. The example watches only build.rs;
declare any other inputs your script reads, including metadata and filter files, with
cargo:rerun-if-changed.
§Commit bindings
For a published library, consider committing generated bindings so consumers do not build the generator. This also works for applications that prefer to regenerate only when their API selections change.
To switch the example above, add .output(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bindings.rs"))
to the builder in build.rs, replace the include! module with mod bindings;, and build once.
Then remove build.rs, move windows-bindgen to dev-dependencies, and add this regeneration test
to src/main.rs (or src/lib.rs in a library):
[dev-dependencies]
windows-bindgen = "0.101"#[test]
fn generate_bindings() {
windows_bindgen::builder()
.filter("GetTickCount64")
.output(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bindings.rs"))
.write();
}Keep the runtime dependencies and commit src/bindings.rs. Do not mix handwritten and generated
code. Run cargo test generate_bindings, then run Cargo again to compile any changed bindings.
CI should reject changes to committed generated files. Regenerate after changing filters, before
adding code that uses newly selected APIs.
The thread-pool sample demonstrates committed raw bindings regenerated by a test.
§Select APIs
Filters select native names, including any A or W suffix. Signature dependencies are included
automatically. Use .filters([...]) for several selections.
| Filter | Selects |
|---|---|
GetTickCount64 | A function |
Windows.Win32.GetTickCount64 | A qualified Win32 function |
Windows.Foundation | A namespace and its descendants |
ISequentialStream | An interface and its methods |
The bundled Win32 metadata uses Windows.Win32, not the umbrella crates’ Cargo feature names.
Select an interface explicitly when you need to call its methods. See
Bindgen::filter
for member selection and exclusions.
§Dependencies and native contracts
Default and .full() bindings use windows-core. For raw bindings, add .sys() and use
windows-link = "0.101" instead. The full and sys styles are mutually exclusive.
Add any support crates referenced by generated output, such as windows-future = "0.101"
for async APIs.
Minimal bindings expose raw Win32 function signatures; .full() adds higher-level wrappers.
Bindings do not initialize COM or remove native threading, lifetime, and ownership requirements.
Every unsafe call must satisfy the underlying API’s contract.
With no input, bindgen uses bundled Windows metadata. .input("component.winmd") replaces that
default; add .input_default() when the component also references Windows definitions.
Use windows-rdl to compile RDL or
windows-clang to start from headers.
Structs§
- Bindgen
- Builder for generating Windows API bindings.