#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;
}
Je suis en train de gérer l'événement généré par bleWatcher lorsqu'une connexion est reçue, lorsque je tente de créer mon rappel, je reçois l'erreur, Erreur C2664 « HRESULT Microsoft :: WRL :: DelegateTraits :: CheckReturn (HRESULT) ': ne peut pas convertir l' argument 1 de 'std :: _ non forcé' à 'HRESULT'erreur en essayant de créer le rappel
utilisateur steno916 semble avoir compris comment gérer cela, mais je ne peux pas comprendre ce que il a fait de son code fourni.
Vous avez juste besoin de définir l'argument de OnConnectionReceived être des pointeurs vers des interfaces (IBluetoothLEAdvertisementWatcher * et * IBluetoothLEAdvertisementReceivedEventArgs), et non des classes. –
Vous êtes mon héros monsieur, je n'ai aucune idée de comment je ne pouvais pas comprendre cela, mais je vous remercie beaucoup de changer cela de commentaire à répondre et votre prime sera accordée. –
Eh bien, trop tard! Hans a en quelque sorte détruit la générosité avec la réponse qu'il a faite sous wiki communautaire ... –