locked
adding a NetworkStatusChangedEventHandler RRS feed

  • Question

  • I have a standard C++ class and I want to get a member function called as the network status changes.  I started with the NetworkInformationSample project and tried to add an event handler as shown in the code sample below.

    // class declaration
    class WFReachabilityBroker : public WRL::RuntimeClass<  WRL::RuntimeClassFlags<  WRL::ClassicCom >, IUnknown> {
    ...
    void onNetworkStatusChange( Object^ sender );
    }

    // code to add status listener
    networkStatusCallback = ref new NetworkStatusChangedEventHandler( this, &WFReachabilityBroker::onNetworkStatusChange, CallbackContext::Same );

    // error from the line above
    Error 1 error C2664: 'Windows::Networking::Connectivity::NetworkStatusChangedEventHandler::NetworkStatusChangedEventHandler<WFReachabilityBroker*>(TFunctor,Platform::CallbackContext,bool)' : cannot convert parameter 2 from 'void (__thiscall WFReachabilityBroker::* )(Platform::Object ^)' to 'Platform::CallbackContext'

    I have tried a number of different incantations to try to get this to compile and can't quite find the right one.  My class is currently declared as such and cannot be change:

    My two choices are to get the handler constructor to compile, or to find another way to get my member function added as a handler.

    Regards, Guy

    Monday, May 14, 2012 9:09 PM

Answers

  • Hello,

    The delegate grammar is the same with CLI/C++, because the WFReachabilityBroker::onNetworkStatusChange is not the MainPage class function, so that you should new a WFReachabilityBroker, then pass the instance as the first parameter like this:

    WFReachabilityBroker^ wrf= ref new WFReachabilityBroker();

        //create handler for network status changed event
        networkStatusCallback = ref new NetworkStatusChangedEventHandler( wrf, &WFReachabilityBroker::onNetworkStatusChange, CallbackContext::Same);


    For more information, please check
    Delegates and Events
    http://msdn.microsoft.com/en-us/library/ms235237(v=vs.100).aspx
    Delegates (C++/CX)
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh755798(v=vs.110).aspx

    Best regards,
    Jesse

     


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Thursday, May 24, 2012 7:08 AM
    • Unmarked as answer by gnichola Friday, May 25, 2012 10:09 PM
    • Marked as answer by gnichola Friday, May 25, 2012 10:58 PM
    Thursday, May 17, 2012 11:42 AM

All replies

  •  

    Hello,

     

    Would you please show us the codes in WFReachabilityBroker::onNetworkStatusChange function?

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, May 15, 2012 9:07 AM
  • Why would the body of the function matter?  It seems like the class and method declaration and the declaration of the function would be the relevant pieces, but in any case here is the function body.


    Regards, Guy

    void WFReachabilityBroker::onNetworkStatusChange( Object^ sender )
    {
    	try
    	{        
    		// Display new Interent Connection profile information               
    		ConnectionProfile^ internetConnectionProfile = NetworkInformation::GetInternetConnectionProfile();
    		std::string connectionProfileInfo = getConnectionProfile(internetConnectionProfile);
    		ReachabilityTransitData* rtd = new ReachabilityTransitData( PlatformStringToUTF8( mUrl ), connectionProfileInfo );
    		WFLuaMessagingProcessor_scheduleFunctionInLuaState( mProcessor, networkChangeCallback, (void*) rtd, deleteTransitData );
    	}
    	catch (...)
    	{
    		// TODO handle this
    	}
    }
    


    Tuesday, May 15, 2012 1:40 PM
  • Hello,

    The delegate grammar is the same with CLI/C++, because the WFReachabilityBroker::onNetworkStatusChange is not the MainPage class function, so that you should new a WFReachabilityBroker, then pass the instance as the first parameter like this:

    WFReachabilityBroker^ wrf= ref new WFReachabilityBroker();

        //create handler for network status changed event
        networkStatusCallback = ref new NetworkStatusChangedEventHandler( wrf, &WFReachabilityBroker::onNetworkStatusChange, CallbackContext::Same);


    For more information, please check
    Delegates and Events
    http://msdn.microsoft.com/en-us/library/ms235237(v=vs.100).aspx
    Delegates (C++/CX)
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh755798(v=vs.110).aspx

    Best regards,
    Jesse

     


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Thursday, May 24, 2012 7:08 AM
    • Unmarked as answer by gnichola Friday, May 25, 2012 10:09 PM
    • Marked as answer by gnichola Friday, May 25, 2012 10:58 PM
    Thursday, May 17, 2012 11:42 AM