尝试创建回调时出错
error trying to create callback
#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'
用户 steno916 似乎已经想出了如何处理这个问题,但我无法从他提供的代码中理解他做了什么。
note: while compiling class template member function 'HRESULT Microsoft::WRL::Details::InvokeHelper::Invoke(ABI::Windows::Devices::Bluetooth::Advertisement::IBluetoothLEAdvertisementWatcher *,ABI::Windows::Devices::Bluetooth::Advertisement::IBluetoothLEAdvertisementReceivedEventArgs *)'
模板编译错误消息可能会很痛苦。这个肯定是妈妈的,问题跟HRESULT无关。查看输出 window 以阅读完整的错误消息非常重要。这是消息的一部分,提供了帮助您诊断问题所在的基本提示。
尽管如此,即使你看了一个小时或一天,仍然看不到它。有时将其编辑下来很有用,将很长的名称替换为较短的名称以降低噪音水平。可能会揭示基本细节,请注意它如何将 接口指针 传递给 Invoke()。因此,回调必须在其签名中同样使用接口指针。添加两个 I:
HRESULT OnConnectionReceived(IBluetoothLEAdvertisementWatcher* watcher,
IBluetoothLEAdvertisementReceivedEventArgs* args) {
// etc...
}
#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'
用户 steno916 似乎已经想出了如何处理这个问题,但我无法从他提供的代码中理解他做了什么。
note: while compiling class template member function 'HRESULT Microsoft::WRL::Details::InvokeHelper::Invoke(ABI::Windows::Devices::Bluetooth::Advertisement::IBluetoothLEAdvertisementWatcher *,ABI::Windows::Devices::Bluetooth::Advertisement::IBluetoothLEAdvertisementReceivedEventArgs *)'
模板编译错误消息可能会很痛苦。这个肯定是妈妈的,问题跟HRESULT无关。查看输出 window 以阅读完整的错误消息非常重要。这是消息的一部分,提供了帮助您诊断问题所在的基本提示。
尽管如此,即使你看了一个小时或一天,仍然看不到它。有时将其编辑下来很有用,将很长的名称替换为较短的名称以降低噪音水平。可能会揭示基本细节,请注意它如何将 接口指针 传递给 Invoke()。因此,回调必须在其签名中同样使用接口指针。添加两个 I:
HRESULT OnConnectionReceived(IBluetoothLEAdvertisementWatcher* watcher,
IBluetoothLEAdvertisementReceivedEventArgs* args) {
// etc...
}