title: My BHO does not work properly. DocumentComplete does not trigger
body: <p>I am doing a BHO for Internet Explorer. The problem is only the SetSite is working. BUt the DocumentComplete does not been triggered. I need some help in pointing me what is I am doing wrong.</p>
  
  <p>Here is what I declared inside my header file:</p>
  
  <pre><code>class ATL_NO_VTABLE CStockBar : 
  public CComObjectRootEx&lt;CComSingleThreadModel&gt;,
  public CComCoClass&lt;CStockBar, &amp;CLSID_StockBar&gt;,
  public IDeskBand,
  public IObjectWithSite,
  public IObjectWithSiteImpl&lt;CStockBar&gt;,
  public IInputObject, 
  public IDispatchImpl&lt;IStockBar, &amp;IID_IStockBar, &amp;LIBID_MOTLEYFOOLLib&gt;,
  public IDispEventImpl&lt;1, CStockBar, &amp;DIID_DWebBrowserEvents2, &amp;LIBID_SHDocVw, 1, 1&gt;
  {
  public:
  CStockBar();
  
  DECLARE_REGISTRY_RESOURCEID(IDR_STOCKBAR)
  DECLARE_PROTECT_FINAL_CONSTRUCT()
  
  BEGIN_CATEGORY_MAP(CStockBar)
  //  IMPLEMENTED_CATEGORY(CATID_InfoBand)
  //  IMPLEMENTED_CATEGORY(CATID_CommBand)
  //  IMPLEMENTED_CATEGORY(CATID_DeskBand)
  END_CATEGORY_MAP()
  
  BEGIN_COM_MAP(CStockBar)
  COM_INTERFACE_ENTRY(IStockBar)
  //  COM_INTERFACE_ENTRY(IInputObject)
  COM_INTERFACE_ENTRY(IOleWindow)
  COM_INTERFACE_ENTRY_IID(IID_IDockingWindow, IDockingWindow)
  COM_INTERFACE_ENTRY(IObjectWithSite)
  COM_INTERFACE_ENTRY_IID(IID_IDeskBand, IDeskBand)
  COM_INTERFACE_ENTRY(IDispatch)
  END_COM_MAP()
  
  BEGIN_SINK_MAP(CStockBar)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
  END_SINK_MAP()
  </code></pre>
  
  <p>Here is my SetSite and DocumentComplete functions:</p>
  
  <pre><code>   STDMETHODIMP CStockBar::SetSite(IUnknown* pUnkSite)
     {
      //If a site is being held, release it.
  if(m_pSite)
  {
      m_ReflectWnd.GetToolBar().SetBrowser(NULL);
      m_pSite-&gt;Release();
      m_pSite = NULL;
  }
      if (pUnkSite != NULL)
     {
      // Cache the pointer to IWebBrowser2.
     HRESULT hr = pUnkSite-&gt;QueryInterface(IID_IWebBrowser2, (void**)&amp;m_spWebBrowser);
     if (SUCCEEDED(hr))
      {
          // Register to sink events from DWebBrowserEvents2.
          hr = DispEventAdvise(m_spWebBrowser);
          if (SUCCEEDED(hr))
          {
              m_fAdvised = TRUE;
          }
      }
  }
  else
  {
    if (m_fAdvised)
      {
          DispEventUnadvise(m_spWebBrowser);
          m_fAdvised = FALSE;
      }
      m_spWebBrowser.Release();
  }
  //If punkSite is not NULL, a new site is being set.
  if(pUnkSite)
  {
      //Get the parent window.
      IOleWindow  *pOleWindow = NULL;
  
      m_hWndParent = NULL;
  
      if(SUCCEEDED(pUnkSite-&gt;QueryInterface(IID_IOleWindow, (LPVOID*)&amp;pOleWindow)))
      {
          pOleWindow-&gt;GetWindow(&amp;m_hWndParent);
          pOleWindow-&gt;Release();
      }
  
      if(!::IsWindow(m_hWndParent))
          return E_FAIL;
  
      if(!RegisterAndCreateWindow())
          return E_FAIL;
  
      //Get and keep the IInputObjectSite pointer.
      if(FAILED(pUnkSite-&gt;QueryInterface(IID_IInputObjectSite, (LPVOID*)&amp;m_pSite)))
      {
          return E_FAIL;
      }  
  
      IWebBrowser2* s_pFrameWB = NULL;
      IOleCommandTarget* pCmdTarget = NULL;
      HRESULT hr = pUnkSite-&gt;QueryInterface(IID_IOleCommandTarget, (LPVOID*)&amp;pCmdTarget);
      if (SUCCEEDED(hr))
      {
          IServiceProvider* pSP;
          hr = pCmdTarget-&gt;QueryInterface(IID_IServiceProvider, (LPVOID*)&amp;pSP);
  
          pCmdTarget-&gt;Release();
  
          if (SUCCEEDED(hr))
          {
              hr = pSP-&gt;QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (LPVOID*)&amp;s_pFrameWB);
              pSP-&gt;Release();
              _ASSERT(s_pFrameWB);
              m_ReflectWnd.GetToolBar().SetBrowser(s_pFrameWB);
              s_pFrameWB-&gt;Release();
          }
      }
   }
  return S_OK;
     }
  
  
  
  
    void STDMETHODCALLTYPE CStockBar::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
   {
     // Retrieve the top-level window from the site.
     HWND hwnd;
     HRESULT hr = m_spWebBrowser-&gt;get_HWND((LONG_PTR*)&amp;hwnd);
     if (SUCCEEDED(hr))
    {
      // Output a message box when page is loaded.
      MessageBox(hwnd,"Hello World!","BHO", MB_OK);
    }
   }
  </code></pre>