我目前正在将 UWP 应用程序从 C++/CX 移植到 C++/WinRT。我遇到了safe_cast<Platform::IBoxArray<byte>^>(data)
哪里data
类型为Windows::Foundation::IInspectable ^
.
我知道 safe_cast
由 as<T>
表示方法,我知道 WinRT/C++ 中有装箱 ( winrt::box_value
) 和拆箱 ( winrt::unbox_value
) 的函数。
但是,我需要知道 Platform::IBoxArray
的等价物为了执行转换(QueryInterface)。根据https://learn.microsoft.com/de-de/cpp/cppcx/platform-iboxarray-interface?view=vs-2017 , IBoxArray
是 C++/CX 等价于 Windows::Foundation::IReferenceArray
,但没有winrt::Windows::Foundation::IReferenceArray
...
nackground 更新:我想要实现的是从其相机检索 HoloLens 附加到每个媒体基础样本的 View 变换。我的代码基于 https://github.com/Microsoft/HoloLensForCV ,除了最后一步之外,我确实一切正常。问题就出在这一段代码上:
static const GUID MF_EXTENSION_VIEW_TRANSFORM = {
0x4e251fa4, 0x830f, 0x4770, 0x85, 0x9a, 0x4b, 0x8d, 0x99, 0xaa, 0x80, 0x9b
};
// ...
// In the event handler, which receives const winrt::Windows::Media::Capture::Frames::MediaFrameReader& sender:
auto frame = sender.TryAcquireLatestFrame();
// ...
if (frame.Properties().HasKey(MF_EXTENSION_VIEW_TRANSFORM)) {
auto /* IInspectable */ userData = frame.Properties().Lookup(MF_EXTENSION_VIEW_TRANSFORM);
// Now I would have to do the following:
// auto userBytes = safe_cast<Platform::IBoxArray<Byte> ^>(userData)->Value;
//viewTransform = *reinterpret_cast<float4x4 *>(userBytes.Data);
}
最佳答案
我还致力于将一些代码从 HoloLensForCV 移植到 C++/WinRT。我针对非常相似的情况提出了以下解决方案(但与您询问的代码行不完全相同):
auto user_data = source.Info().Properties().Lookup(c_MF_MT_USER_DATA); // type documented as 'array of bytes'
auto source_name = user_data.as<Windows::Foundation::IReferenceArray<std::uint8_t>>(); // Trial and error to get the right specialization of IReferenceArray
winrt::com_array<std::uint8_t> arr;
source_name.GetUInt8Array(arr);
winrt::hstring source_name_str{ reinterpret_cast<wchar_t*>(arr.data()) };
具体来说,您可以替换 safe_cast
与 .as<Windows::Foundation::IReferenceArray<std::uint8_t>
对于装箱的字节数组。然后,我怀疑做与我相同的转换(除了 float4x4* 而不是 wchar_t*)会对您有用。
上面的示例不需要/ZW 标志。
关于windows-runtime - C++/WinRT 中 Platform::IBoxArray 的等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948092/