尝试创建回调时出错

error trying to create callback

本文关键字:出错 回调 创建      更新时间:2023-10-16
#include "stdafx.h"
#include <iostream>
#include <Windows.Foundation.h>
#include <wrlwrapperscorewrappers.h>
#include <wrlclient.h>
#include <wrlevent.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%Xn", 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=所需

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

注意:编译类模板成员函数'HRESULT Microsoft::WRL::Details::InvokeHelper::Invoke

模板编译错误消息可能很痛苦。这一个当然是母亲,问题与HRESULT无关。在"输出"窗口中查看完整的错误消息非常重要。这是消息中提供重要提示的部分,可帮助您诊断问题所在。

尽管如此,即使在那时,你也可以看一个小时或一天,仍然看不到它。有时,把它编辑下来是有用的,用短名字代替很长的名字,以降低噪音水平。可能会揭示关键细节,请注意它是如何将接口指针传递给Invoke()的。因此回调也必须在其签名中使用接口指针。添加两个I:

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