How can get IP Address change event in Qt.
-
wrote on 10 May 2013, 11:21 last edited by
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 followingCoNetworkEventHandlerr.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 -
wrote on 10 May 2013, 12:00 last edited by
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 -
wrote on 10 May 2013, 14:14 last edited by
-
wrote on 13 May 2013, 11:17 last edited by
Hi Tobias,
Thank you for your reply. But this is not working in my case, because i want to signal for IP address change.
-
wrote on 13 May 2013, 16:53 last edited by
Pardeep: I have not tried this, but I would expect an ip address change to happen only when a connection is set up.
-
wrote on 16 May 2013, 08:52 last edited by
Tobias: Yes you are right, ip is chnaged when new connection is set up. But still no event is coming when i changed network.
-
wrote on 16 May 2013, 15:43 last edited by
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.
-
wrote on 17 May 2013, 03:54 last edited by
I am changing ip by unpluging the LAN cable & by switching wifi...
1/8