I have tried multiple examples to such that events get triggered whenever it is raised. Using AFXConnectionAdvise getting appropriate results. But I do not want to use MFC So I am using IConnectionPoint::Advise() but the events are not getting triggered. I cannot understand why it is not getting triggered.
CNLMHelper::CNLMHelper(void) : m_pNLM(NULL), pCpc(NULL),pConnectionPoint(NULL),m_pSink(NULL), m_pUnkSink(NULL),m_dwCookie(0)
{
if (SUCCEEDED(CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, __uuidof(INetworkListManager), (LPVOID*)&m_pNLM)))
{
if (SUCCEEDED(m_pNLM->QueryInterface(IID_IConnectionPointContainer, (void**)&pCpc)))
{
CComPtr<IConnectionPoint> pConnectionPoint;
if (SUCCEEDED(pCpc->FindConnectionPoint(IID_INetworkEvents, &pConnectionPoint)))
{
m_pSink = new EventHandler();
if (SUCCEEDED(m_pSink->QueryInterface(IID_IUnknown, (void**)&m_pUnkSink)))
{
pConnectionPoint->Advise(m_pUnkSink, &m_dwCookie);
}
}
}
}
}
CNLMHelper:: ~CNLMHelper(void)
{
m_pConnectionPoint->Unadvise(m_dwCookie);
m_pSink->Release();
}
Event Handle function code is here:
STDMETHODIMP EventHandler::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) EventHandler::AddRef()
{
m_lRefCnt++;
return m_lRefCnt;
}
STDMETHODIMP_(ULONG) EventHandler::Release()
{
m_lRefCnt--;
if(m_lRefCnt == 0)
{
delete this;
return (0);
}
return m_lRefCnt;
//return 0;
}
STDMETHODIMP CoNetworkEventHandler :: ConnectivityChanged( NLM_CONNECTIVITY NewConnectivity)
{
return S_OK;
}
Is there anything wrong here because the use pConnectionPoint->Advise(m_pUnkSink, &m_dwCookie) is correct as per my knowledge and it is succeeding also as the return value of Advise is coming 0 which means it is succeeding then why the events are not getting triggered?