如何使用WRL返回内置的winrt组件

How to return a build-in winrt component using WRL?

本文关键字:winrt 组件 内置 返回 何使用 WRL      更新时间:2023-10-16

当我使用WRL创建winrt组件时,问题是我只能使用ABI::Windows::xxx命名空间,而不能在WRL中使用Windows::UI::Xaml::Media::Imaging命名空间。

那么,如何创建一个内置的winrt组件作为返回值呢?

// idl
import "inspectable.idl";
import "Windows.Foundation.idl";
import "Windows.UI.Xaml.Media.Imaging.idl";
namespace Decoder
{
interface IPhotoDecoder;
runtimeclass PhotoDecoder;
interface IPhotoDecoder : IInspectable
{
HRESULT Decode([in] int width, [in] int height, [out, retval] Windows.UI.Xaml.Media.Imaging.BitmapImage **ppBitmapImage);
}
[version(COMPONENT_VERSION), activatable(COMPONENT_VERSION)]
runtimeclass PhotoDecoder
{
[default] interface IPhotoDecoder;
}
}
// cpp
using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace ABI::Windows::UI::Xaml::Media::Imaging;
namespace ABI
{
namespace Decoder
{
class PhotoDecoder: public RuntimeClass<IPhotoDecoder>
{
InspectableClass(L"Decoder.PhotoDecoder", BaseTrust)
public:
PhotoDecoder()
{
}
HRESULT __stdcall Decode(_In_ int width, _In_ int height, _Out_ IBitmapImage **ppBitmapImage)
{
// How to create Windows.UI.Xaml.Media.Imaging.BitmapImage without using Windows::UI::Xaml::Media::Imaging
}
};
ActivatableClass(PhotoDecoder);
}
}

有两组名称空间:

  • 根在全局命名空间中的那些(例如Windows::Foundation)
  • 根在ABI命名空间中的那些(例如ABI::Windows::Foundation)

每个的内容都"相同"。例如,Windows::Foundation::IUriRuntimeClass将相同的接口命名为ABI::Windows::Foundation::IUriRuntimeClass

那么,为什么有两组名称空间呢?根在全局名称空间中的名称空间保留供C++/CX使用:它在这些名称空间中生成运行时类的投影。当您使用WRL时,您将始终使用植根于ABI命名空间的命名空间(这些名称是"未投影"的名称,也就是说,它们正是ABI层中存在的名称)。

运行时类以两种方式之一实例化("激活")。如果类型是默认可构造的,则可以通过调用RoActivateInstance来默认构造它。如果一个类型声明了其他构造函数,则可以通过调用RoGetActivationFactory获取运行时类型的激活工厂来调用这些构造函数。例如,您可以默认构造BitmapImage,如下所示:

using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::UI::Xaml::Media::Imaging;
HStringReference classId(RuntimeClass_Windows_UI_Xaml_Media_Imaging_BitmapImage);
ComPtr<IInspectable> inspectable;
if (FAILED(RoActivateInstance(classId.Get(), inspectable.GetAddressOf())))
{
// Handle failure
}
ComPtr<IBitmapImage> bitmapImage;
if (FAILED(inspectable.As(&bitmapImage)))
{
// Handle failure
}

WRL还有一个有用的函数模板Windows::Foundation::ActivateInstance,它既调用RoActivateInstance,又对所需的目标接口执行QueryInterface

using namespace Windows::Foundation;
ComPtr<IBitmapImage> bitmapImage;
if (FAILED(ActivateInstance(classId.Get(), bitmapImage.GetAddressOf())))
{
// Handle failure
}