在C ++ / WinRT中等效于Platform :: IBoxArray

时间:2019-03-01 15:52:18

标签: windows-runtime c++-winrt

我目前正在将UWP应用程序从C ++ / CX移植到C ++ / WinRT。我遇到了safe_cast<Platform::IBoxArray<byte>^>(data)类型为data的{​​{1}}。

我知道Windows::Foundation::IInspectable ^safe_cast方法表示,并且我知道WinRT /中有用于装箱(as<T>)和取消装箱(winrt::box_value)的函数。 C ++。

但是,我需要知道winrt::unbox_value的等效对象才能执行强制转换(QueryInterface)。根据{{​​3}},Platform::IBoxArrayIBoxArray的C ++ / CX等效物,但没有Windows::Foundation::IReferenceArray ...

针对nackground的更新:我要实现的目标是从其相机中检索HoloLens附加到每个Media Foundation样本的视图转换。我的代码基于https://docs.microsoft.com/de-de/cpp/cppcx/platform-iboxarray-interface?view=vs-2017,除了最后一步,我实际上可以正常工作。问题位于这段代码周围:

winrt::Windows::Foundation::IReferenceArray

2 个答案:

答案 0 :(得分:0)

我不敢相信这确实有效,但是我使用https://docs.microsoft.com/de-de/windows/uwp/cpp-and-winrt-apis/interop-winrt-cx的信息,提出了以下解决方案:

通过/ ZW启用“使用Windows运行时扩展”并使用以下转换:

auto abi = reinterpret_cast<Platform::Object ^>(winrt::get_abi(userData));
auto userBytes = safe_cast<Platform::IBoxArray<byte> ^>(abi)->Value;
viewTransform = *reinterpret_cast<float4x4 *>(userBytes->Data);

不幸的是,该解决方案具有生成的缺点

  

警告C4447:在没有线程模型的情况下发现了“主要”签名。考虑使用'int main(Platform :: Array ^ args)'。

但是现在,我可以忍受...

答案 1 :(得分:0)

我还在努力将一些代码从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标志。