正如 Microsoft 文档中提到的,有两种创建 COM 对象的方法:
- 实现该对象的模块可能会提供专门设计用于创建该对象的实例的函数。
- 或者,COM 提供了一个名为
CoCreateInstance
的通用创建函数。
在 direct3D 中,您可以使用第一种方法来创建对象,就像使用 D3D11CreateDevice()
但是你如何通过第二种方法来做到这一点。也许你需要我没有找到的CLSID_D3D11CreateDevice
。是否有任何库可以包含,或者也许不可能通过第二种方式创建。
另外,我真的很想知道是否有办法查看 COM 对象以及它们实现的接口(interface)。
最佳答案
DirectX API 不是“真正的 COM”。这些 API 派生自用于引用计数的 IUnknown 和用于版本控制的多态接口(interface),但它们并不遵循 COM 对象的所有其他规则。 “子”对象的生命周期规则也进行了修改,并且 DirectX API 仅支持 COINIT_MULTITHREADED
。
这是一种非正式地称为“COM lite”或“nano-COM”的设计模式。
- 对象是使用 Win32“平面”工厂函数创建的
- 对象通常仅支持
IUnknown
、主接口(interface)和主接口(interface)的派生版本 - 接口(interface)上的方法可以返回
void
或HRESULT
- 并非所有方法都需要线程安全
- 它们不支持 COM 聚合
- 接口(interface)指针作为参数不支持客户端提供的实现
Direct2D/DirectWrite and Windows Imaging Component (WIC) support
CoCreateInstance
as did did the original XAudio2 for Windows up to version 2.7 and legacy XACT. Otherwise, "DirectX" components do not support registry-based or manifest-based creation.
此模式提供了稳定的“应用程序二进制接口(interface) (ABI)”,并很好地映射到纯虚拟类的 C++ 单继承。它提供了一种简单的方法来扩展新版本以添加方法以及生命周期管理的引用计数。生成的 API 通常与 IID_PPV_ARGS
宏以及 COM 智能指针(如 Microsoft::WRL::ComPtr)兼容。 , winrt::com_ptr ,或旧版 ATL CComPtr .
参见Microsoft Docs: Programming DirectX with COM
More recent versions of DirectX like DirectX 12 have adopted "strongly-typed bitmasks" which were not used in older versions. See this blog post.
While historically the DirectX "COM lite" APIs have provided C support through macros, the most recent guidance is to just support C++ and make use of C++11 features like strongly-typed enumerations (a.k.a. scoped enumerations). You'll see this reflected in the DXCore interfaces.
关于winapi - Direct3D 中的 COM 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64751273/