尝试创建回调时出错
error trying to create callback
#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...
}
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 不带轮询的 SDL2 事件回调
- C++存储带有可变参数的回调
- 如何使用 Node-addon-API 实现 node-nan 回调
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- JNI 回调中出错
- 使用 boost::bind 进行订阅回调时出错
- 尝试创建回调时出错
- 在Python中使用回调时出错
- 'No Match' 使用 std::tr1::函数的成员回调函数出错