gpt4 book ai didi

c++ - 尝试创建回调时出错

转载 作者:搜寻专家 更新时间:2023-10-31 01:32:56 26 4
gpt4 key购买 nike

#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 似乎已经想出了如何处理这个问题,但我无法从他提供的代码中理解他做了什么。

最佳答案

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无关。查看“输出”窗口以阅读完整的错误消息非常重要。这是消息的一部分,提供重要提示以帮助您诊断问题所在。

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

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

关于c++ - 尝试创建回调时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42210305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com