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
[]
= "0.101"
use ;
let name: &HSTRING = h!;
assert_eq!;
assert!;
assert!;
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.