This will get you started. It's some code I cobbled together.
It doesn't do error checking. You should check the HRESULT or error code from each major function invoked below.
It doesn't show you how to implement INetworkStatusChangedEventHandler
. You have to implement an implementation of that class yourself using the standard COM principals.
You'll need to link with runtimeobjects.lib
for RoGetActivationFactory.
#include <windows.h>
#include <roapi.h>
#include <Windows.Networking.h>
#include <Windows.Networking.Connectivity.h>
using ABI::Windows::Networking::Connectivity::INetworkInformationStatics;
using ABI::Windows::Networking::Connectivity::INetworkStatusChangedEventHandler;
int main()
{
CoInitialize(nullptr);
HSTRING hstr = nullptr;
IActivationFactory* pFactory = nullptr;
INetworkInformationStatics* pStatics = nullptr;
EventRegistrationToken token = {};
const wchar_t* interfaceName = RuntimeClass_Windows_Networking_Connectivity_NetworkInformation;
::WindowsCreateString(interfaceName, (DWORD)(wcslen(interfaceName)), &hstr);
::RoGetActivationFactory(hstr, IID_IActivationFactory, (void**)&pFactory);
WindowsDeleteString(hstr);
INetworkStatusChangedEventHandler* pHandler = <ptr to com object you create that implements INetworkStatusChangedEventHandler>
pFactory->QueryInterface(&pStatics);
pStatics->add_NetworkStatusChanged(pHandler, &token);
return 0;
}