0

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?

Rohit
  • 1
  • 1
  • Make sure you pump COM messages (GetMessage/DispatchMessage), something similar to what's demonstrated here: https://stackoverflow.com/a/59974072/403671 – Simon Mourier Jun 10 '21 at 12:06
  • Thanks. It helps but it is for console mode. I want to run this program normally and check whether the events are getting triggered or not but not in console mode – Rohit Jun 10 '21 at 12:22
  • This was just a sample. Console mode or not you must pump messages. – Simon Mourier Jun 10 '21 at 12:24
  • But that is a console sample. I don't have much knowledge about pump messages in a legacy code so that event gets triggered. If you have some examples of non console mode. can you share? It will help as I am learning this stuffs – Rohit Jun 10 '21 at 12:29
  • Just copy the GetMessage/DispatchMessage loop from the sample code, or google for "windows message pump" – Simon Mourier Jun 10 '21 at 12:44
  • What's `CoNetworkEventHandler` and how, if at all, is it related to `EventHandler`? – Igor Tandetnik Jun 11 '21 at 15:26
  • Both are same its a mistake that I have kept different names. – Rohit Jun 14 '21 at 03:09

0 Answers0