Skip to main content

Crate windows_core

Crate windows_core 

Expand description

§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:

APIUse
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.

Macros§

h
A literal HSTRING, length-prefixed wide string with a trailing null terminator.
implement_decl
Declares a Rust type as the COM implementer of one or more interfaces, without using the #[implement] proc-macro.
interface_decl
Declares a COM interface inheriting from IUnknown, without using the #[interface] proc-macro.
link
Defines an external function to import.
s
A literal UTF-8 string with a trailing null terminator.
w
A literal UTF-16 wide string with a trailing null terminator.

Structs§

AgileReference
A type representing an agile reference to a COM/WinRT object.
Array
A WinRT array stores elements contiguously in a heap-allocated buffer.
BOOL
A 32-bit Windows Boolean value.
BSTR
A length-prefixed wide string.
ComObject
A counted pointer to a type that implements COM interfaces.
Error
A Windows error code with optional COM error information.
Event
A type that you can use to declare and implement an event of a specified delegate type.
EventRevoker
Revokes an event registration when dropped.
GUID
A globally unique identifier (GUID).
HRESULT
An error code value returned by most COM functions.
HSTRING
An (HSTRING) is a reference-counted and immutable UTF-16 string type.
HStringBuilder
Builder for preallocating immutable HSTRING values.
IInspectable
The base interface for WinRT classes, interfaces, and boxed values.
IUnknown
Base interface for all COM interfaces.
InRef
A borrowed ABI-compatible input parameter.
InterfaceRef
A borrowed COM interface pointer with the same representation as I.
NTSTATUS
An error or status code value returned by some operating system functions.
OutRef
A borrowed ABI-compatible out parameter.
Owned
A wrapper to provide ownership for resources to automatically drop via the resource’s Free trait.
PCSTR
A pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters.
PCWSTR
A pointer to a constant null-terminated string of 16-bit Unicode characters.
PSTR
A pointer to a null-terminated string of 8-bit Windows (ANSI) characters.
PWSTR
A pointer to a null-terminated string of 16-bit Unicode characters.
RPC_STATUS
An error or status code value returned by some operating system functions.
StaApartment
Keeps the calling thread’s COM single-threaded apartment initialized.
StaticComObject
Stores a COM object in static memory.
WIN32_ERROR
An error or status code value returned by some operating system functions.
Weak
Weak holds a non-owning reference to an object.

Traits§

AsImpl
A trait for retrieving the implementation behind a COM or WinRT interface.
ComObjectInner
Identifies types that can be placed in ComObject.
ComObjectInterface
Describes the COM interfaces implemented by a specific COM object.
Free
Custom code to free a resource.
Interface
Provides low-level access to a COM interface vtable.
OutParam
Converts values to Windows API output parameters.
Param
Converts values to Windows API input parameters.

Functions§

create_instance
Creates an instance of an in-process COM class and returns the requested interface.
factory
Attempts to load the factory object for the given WinRT class. This can be used to access COM interfaces implemented on a Windows Runtime class factory.
init_mta
Initializes COM for apartment-agnostic code and keeps the multithreaded apartment alive for the rest of the process.
init_sta
Initializes the calling thread as a COM single-threaded apartment (STA).

Type Aliases§

Ref
The parameter type for a generic type T used in _Impl trait method signatures.
Result
A specialized Result type that provides Windows error information.

Attribute Macros§

implement
Implements one or more COM interfaces for a struct or enum.
interface
Defines a COM interface to call or implement.