When IHTMLWindow2::get_document returns E_ACCESSDENIED
来源:互联网 发布:java 条码枪的开发 编辑:程序博客网 时间:2024/05/17 09:41
When IHTMLWindow2::get_document returns E_ACCESSDENIED
Internet Explorer extensions usually needs to access HTML elements. When extensions are initialized they get a IWebBrowser2pointer representing the browser. Starting with this pointer one canget any HTML element in the web page but to do that we need to browse ahierarchy of frames first. The simplest web pages only have one frameand one document. Web pages containing <frame> or <iframe> have a hierarchy of frames, each frame having its own document.Here are the objects involved and the corresponding interfaces:
browser - IWebBrowser2
frame/iframe - IHTMLWindow2
document - IHTMLDocument2
element - IHTMLElement
The list bellow shows what method to call to get one object from another:
browser -> document IWebBrowser2::get_Document
document -> frame IHTMLDocument2::get_parentWindow
frame -> document IHTMLWindow2::get_document
frame -> parent frame IHTMLWindow2::get_parent
frame -> children frames IHTMLWindow2::get_frames
A normal call chain to get a HTML element is:
browser -> document -> frame -> child frame -> ... -> child frame -> document -> element
Thiswill work almost all the time. The problems arise when different framescontain documents loaded from different internet domains. In this case IHTMLWindow2::get_document returns E_ACCESSDENIED when trying to get the document from the frame object. I think this happens to prevent cross frame scripting atacks.
Here is HtmlWindowToHtmlDocument function I wrote to be used instead IHTMLWindow2::get_document to bypass the restriction:
// Converts a IHTMLWindow2 object to a IHTMLDocument2. Returns NULL in case of failure.
// It takes into account accessing the DOM across frames loaded from different domains.
CComQIPtr<IHTMLDocument2> HtmlWindowToHtmlDocument(CComQIPtr<IHTMLWindow2> spWindow)
{
ATLASSERT(spWindow != NULL);
CComQIPtr<IHTMLDocument2> spDocument;
HRESULT hRes = spWindow->get_document(&spDocument);
if ((S_OK == hRes) && (spDocument != NULL))
{
// The html document was properly retrieved.
return spDocument;
}
// hRes could be E_ACCESSDENIED that means a security restriction that
// prevents scripting across frames that loads documents from different internet domains.
CComQIPtr<IWebBrowser2> spBrws = HtmlWindowToHtmlWebBrowser(spWindow);
if (spBrws == NULL)
{
return CComQIPtr<IHTMLDocument2>();
}
// Get the document object from the IWebBrowser2 object.
CComQIPtr<IDispatch> spDisp;
hRes = spBrws->get_Document(&spDisp);
spDocument = spDisp;
return spDocument;
}
// Converts a IHTMLWindow2 object to a IWebBrowser2. Returns NULL in case of failure.
CComQIPtr<IWebBrowser2> HtmlWindowToHtmlWebBrowser(CComQIPtr<IHTMLWindow2> spWindow)
{
ATLASSERT(spWindow != NULL);
CComQIPtr<IServiceProvider> spServiceProvider = spWindow;
if (spServiceProvider == NULL)
{
return CComQIPtr<IWebBrowser2>();
}
CComQIPtr<IWebBrowser2> spWebBrws;
HRESULT hRes = spServiceProvider->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&spWebBrws);
if (hRes != S_OK)
{
return CComQIPtr<IWebBrowser2>();
}
return spWebBrws;
}
- When IHTMLWindow2::get_document returns E_ACCESSDENIED
- 利用IHTMLWindow2 的get_document 返回E_ACCESSDENIED 解决方法
- IHTMLWindow2的get_document方法有时候会返回E_ACCESSDENIED
- what-to-do-when-nexus-returns-401
- Vector3.Lerp(When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the point midway betw)
- When a Subquery Returns Too Many Values(in(
- 解决CeRapiInvoke 返回 E_ACCESSDENIED
- 解决0x80070005 (E_ACCESSDENIED)问题
- superman returns
- Superman Returns
- DCOM Security---the reason for e_accessdenied
- 如何查看IHTMLWindow2等调用不成功后返回的错误信息
- 利用IWebBrowser2的IWebBrowser2::get_Document得doc对象
- The Godfather Returns
- 超人再起 Superman Returns
- A function returns function
- CFReadStreamCopyProperty() returns NULL?
- geoCoder.getFromLocationName returns null
- AS3.0基础学习笔记(6): 类继承
- PB5.0/6.0更新地址
- 走近汇编理解与内核编程
- VC++6.0远程调试
- MIME 类型大全
- When IHTMLWindow2::get_document returns E_ACCESSDENIED
- 获取IE (控件)的所有链接(包括Frameset, iframe)
- c#反射实践
- 数据库和金库,哪个能防监守自盗?
- JAVA反射机制的应用
- 学习Oracle
- 怎么找java方面的实习工作
- MyEclipse快捷键
- 没啥