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.
CodePudding user response:
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([=](...) // 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