gpt4 book ai didi

c++ - 如何在 "native c++"环境中使用 WinRT IAsyncOperation* 对象

转载 作者:搜寻专家 更新时间:2023-10-31 01:14:22 28 4
gpt4 key购买 nike

我有应该移植到 Windows 8 Metro (WinRT) 的 C/C++ SDK 库。库主要独立于操作系统,但它包含一些使用操作系统提供的 API 与硬件交互的模块。

在将其移植到 WinRT 时,我决定尽可能多地尝试使用 WRL 而不是 C++/CX。所以现在我可以创建和使用大部分所需的 WinRT 对象。但是在使用 WinRT 提供的异步对象时,我遇到了绝对的障碍。

例如,我使用以下代码来枚举硬件设备:

// create interface to "static" members of DeviceInformation class
ComPtr<IDeviceInformationStatics> DeviceInformationStatics;
HRESULT hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Enumeration_DeviceInformation).Get(), &DeviceInformationStatics);

ComPtr<IAsyncOperation<DeviceInformationCollection*>> operation;
hr = DeviceInformationStatics->FindAllAsyncDeviceClass( DeviceClass_All, &operation);

此时我有有效的 IAsyncOperation 指针。我认为它可以这样使用:

task<ComPtr<DeviceInformationCollection*>> tsk(operation);

但我失败了,因为接受 IAsyncOperation 的 task<> 构造函数是在 ppltasks.h 中的“#if defined(__cplusplus_winrt)”下声明的,而这又取决于/ZW 编译器选项。

在这种情况下我应该如何使用 IAsyncOperation 对象?其实我只需要等待操作完成即可。

最佳答案

以防其他人在 C++/WinRT 中寻找这个:

    auto asyncOp = someFunctionReturningIAsyncOperation();

asyncOp.Completed([](auto &&result, auto && status) {
// do whatever with result and status
});

关于c++ - 如何在 "native c++"环境中使用 WinRT IAsyncOperation* 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11418456/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com