尝试创建回调时出错

时间:2017-02-13 17:49:04

标签: c++ windows-runtime bluetooth-lowenergy

#include "stdafx.h"

#include <iostream>
#include <Windows.Foundation.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>
#include <wrl\event.h>
#include <stdio.h>
#include <../winrt/windows.devices.bluetooth.h>
#include <../winrt/windows.devices.bluetooth.advertisement.h>
#include <memory>
#include <functional>

using namespace std;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Devices::Bluetooth::Advertisement;
using namespace ABI::Windows::UI::Input;


// Prints an error string for the provided source code line and HRESULT
// value and returns the HRESULT value as an int.
int PrintError(unsigned int line, HRESULT hr)
{
    wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr);
    return hr;
}
struct Test {
    Test() {}
    Test(int i) {}

    HRESULT OnConnectionReceived(BluetoothLEAdvertisementWatcher* watcher, BluetoothLEAdvertisementReceivedEventArgs* args) {
        MessageBox(0, L"connected", L"MessageBox caption", MB_OK);
        return S_OK;
    }
};

EventRegistrationToken *watcherToken;


int main()
{
    watcherToken = new EventRegistrationToken();

    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }

    // Get the activation factory for the IBluetoothLEAdvertisementWatcherFactory interface.
    ComPtr<IBluetoothLEAdvertisementWatcherFactory> bleAdvWatcherFactory;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementWatcher).Get(), &bleAdvWatcherFactory);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }


    ComPtr<IBluetoothLEAdvertisementWatcher> bleWatcher;
    ComPtr<IBluetoothLEAdvertisementFilter> bleFilter;


    Wrappers::HStringReference class_id_filter2(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementFilter);
    hr = RoActivateInstance(class_id_filter2.Get(), reinterpret_cast<IInspectable**>(bleFilter.GetAddressOf()));
    hr = bleAdvWatcherFactory->Create(bleFilter.Get(), &bleWatcher);



    if (bleWatcher == NULL)
    {
        cout << "bleWatcher is NULL, err is " << hex << hr;
    }
    else
    {
        bleWatcher->Start();
        Test test;
        //Problem is here
        ComPtr<ITypedEventHandler<BluetoothLEAdvertisementWatcher*, BluetoothLEAdvertisementReceivedEventArgs*>> handler;
        handler = Callback<ITypedEventHandler<BluetoothLEAdvertisementWatcher*, BluetoothLEAdvertisementReceivedEventArgs*> > 
            (std::bind(
                &Test::OnConnectionReceived,
                &test,
                placeholders::_1,
                placeholders::_2
            ));

    hr = bleWatcher->add_Received(handler.Get(), watcherToken);
        while (1) {

            Sleep(1000);
        }
    }


    return 0;
}

我正在尝试处理接收连接时bleWatcher生成的事件,当我尝试创建回调时收到错误, 错误C2664'HRESULT Microsoft :: WRL :: DelegateTraits :: CheckReturn(HRESULT)':无法将参数1从'std :: _ Unforced'转换为'HRESULT'

https://social.msdn.microsoft.com/Forums/Lync/en-US/e321cb3c-462a-4b16-b7e4-febdb3d0c7d6/windows-10-pairing-a-ble-device-from-code?forum=wdk&prof=required

用户steno916似乎已经弄清楚如何处理这个问题,但我无法理解他从提供的代码中做了什么。

1 个答案:

答案 0 :(得分:3)

  

注意:编译类模板成员函数&#39; HRESULT Microsoft :: WRL :: Details :: InvokeHelper :: Invoke(ABI :: Windows :: Devices :: Bluetooth :: Advertisement :: IBluetoothLEAdvertisementWatcher *,ABI: :Windows :: Devices :: Bluetooth :: Advertisement :: IBluetoothLEAdvertisementReceivedEventArgs *)&#39;

模板编译错误消息可能很麻烦。这个肯定是母亲,问题与HRESULT无关。在“输出”窗口中查看完整的错误消息非常重要。这是消息的一部分,它提供了帮助您诊断出错的基本提示。

尽管如此,即便如此,你仍然可以看一小时或一天,但仍然看不到它。有时将它编辑下来很有用,用短的名称替换很长的名称以减少噪音水平。可能会揭示基本细节,请注意它如何将接口指针传递给Invoke()。因此回调必须同样在其签名中使用接口指针。添加两个I:

HRESULT OnConnectionReceived(IBluetoothLEAdvertisementWatcher* watcher,
                             IBluetoothLEAdvertisementReceivedEventArgs* args) {
    // etc...
}