winrt::Windows::Foundation::IUnknown 结构(C++/WinRT)

Every C++/WinRT runtime class (whether a Windows or a third party runtime class) derives from winrt::Windows::Foundation::IUnknown. It represents the COM IUnknown interface, and it provides facilities such as querying for a different interface, abi functions, and comparison operators.

Syntax

struct IUnknown

Requirements

支持的最低 SDK: Windows SDK 版本 10.0.17134.0(Windows 10 版本 1803)

Namespace: winrt

Header: %WindowsSdkDir%Include<WindowsTargetPlatformVersion>\cppwinrt\winrt\base.h (included by default)

Constructors

Constructor Description
IUnknown::IUnknown constructor Initializes a new instance of the IUnknown struct, optionally with a copy or move of the input data.

Member functions

Function Description
IUnknown::as function 请求接口,如果不支持该接口,则会引发该接口。
IUnknown::try_as function 请求接口,如果不支持该接口,则生成 null 接口。
IUnknown::try_as_with_reason function 请求接口,如果不支持该接口,请报告原因。

Member operators

Operator Description
IUnknown::operator bool Checks whether or not the IUnknown object is referencing an interface.
IUnknown::operator= (赋值运算符) Assigns a value to the IUnknown object.

Free functions

Function Description
attach_abi function Attaches an IUnknown object to a raw pointer that owns a reference to its target; an additional reference is not added.
copy_from_abi function Copies to an IUnknown object from another pointer. 递减当前引用的任何接口或对象的引用计数,复制原始指针参数,并开始管理它指向的接口或对象的生存期。
copy_to_abi function Copies to another pointer from an IUnknown object. 递增当前引用的任何接口或对象的引用计数,并将该接口或对象的内存地址复制到参数中。
detach_abi function Detaches from the raw IUnknown interface without decrementing the reference count, perhaps to return it to a caller.
get_abi function Returns the underlying raw IUnknown interface pointer should you need to pass it to a function.
get_unknown function A helper function that returns the address of the underlying raw IUnknown interface of an object of a projected type.
put_abi function Returns the address of the underlying raw IUnknown interface pointer as a pointer to a pointer to void; this function helps you call methods (such as COM methods) that return references as out parameters via a pointer to a pointer to void.
swap function Swaps the contents of the two IUnknown parameters so that they point at one another's target.

Free operators

Function Description
operator!= (不等运算符) 返回一个值,该值指示这两个参数是否引用不同的目标。
运算符< (小于运算符) 返回一个值,该值指示第一个参数的目标是否早于内存中的第二个参数的目标。
operator<=(小于或等于运算符) 返回一个值,该值指示第一个参数的目标是在内存中早于第二个参数的目标,还是在与第二个参数相同的位置发生。
operator== (相等运算符) 返回一个值,该值指示这两个参数是否引用同一接口和/或对象。
运算符> (greater-than 运算符) 返回一个值,该值指示第一个参数的目标在内存中是否晚于第二个参数的目标。
operator>=(大于或等于运算符) 返回一个值,该值指示第一个参数的目标在内存中是晚于第二个参数的目标,还是在与第二个参数相同的位置发生。

IUnknown::IUnknown constructor

Initializes a new instance of the IUnknown struct, optionally with a copy or move of the input data.

Syntax

IUnknown() noexcept;
IUnknown(std::nullptr_t) noexcept;
IUnknown(winrt::Windows::Foundation::IUnknown const& other) noexcept;
IUnknown(winrt::Windows::Foundation::IUnknown&& other) noexcept;

Parameters

other Another IUnknown that initializes the IUnknown object.

IUnknown::as function

IUnknown.. 请求指定的接口 如果接口不受支持,则引发。 如果希望支持接口,请使用此方法。

Syntax

template <typename To> auto as() const;
template <typename To> void as(To& to) const;

Template parameters

typename To 描述所请求接口的类型。 此类型可以是 C++/WinRT 接口名称或 C++/WinRT 运行时类名。

auto对于返回重载,类型也可以是经典 COM 接口。

void对于返回重载,类型也可以是com_ptr<I>I经典 COM 接口。

Parameters

to 对用于接收所请求接口的值的引用。

Return value

auto返回重载以C++/WinRT 接口或运行时类名称的形式To返回请求的接口,或者以经典 To COM 接口的形式com_ptr<To>返回请求的接口。

如果为 IUnknown ,则auto返回重载返回null,而void返回重载将设置为 tonullnull

如果不是 IUnknown 接口null ,但无法获取接口,则该方法将引发。

IUnknown::try_as function

IUnknown.. 请求指定的接口 如果接口不受支持,则生成 null

Syntax

template <typename To> auto try_as() const noexcept;
template <typename To> bool try_as(To& to) const noexcept;

Template parameters

typename To 描述所请求接口的类型。 此类型可以是 C++/WinRT 接口名称或 C++/WinRT 运行时类名。

auto对于返回重载,类型也可以是经典 COM 接口。

bool对于返回重载,类型也可以是com_ptr<I>I经典 COM 接口。

Parameters

to 对用于接收所请求接口的值的引用。

Return value

auto返回重载以C++/WinRT 接口或运行时类名称的形式To返回请求的接口,或者以经典 To COM 接口的形式com_ptr<To>返回请求的接口。

如果或无法获取接口,则auto返回重载将null返回,并且bool返回重载将设置为 tonullnullIUnknown

bool如果返回to的值不是,并且返回的重载为nullnull重载,则false返回true重载。

IUnknown::try_as_with_reason function

返回请求的接口(如果受支持的话)并报告不支持该接口的原因。

Syntax

template <typename To> auto try_as_with_reason(winrt::hresult& reason) const noexcept;

Template parameters

typename To 描述所请求接口的类型。 此类型可以是C++/WinRT 接口名称、C++/WinRT 运行时类名或经典 COM 接口。

Parameters

reason winrt::hresult接收描述查询结果的项。

Return value

返回请求的接口,其形式To为C++/WinRT 接口或运行时类名称,或者以经典 To COM 接口的形式com_ptr<To>返回请求的接口。

IUnknown如果或null无法获取接口,则该方法返回 null

Remarks

从 C++/WinRT 版本 2.0.250303.1 开始,可以使用此方法。

如果为 ,IUnknownnull则该方法返回null并设置reasonS_OK

如果不是IUnknownnull,则该方法返回查询的结果,并reason接收winrt::hresult查询生成的结果。

IUnknown::operator bool

Checks whether or not the IUnknown object is referencing an interface. If the IUnknown object is not referencing an interface, then it is logically null; otherwise it is logically not null.

Syntax

explicit operator bool() const noexcept;

Return value

true if the IUnknown object is referencing an interface (logically not null), otherwise false (logically null).

IUnknown::operator= (赋值运算符)

Assigns a value to the IUnknown object.

Syntax

winrt::Windows::Foundation::IUnknown& operator=(winrt::Windows::Foundation::IUnknown const& other) noexcept;
winrt::Windows::Foundation::IUnknown& operator=(winrt::Windows::Foundation::IUnknown&& other) noexcept;
winrt::Windows::Foundation::IUnknown& operator=(std::nullptr_t) noexcept;

Parameters

other An IUnknown value to assign to the IUnknown object, either by copy or by move.

Return value

A reference to the IUnknown object.

attach_abi function

Attaches an IUnknown object to a raw pointer that owns a reference to its target; an additional reference is not added. 如果需要,可以使用此函数合并引用。

Syntax

void attach_abi(winrt::Windows::Foundation::IUnknown& object, void* value) noexcept;

Parameters

object An IUnknown value to operate on.

value 拥有对其目标的引用的原始指针。

copy_from_abi function

Copies to an IUnknown object from another pointer. 递减当前引用的任何接口或对象的引用计数,复制原始指针参数,并开始管理它指向的接口或对象的生存期。

Syntax

void copy_from_abi(winrt::Windows::Foundation::IUnknown& object, void* value) noexcept;

Parameters

object An IUnknown value to operate on.

value A raw pointer to a target whose lifetime should be managed by the IUnknown object.

copy_to_abi function

Copies to another pointer from an IUnknown object. 递增当前引用的任何接口或对象的引用计数,并将该接口或对象的内存地址复制到参数中。 This function lets you hand out a reference to the same interface without calling QueryInterface.

Syntax

void copy_to_abi(winrt::Windows::Foundation::IUnknown const& object, void*& value) noexcept;

Parameters

object An IUnknown value to operate on.

value A raw pointer reference; via which to copy the pointer to the IUnknown object's target.

detach_abi function

Detaches an IUnknown object from its raw IUnknown interface without decrementing the reference count, perhaps to return it to a caller.

Syntax

void* detach_abi(winrt::Windows::Foundation::IUnknown& object) noexcept;
void* detach_abi(winrt::Windows::Foundation::IUnknown&& object) noexcept;

Parameters

object An IUnknown value to operate on.

Return value

A pointer to the raw IUnknown interface referenced by the IUnknown object.

get_abi function

Returns the underlying raw IUnknown interface pointer should you need to pass it to a function. You may call AddRef, Release, or QueryInterface on the returned pointer.

Syntax

void* get_abi(winrt::Windows::Foundation::IUnknown const& object) noexcept;

Parameters

object An IUnknown value to operate on.

Return value

A pointer to the raw IUnknown interface referenced by the IUnknown object.

operator!= (不等运算符)

返回一个值,该值指示这两个参数是否引用不同的目标。

Syntax

bool operator!=(winrt::Windows::Foundation::IUnknown const& left, winrt::Windows::Foundation::IUnknown const& right) noexcept;

Parameters

left right An IUnknown value whose target's memory address to compare with that of the other parameter.

Return value

true 如果两个参数指向不同的 target,则 false.

运算符< (小于运算符)

返回一个值,该值指示第一个参数的目标是否早于内存中的第二个参数的目标。

Syntax

bool operator<(winrt::Windows::Foundation::IUnknown const& left, winrt::Windows::Foundation::IUnknown const& right) noexcept;

Parameters

left right An IUnknown value whose target's memory address to compare with that of the other parameter.

Return value

true 如果第一个参数的目标的内存地址小于第二个参数的内存地址,否则 false.

operator<=(小于或等于运算符)

返回一个值,该值指示第一个参数的目标是在内存中早于第二个参数的目标,还是在与第二个参数相同的位置发生。

Syntax

bool operator<=(winrt::Windows::Foundation::IUnknown const& left, winrt::Windows::Foundation::IUnknown const& right) noexcept;

Parameters

left right An IUnknown value whose target's memory address to compare with that of the other parameter.

Return value

true 如果第一个参数的目标的内存地址小于或等于第二个参数的内存地址,否则 false.

operator== (相等运算符)

返回一个值,该值指示这两个参数是否引用同一接口和/或对象。

Syntax

bool operator==(winrt::Windows::Foundation::IUnknown const& left, winrt::Windows::Foundation::IUnknown const& right) noexcept;

Parameters

left right An IUnknown value whose target's memory address to compare with that of the other parameter.

Return value

true 如果两个参数指向同一个目标,则为 false

运算符> (greater-than 运算符)

返回一个值,该值指示第一个参数的目标在内存中是否晚于第二个参数的目标。

Syntax

bool operator>(winrt::Windows::Foundation::IUnknown const& left, winrt::Windows::Foundation::IUnknown const& right) noexcept;

Parameters

left right An IUnknown value whose target's memory address to compare with that of the other parameter.

Return value

true 如果第一个参数的目标的内存地址大于第二个参数的内存地址,否则 false.

operator>=(大于或等于运算符)

返回一个值,该值指示第一个参数的目标在内存中是晚于第二个参数的目标,还是在与第二个参数相同的位置发生。

Syntax

bool operator>=(winrt::Windows::Foundation::IUnknown const& left, winrt::Windows::Foundation::IUnknown const& right) noexcept;

Parameters

left right An IUnknown value whose target's memory address to compare with that of the other parameter.

Return value

true 如果第一个参数的目标的内存地址大于或等于第二个参数的内存地址,否则 false.

put_abi function

Returns the address of the underlying raw IUnknown interface pointer as a pointer to a pointer to void; this function helps you call methods (such as COM methods) that return references as out parameters via a pointer to a pointer to void.

Syntax

void** put_abi(winrt::Windows::Foundation::IUnknown& object) noexcept;

Parameters

object An IUnknown value to operate on.

Return value

The address of the underlying raw IUnknown interface pointer.

swap function

Swaps the contents of the two IUnknown parameters so that they point at one another's target.

Syntax

void swap(winrt::Windows::Foundation::IUnknown& left, winrt::Windows::Foundation::IUnknown& right) noexcept;

Parameters

left right An IUnknown value whose pointer to mutually swap with that of the other parameter.

See also