首页 > 解决方案 > 在 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_opHandler

欢迎任何想法。

提前致谢。

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();
            }
        });
}

标签: c++asynchronouswindows-runtimebluetooth-lowenergyiasyncoperation

解决方案


我让它工作:

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.


推荐阅读