How can get IP Address change event in Qt.



  • Hi,

    I have an application in which i want to capture the ip address changes event.
    i have tried for this, but its not working. My code is as following

    CoNetworkEventHandlerr.h file
    @
    #include "netlistmgr.h"
    #include <netlistmgr.h>
    #include <QObject>

    class ApplicationManager;

    class CoNetworkEventHandler : public QObject,
    /* public INetworkListManagerEvents,/public INetworkEvents
    {
    Q_OBJECT
    public:
    CoNetworkEventHandler(ApplicationManager
    appManager);
    virtual ~ CoNetworkEventHandler(void);
    STDMETHODIMP ConnectivityChanged(NLM_CONNECTIVITY NewConnectivity);

    STDMETHODIMP QueryInterface(REFIID refIID, void** pIFace);
    
    STDMETHODIMP_(ULONG) AddRef();
    
    STDMETHODIMP_(ULONG) Release();
    
    STDMETHODIMP NetworkConnectivityChanged(GUID networkId, NLM_CONNECTIVITY newConnectivity);
    
    STDMETHODIMP NetworkAdded( GUID networkId);
    
    STDMETHODIMP /*STDMETHODCALLTYPE*/ NetworkDeleted( GUID networkId);
    
    STDMETHODIMP NetworkPropertyChanged(GUID networkId, NLM_NETWORK_PROPERTY_CHANGE flags);
    
    void isOnline(bool status);
    

    signals:
    void onlineStateChanged(bool);

    private:
    long m_lRefCnt;
    DWORD m_dwCookie;
    ApplicationManager *mpAppManager;

    };
    @

    CoNetworkEventHandler.cpp file

    @

    #include "CoNetworkEventHandler.h"
    #include <QDebug>

    CoNetworkEventHandler::CoNetworkEventHandler(ApplicationManager* appManager)
    : mpAppManager(appManager),m_lRefCnt(0), m_dwCookie(0)
    {
    }

    CoNetworkEventHandler::~CoNetworkEventHandler(void)
    {

    }

    STDMETHODIMP CoNetworkEventHandler::QueryInterface(REFIID refIID, void** pIFace)
    {
    pIFace = NULL;
    if(refIID == IID_IUnknown || refIID == __uuidof(INetworkListManagerEvents))
    {
    pIFace = (IUnknown)(INetworkListManagerEvents
    )(this);
    }
    if (pIFace == NULL)
    {
    return E_NOINTERFACE;
    }
    ((IUnknown
    )*pIFace)->AddRef();

    return S_OK;
    

    }

    STDMETHODIMP_(ULONG) CoNetworkEventHandler::AddRef()
    {
    m_lRefCnt++;
    return m_lRefCnt;
    }

    STDMETHODIMP_(ULONG) CoNetworkEventHandler::Release()
    {
    m_lRefCnt--;
    if(m_lRefCnt == 0)
    {
    delete this;
    return (0);
    }
    return m_lRefCnt;
    }

    STDMETHODIMP CoNetworkEventHandler :: ConnectivityChanged( NLM_CONNECTIVITY NewConnectivity)
    {
    if((NewConnectivity == NLM_CONNECTIVITY_DISCONNECTED))
    {
    isOnline(false);
    }
    else
    {
    isOnline(true);
    }
    qDebug()<< "Connectivity Changed:";
    return S_OK;
    }

    STDMETHODIMP CoNetworkEventHandler :: NetworkConnectivityChanged(GUID networkId, NLM_CONNECTIVITY NewConnectivity)
    {
    // if((NewConnectivity == NLM_CONNECTIVITY_DISCONNECTED))
    // {
    // isOnline(false);
    // }
    // else
    // {
    // isOnline(true);
    // }

     qDebug()<< "Network Connectivity Changed:";
     return S_OK;
    

    }

    STDMETHODIMP CoNetworkEventHandler :: NetworkPropertyChanged(GUID networkId, NLM_NETWORK_PROPERTY_CHANGE flags)
    {
    qDebug()<< "Network Property Changed:";
    return S_OK;
    }

    STDMETHODIMP CoNetworkEventHandler :: NetworkDeleted(GUID networkId)
    {
    qDebug()<< "Network Deleted:";
    return S_OK;
    }

    STDMETHODIMP CoNetworkEventHandler :: NetworkAdded( GUID networkId)
    {
    qDebug()<< "Network Added:";
    return S_OK;
    }

    void CoNetworkEventHandler :: isOnline(bool status)
    {
    if(!status)
    {
    VisualCom_Log_Fatal("CoNetworkEventHandler() isOnline false");
    onlineStateChanged(false);
    }
    else
    {
    VisualCom_Log_Trace("CoNetworkEventHandler() isOnline true");
    onlineStateChanged(true);
    }

    }

    @

    And i am calling it as

    @

    HRESULT hr = S_OK;

    if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
    {
        {
            INetworkListManager* pNLM;
            hr = CoCreateInstance(CLSID_NetworkListManager,
                NULL, CLSCTX_ALL, __uuidof(INetworkListManager), (LPVOID*)&pNLM);
            if (SUCCEEDED(hr))
            {
                VisualCom_Log_Debug("startNetworkEventHandler() CoCreateInstance SUCCEEDED");
                IConnectionPointContainer *pCPContainer = NULL;
                hr = pNLM->QueryInterface(IID_IConnectionPointContainer,
                                                        (void **)&pCPContainer);
                if (SUCCEEDED(hr))
                {
                    VisualCom_Log_Debug("startNetworkEventHandler() QueryInterface SUCCEEDED");
                    IConnectionPoint *pConnectPoint = NULL;
                    hr = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents,
                                                              &pConnectPoint);
                    if(SUCCEEDED(hr))
                    {
                       VisualCom_Log_Debug("startNetworkEventHandler() FindConnectionPoint SUCCEEDED");
                       DWORD Cookie = NULL;
                       if(mpNetEvent)
                       {
                           delete mpNetEvent;
                           mpNetEvent = NULL;
                       }
                       mpNetEvent = new CoNetworkEventHandler(this);
                       hr = pConnectPoint->Advise((IUnknown *)mpNetEvent, &Cookie);
                       if (SUCCEEDED(hr))
                       {
                           BOOL bRet;
                           MSG msg;
                           while((bRet = GetMessage(&msg, NULL, 0, 0 )) != 0)
                           {
                               if (bRet == -1)
                               {
                                   break;
                               }
    
                               TranslateMessage(&msg);
                               DispatchMessage(&msg);
                           }
                       }
    
                      // connect( mpNetEvent, SIGNAL( onlineStateChanged(bool)),
                       //        this, SLOT(onlineStateChanged(bool)));
                    }
                }
            }
        }
    }
    

    @

    Can anyone help me for this?

    Rgds,
    Pardeep Sharma



  • Hi,

    your code don't use Qt API but native ones.
    IMHO Qt have not this facility, you can try polling over network interfaces end verify if IP addresses are changed


  • Moderators



  • Hi Tobias,

    Thank you for your reply. But this is not working in my case, because i want to signal for IP address change.


  • Moderators

    Pardeep: I have not tried this, but I would expect an ip address change to happen only when a connection is set up.



  • Tobias: Yes you are right, ip is chnaged when new connection is set up. But still no event is coming when i changed network.


  • Moderators

    How do you change networks? By plugging your ethernet cable into a different network or by switching wifi networks?

    I would have expected both to be visible with the signal I pointed out.

    You might be out of luck if you just set a different IP yourself though.



  • I am changing ip by unpluging the LAN cable & by switching wifi...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.