title: Invalid window in DocumentComplete (DISPID_DOCUMENTCOMPLETE) in IE9 BHO
body: <p>I'm developing the BHO and I have a problems with events handling in <strong>IE9</strong>.
  I handle <strong>DocumentComplete</strong> (DISPID_DOCUMENTCOMPLETE), then I get <strong>IHTMLWindow2</strong> and its <strong>IHTMLLocation</strong> for future operations.</p>
  
  <p><strong>Code (simplified)</strong></p>
  
  <p>The BHO class uses</p>
  
  
  
  <pre class="lang-c++ prettyprint-override"><code>public IDispEventImpl&lt;1, CBHO, &amp;DIID_DWebBrowserEvents2, &amp;LIBID_SHDocVw, 1, 0&gt;,
  </code></pre>
  
  <p>Advising browsers events in <strong>SetSite</strong></p>
  
  <pre class="lang-c++ prettyprint-override"><code>STDMETHODIMP CBHO::SetSite(IUnknown *punkSite)
  {
      if(punkSite != NULL)
      {
          CComQIPtr&lt;IServiceProvider&gt; pServiceProvider = punkSite;
          pServiceProvider-&gt;QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (void**)&amp;m_pWebBrowser);
          IDispEventImpl&lt;1, CBHO, &amp;DIID_DWebBrowserEvents2, &amp;LIBID_SHDocVw, 1, 0&gt;::DispEventAdvise(m_pWebBrowser);
      }
  ...
  }
  </code></pre>
  
  <p>Sink map</p>
  
  <pre class="lang-c++ prettyprint-override"><code>BEGIN_SINK_MAP(CBHO)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOWNLOADCOMPLETE, OnDownloadComplete)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_NAVIGATECOMPLETE2, OnNavigateComplete2)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_ONQUIT, OnQuit)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_WINDOWSTATECHANGED, OnWindowStateChanged)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_PROPERTYCHANGE, OnPropertyChange)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, BeforeNavigate2)
      SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, DocumentComplete)
  END_SINK_MAP()
  </code></pre>
  
  <p>DocumentComplete event handler:</p>
  
  <pre class="lang-c++ prettyprint-override"><code>void _stdcall CBHO::DocumentComplete( IDispatch *pDisp, VARIANT *URL)
  {
      CComQIPtr&lt;IWebBrowser2&gt; pBrowser(pDisp);
      CComPtr&lt;IDispatch&gt; pDispDoc;
      pBrowser-&gt;get_Document(&amp;pDispDoc);
      CComQIPtr&lt;IHTMLDocument2&gt; pDoc(pDispDoc);;
      CComPtr&lt;IHTMLWindow2&gt; wnd;
      pDoc-&gt;get_parentWindow(&amp;wnd);
  
      CComPtr&lt;IHTMLLocation&gt; pLoc;
      HRESULT hr = wnd-&gt;get_location(&amp;pLoc);
      CComBSTR locHref; 
      hr=oLoc-&gt;get_href(&amp;locHref);  //"Access denied" error here
  }
  </code></pre>
  
  <p>This works correct in most cases, <strong>except the opening link in new tab</strong> (using middle click or links with target="_blank").
  In this new-tab case I get DocumentComplete event, then get non-NULL wnd pointer to the IHTMLWindow2 and IHTMLLocation, but when I try any operation with pLoc pointer, such as getting href or anything else, I get "Access denied error" error.
  Also, when check <em>doc.documentElement.outerHTML</em> I got</p>
  
  <pre class="lang-c++ prettyprint-override"><code>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;
  &lt;BODY&gt;
  &lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;
  </code></pre>
  
  <p>and this is not a content of target page.</p>
  
  <p>At the same time in IE10 this code works as expected.</p>
  
  <p>Could anybody please advise, where I am wrong and why I get invalid object in DocumentComplete?</p>