Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can get IP Address change event in Qt.
Forum Updated to NodeBB v4.3 + New Features

How can get IP Address change event in Qt.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    psbhardwaj09gmail.com
    wrote on 10 May 2013, 11:21 last edited by
    #1

    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

    Pardeep Sharma

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 10 May 2013, 12:00 last edited by
      #2

      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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on 10 May 2013, 14:14 last edited by
        #3

        http://qt-project.org/doc/qt-5.0/qtnetwork/qnetworksession.html#stateChanged might help.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          psbhardwaj09gmail.com
          wrote on 13 May 2013, 11:17 last edited by
          #4

          Hi Tobias,

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

          Pardeep Sharma

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on 13 May 2013, 16:53 last edited by
            #5

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

            1 Reply Last reply
            0
            • P Offline
              P Offline
              psbhardwaj09gmail.com
              wrote on 16 May 2013, 08:52 last edited by
              #6

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

              Pardeep Sharma

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on 16 May 2013, 15:43 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  psbhardwaj09gmail.com
                  wrote on 17 May 2013, 03:54 last edited by
                  #8

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

                  Pardeep Sharma

                  1 Reply Last reply
                  0

                  1/8

                  10 May 2013, 11:21

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved