0

I am attempting to utilize the C++/WinRT code provided in the sample of LampArray, cppwinrt to create a C++ DLL. However due to the tight coupling of this sample with the UI, I am not able to use below API independently within my DLL code, without incorporating UI Code.

m_deviceAddedRevoker = m_deviceWatcher.Added(winrt::auto_revoke, { get_weak(), &Scenario1_Basics::Watcher_Added});
m_deviceRemovedRevoker = m_deviceWatcher.Removed(winrt::auto_revoke, { get_weak(), &Scenario1_Basics::Watcher_Removed });

Could anyone please guide me on whether there's a method to directly invoke the API in my C++ DLL code without relying on the UWP UI portion? Note: I am trying to create a C++ DLL to access the lamp devices attached to system like lights/LEDs, etc.

9
  • The device watcher doesn't depend on a UI. If you don't want to incorporate the UI code, don't. I believe the calling thread needs to have a dispatcher queue to bring the event delegates to life (see CreateDispatcherQueueController() for more information). Commented Apr 23, 2024 at 10:21
  • @sunil - DeviceWatcher works on any thread (events are also coming also from any thread, not UI threads). Stupid question but have you called Start? Otherwise please post a reproducing sample How to create a Minimal, Reproducible Example Commented Apr 23, 2024 at 17:16
  • Will using Windows Runtime components meet your needs? You can refer to this blog use Windows Runtime Components in Desktop Apps. Commented Apr 24, 2024 at 8:10
  • Hi, @SimonMourier, Yes DeviceWatcher doesn't depend on the UI but when I call using LampArray::GetDeviceSelector() as below then getting the compilation error (get_weak: identifier not found ) while i have added the required header file which is in base.h, Yes I have called Start. m_deviceWatcher=DeviceInformation::CreateWatcher(LampArray::GetDeviceSelector()); m_deviceAddedRevoker = m_deviceWatcher.Added(winrt::auto_revoke, { get_weak(), Watcher_Added }); m_deviceWatcher.Start(); Commented Apr 24, 2024 at 18:28
  • @sunil - Please provide a minimal reproducible sample (by code or somewhere on internet), not code in comments Commented Apr 25, 2024 at 4:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.