windows-core 0.101.0

Core type support for COM and Windows
Documentation

windows-core

Shared COM and WinRT types, interface ownership, apartment initialization, and authoring macros. Focused Windows crates often re-export the types they need; depend on windows-core directly when your own code names them.

Get started

[dependencies]
windows-core = "0.101"
use windows_core::{h, Result, HRESULT, HSTRING};

let name: &HSTRING = h!("Windows.Foundation.Uri");
assert_eq!(name, &HSTRING::from("Windows.Foundation.Uri"));

fn check(code: HRESULT) -> Result<()> {
    code.ok()
}

assert!(check(HRESULT(0)).is_ok());
assert!(check(HRESULT(-2147467259)).is_err());

Initialize COM

Follow an existing UI framework's apartment model. For standalone code:

API Use
init_mta() Keep a process-wide MTA alive; uninitialized threads use it implicitly.
init_sta() Initialize an STA on the calling thread and retain its guard.

init_mta does not change a thread that has already joined another apartment. An STA guard must outlive its thread's COM objects. create_instance::<T>(&class_id) activates an in-process COM class; choose one compatible with the calling apartment.

Own and share interfaces

Interface values own reference-counted COM pointers. Clone to retain a reference, let Drop release it, and use Interface::cast to request another interface. Follow the interface's threading rules when sharing it between threads.

Raw-pointer conversions require a known ownership and lifetime contract. A Win32 handle is not a COM interface and may need a separate close operation.

The default proc-macros feature supplies #[interface] and #[implement] for declaring and implementing interfaces. See windows-interface and windows-implement.