Skip to content

Latest commit

Β 

History

History
62 lines (46 loc) Β· 1.81 KB

File metadata and controls

62 lines (46 loc) Β· 1.81 KB

windows-interface

The #[interface] macro for declaring COM interfaces in Rust.

windows-interface provides the #[interface] attribute macro used to declare a COM interface as a Rust trait. It is re-exported by windows-core, which is the dependency you normally take β€” the generated code refers to windows-core for IUnknown, the vtable layout, and QueryInterface support.

Annotate an unsafe trait that derives from IUnknown with the interface's GUID. The macro generates the vtable and the plumbing needed to call and implement the interface. Pair it with #[implement] to supply a Rust implementation.

use windows_core::*;

// Declare a custom COM interface with its GUID.
#[interface("7e75ffe0-2f8c-4040-953e-b1f83a48f77b")]
unsafe trait IValue: IUnknown {
    unsafe fn value(&self) -> i32;
}

// Implement it with `#[implement]`.
#[implement(IValue)]
struct Value {
    value: i32,
}

impl IValue_Impl for Value_Impl {
    unsafe fn value(&self) -> i32 {
        self.value
    }
}

fn main() {
    let object: IValue = Value { value: 42 }.into();
    assert_eq!(unsafe { object.value() }, 42);
}

Internal documentation

The remainder of this page covers how the crate is built and maintained. It is for contributors and is not needed to use windows-interface.

How it's built

A proc-macro crate. The crate-level docs live inline in src/lib.rs. Uses syn/quote/proc-macro2.

Testing

Run cargo test -p windows-interface; see also the workspace test crates.