1

I'm working on the WinUI3 desktop application in C++. I was checking how we can get an event when the network to which system is connected to changes. I came across NetworkInformation.NetworkStatusChanged event. But I was not able to find any example of its implementation in C++.

NetworkStatusChangedEventHandler says it has one parameter which is IInspectable object.

i tried

static void NetworkStatusChange(winrt::Windows::Foundation::IInspectable const& sender);

But it gave this error

*\Generated Files\winrt\Windows.Networking.Connectivity.h(2213,81): error C2297: '.*': not valid as right operand has type 'const M'

Can you please help me with help me with how to implement NetworkStatusChanged event in WinUI3 C++ desktop application correctly.

Thank you.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Harshith
  • 181
  • 2
  • 9

1 Answers1

1

This is how you can do it with C++/WinRT:

add this into the pch.h:

#include <winrt/Windows.Networking.Connectivity.h>

add this somewhere in your code:

// you can instead add a "using namespace Windows::Networking::Connectivity;"
// and use NetworkInformation directly if you prefer
Windows::Networking::Connectivity::NetworkInformation info{};
info.NetworkStatusChanged([=](auto&&...) // sender is not super interesting in this type of event so I've not declared it
{
    // do your stuff here
    MessageBox(nullptr, L"Something Changed!", L"Network", 0);
});

If you prefer the "raw" C/C++ way, there's an example here: How to detect network change events asynchronously using c++ WinRT

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • 4
    Do not use `[&]` in asynchronous callbacks. By the time the callback occurs, the referenced objects have probably been destroyed. – Raymond Chen Jan 10 '23 at 17:37
  • Thank you this was very helpful, can you please explain the parameters [=] and (...) used in NetworkStatusChanged() function. – Harshith Jan 11 '23 at 06:04
  • The `(...)` should be `(auto&&...)`. Otherwise, you run afoul of [**expr.call**](https://timsong-cpp.github.io/cppwp/expr.call#12): (variadic function parameters): "Passing a potentially-evaluated argument of a scoped enumeration type or of a class type ([class]) having an eligible non-trivial copy constructor, an eligible non-trivial move constructor, or a non-trivial destructor ([special]), with no corresponding parameter, is **conditionally-supported with implementation-defined semantics**." Some implementations (e.g. clang) raise a compile-time and/or runtime error, which are both valid. – Raymond Chen Jan 11 '23 at 20:05
  • @RaymondChen - Thanks again. Strange I didn't even see the warning (https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4840) in Output/ErrorList windows. (c++ lambda are so complicated vs C# ones...) – Simon Mourier Jan 11 '23 at 20:15
  • 1
    @Harshith - [=] means in lambda code you capture all used variables (if you want to use info for example) by copy. (auto&&...) means you don't want to identify/use the parameters but still want the system to compile. https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp – Simon Mourier Jan 11 '23 at 20:16