获得iframe标签的元素指针

CComPtr<IHTMLElement> spAdIframe = ...

CComQIPtr<IHTMLFrameBase2> spFrameBase2 = spAdIframe;

CComPtr<IHTMLWindow2> spIframeWindow2 = NULL;
hr = spFrameBase2->get_contentWindow(&spIframeWindow2);

由此可得到iframe元素对应的IHTMLWindow2指针,然而如果直接使用IHTMLWindow2::get_document来获取IHTMLDocument2指针的话,经常得到的是E_ACCESSDENIED错误,因为iframe元素经常用于跨域的访问,即iframe内的文档来源src与其parent的src的hostname不同,但可以通过下面的方法绕过webbrowser的安全检查。

// 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;
}

  

附:
IE(控件/接口)中主要有4个部分, Browser, Document, Frame/IFrame, Element , 其对应接口分别是
Browser - IWebBrowser2
Document - IHTMLDocument2
Frame/IFrame- IHTMLWindow2
Element - IHTMLElement
可以通过下面方法互相获取
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
element -> Frame IHTMLElement->QI(IHTMLFrameBase2) -> IHTMLFrameBase2->get_contentWindow -> IHTMLWindow2 ->
HtmlWindowToHtmlWebBrowser -> IWebBrowser2

参考:

http://shenan1984.blog.163.com/blog/static/2530851020109201042263/

最新文章

  1. NOIP2009最优贸易[spfa变形|tarjan 缩点 DP]
  2. &quot;undefined method `root&#39; for nil:NilClass&quot; error when using &quot;pod install&quot; 解决办法
  3. C语言习题(结构)
  4. C#中的选择语句
  5. Java中的抽象类
  6. 细说Linux下软件包的安装与管理
  7. poj 2739 Sum of Consecutive Prime Numbers 解题报告
  8. 在VC中显示和处理图片的方法
  9. SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言]
  10. 用golang写的生成文件md5sum,检验文件md5sum
  11. javascript于&amp;quot;return obj === void 0&amp;quot;这样的书面理由和优势
  12. java的return区别
  13. 【网络爬虫入门04】彻底掌握BeautifulSoup的CSS选择器
  14. C# 委托详解(一)
  15. kubernetes进阶(01)kubernetes的namespace
  16. 未完成的IT路停在回车键---2014年末总结篇
  17. 深入Ambari Metrics 机制分析
  18. LTE学习笔记(一)——背景知识
  19. python itchat+机器人web api实现个人微信机器人
  20. 基于jQuery+HTML5加入购物车代码

热门文章

  1. [Tex学习笔记]发一篇文章的经历
  2. git branch几个简单操作
  3. Chap5:32– 34
  4. python笔记一
  5. &lt;meta http-equiv=&quot;refresh&quot; content=&quot;0; url=&quot;&gt;是什么意思?
  6. 关于thinkphp 中的字段自动检查机制
  7. Libevent库 编译与使用
  8. 在一般处理文件中访问Session需要添加IRequiresSessionState(转载)
  9. 登陆mysql时出现unknown variable &#39;character_set_client=UTF8&#39; 的错误
  10. C#算法之向一个集合中插入随机不重复的100个数