c++ - 在 C++ WinRT 中无法获取异步操作结果(Windows::Foundation::IAsyncOperation 接口)
问题描述
我有点绝望地试图获得带有Windows::Foundation::IAsyncOperation
接口的 winrt 异步方法的结果。该项目是启用了 winRT 扩展的 Visual Studio Community 2017 c++ 项目。我试过使用std::future
/co_await
函数和任务方法,但调用GetResults()
总是生成“在意外时间调用”异常。
使用以下代码,我没有得到异常但GetResults()
返回nullptr
。我也尝试在代码中声明async_op
为 ashared_ptr
auto async_op = std::make_shared<Windows::Foundation::IAsyncOperation<Windows::Devices::Bluetooth::BluetoothLEDevice^> ^>
并访问它,但我得到了相同的结果。*async_op
Handler
欢迎任何想法。
提前致谢。
void BleEnumeration::PerformConnectDevice(std::string* bleDevId)
{
std::wstring bleDevId_w_str = std::wstring((*bleDevId).begin(), (*bleDevId).end());
const wchar_t* bleDevId_w_char = bleDevId_w_str.c_str();
Platform::String^ bleDevId_refStr = ref new Platform::String(bleDevId_w_char);
auto async_op = Windows::Devices::Bluetooth::BluetoothLEDevice::FromIdAsync(bleDevId_refStr);
async_op->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<Windows::Devices::Bluetooth::BluetoothLEDevice^>(
[async_op] (Windows::Foundation::IAsyncOperation< Windows::Devices::Bluetooth::BluetoothLEDevice^ >^ operation, Windows::Foundation::AsyncStatus status)
{
if (async_op->Status == Windows::Foundation::AsyncStatus::Completed)
{
auto bleDevTest = async_op->GetResults();
}
});
}
解决方案
我让它工作:
Windows::Devices::Bluetooth::BluetoothLEDevice^ BleEnumeration::getBleDevice(Windows::Foundation::IAsyncOperation<Windows::Devices::Bluetooth::BluetoothLEDevice^>^ async)
{
if ((async)->Status != Windows::Foundation::AsyncStatus::Completed)
{
HANDLE signal = CreateEvent(nullptr, true, false, nullptr);
(async)->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<Windows::Devices::Bluetooth::BluetoothLEDevice^>(
[&signal](Windows::Foundation::IAsyncOperation< Windows::Devices::Bluetooth::BluetoothLEDevice^ >^ operation, Windows::Foundation::AsyncStatus status)
{
SetEvent(signal);
});
WaitForSingleObject(signal, INFINITE);
}
Windows::Devices::Bluetooth::BluetoothLEDevice^ device = async->GetResults();
return device;
}
bool BleEnumeration::PerformConnectDevice(std::string* bleDevId)
{
bool success = false;
Windows::Devices::Bluetooth::BluetoothLEDevice^ bleDevTest = nullptr;
std::wstring bleDevId_w_str = std::wstring((*bleDevId).begin(), (*bleDevId).end());
const wchar_t* bleDevId_w_char = bleDevId_w_str.c_str();
Platform::String^ bleDevId_refStr = ref new Platform::String(bleDevId_w_char);
Windows::Foundation::IAsyncOperation<Windows::Devices::Bluetooth::BluetoothLEDevice^>^ asyncOp = Windows::Devices::Bluetooth::BluetoothLEDevice::FromIdAsync(bleDevId_refStr);
bleDevTest = getBleDevice(asyncOp);
if (bleDevTest != nullptr)
{
success = true;
}
return success;
}
而且,GetResults() 给出 nullptr 的原因是:在 appxmanifest 中启用功能
必须在 Visual Studio 项目 Package.appxmanifest 文件中启用所有需要的功能,在我的例子中:“蓝牙”。我注意到我收到了错误:onecoreuap\drivers\wdm\bluetooth\user\winrt\device\bluetoothledevice.cpp(728)\Windows.Devices.Bluetooth.dll!00007FFC1601AE2F: (caller: 00007FFC1602081A) Exception(1) tid(212c) 80070005 Access is denied.
推荐阅读
- machine-learning - 试图为问题做一个搜索引擎
- java - 在 android 中使用 String 正文发送 POST 请求
- request - soapui wsdl 空请求
- c - 获取()函数抛出异常?
- scala - 将scala Json解析为数据框
- triggers - 顶点测试类
- unit-testing - 使用数据库访问在 Symfony 4 中测试服务的正确方法
- reactive-programming - 如何从 spring webflux 将专有流转换为 Flux
- php - 在没有卷的情况下“连接”两个 Docker 容器的最佳方式
- python - 如何在列表中的python中查询SQLite 3