Invalid window in DocumentComplete (DISPID_DOCUMENTCOMPLETE) in IE9 BHO
I am developing BHO and I am having problems handling events in IE9 . I process DocumentComplete (DISPID_DOCUMENTCOMPLETE) then I get IHTMLWindow2 and IHTMLLocation for future operations.
Code (simplified)
The BHO class uses
public IDispEventImpl<1, CBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 0>,
Browser Event Consulting in SetSite
STDMETHODIMP CBHO::SetSite(IUnknown *punkSite)
{
if(punkSite != NULL)
{
CComQIPtr<IServiceProvider> pServiceProvider = punkSite;
pServiceProvider->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (void**)&m_pWebBrowser);
IDispEventImpl<1, CBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 0>::DispEventAdvise(m_pWebBrowser);
}
...
}
Shell Map
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()
DocumentComplete event handler:
void _stdcall CBHO::DocumentComplete( IDispatch *pDisp, VARIANT *URL)
{
CComQIPtr<IWebBrowser2> pBrowser(pDisp);
CComPtr<IDispatch> pDispDoc;
pBrowser->get_Document(&pDispDoc);
CComQIPtr<IHTMLDocument2> pDoc(pDispDoc);;
CComPtr<IHTMLWindow2> wnd;
pDoc->get_parentWindow(&wnd);
CComPtr<IHTMLLocation> pLoc;
HRESULT hr = wnd->get_location(&pLoc);
CComBSTR locHref;
hr=oLoc->get_href(&locHref); //"Access denied" error here
}
In most cases, this works correctly, except for the link to open in a new tab (using middle click or links with target = "_blank"). In this case, with a new tab, I get a DocumentComplete event, and then I get a non-NULL wnd pointer to IHTMLWindow2 and IHTMLLocation, but when I try to do any operation on the pLoc pointer like get href or whatever, I get the error "Error access ", Also, when checking the doc.documentElement.outerHTML document, I got
<HTML><HEAD></HEAD> <BODY> <P> </P></BODY></HTML>
and that is not the content of the landing page.
At the same time, in IE10 this code works as expected.
Can anyone please advise where I am going wrong and why I am getting an invalid object in the DocumentComplete?
source to share
No one has answered this question yet
Check out similar questions: