-1
networkStatusCallback = new NetworkStatusChangedEventHandler(OnNetworkStatusChange);
    if (!registeredNetworkStatusNotif)
    {
        NetworkInformation.NetworkStatusChanged += networkStatusCallback;
        registeredNetworkStatusNotif = true;
    }

I'm getting the error NetworkInformation.NetworkStatusChanged += networkStatusCallback

Priyanka
  • 1
  • 3
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Also please learn how to create a [mre]. Lastly please learn how to [edit] your questions to improve them. – Some programmer dude Sep 13 '21 at 06:31
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 19 '21 at 03:59

1 Answers1

1

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;
}
selbie
  • 100,020
  • 15
  • 103
  • 173