WinUI3 : Handle network change event in WinUI3 desktop with c++
See the question and my original answer on StackOverflowThis 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