获得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/

firame标签: IHTMLElement -> IHTMLFrameBase2 -> IHTMLWindow2 -> IHTMLDocument2 跨域访问的更多相关文章

  1. js,java,ajax实现跨域访问及其原理

    http://blog.csdn.net/saytime/article/details/51540876 这篇文章对跨域访问做了较为细致得分析,我这里做下简单总结 1.实现跨域访问原理: 浏览器由于 ...

  2. WebBrowser控件跨域访问页面内容

    原文出处 :http://blog.csdn.net/nocky/article/details/6056802 源码出处:http://www.codecentrix.com/blog/wnd2do ...

  3. Jsonp跨域访问

    很早之前看过好几篇跨域访问的文章,然后做项目的时候基本没有遇到跨域访问的问题.不过该来的还是会来,前些天终于让我遇到了.于是重温了一下原理这些,再进行实战.于是现在也敢通过实战后的一些理解来和大家分享 ...

  4. js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource

    js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...

  5. 浅析JSONP-解决Ajax跨域访问问题

    浅析JSONP-解决Ajax跨域访问问题 很久没有写随笔了,总是感觉没时间,其实时间就是...废话少说,前几天,工作上有一新需求,需要前端web页面异步调用后台的Webservice方法返回信息.实现 ...

  6. 用jQuery与JSONP轻松解决跨域访问的问题

    浏览器端的真正跨域访问,推荐的是目前jQuery $.ajax()支持get方式的跨域,这其实是采用jsonp的方式来完成的. var qsData = {'searchWord':$("# ...

  7. 三种方法实现js跨域访问

    转自:http://narutolby.iteye.com/blog/1464436 javascript跨域访问是web开发者经常遇到的问题,什么是跨域,一个域上加载的脚本获取或操作另一个域上的文档 ...

  8. jQuery 跨域访问问题解决方法(转)

    转自:http://www.jb51.net/article/21213.htm 浏览器端跨域访问一直是个问题, 多数研发人员对待js的态度都是好了伤疤忘了疼,所以病发的时候,时不时地都要疼上一疼.记 ...

  9. C# 和Jsonp的一个小demo 用jQuery与JSONP轻松解决跨域访问的问题

    客服端:      在A项目下面 建立一个 JsonpClient.aspx页面,代码如下: <%@ Page Language="C#" AutoEventWireup=& ...

随机推荐

  1. 深入理解 Laravel Eloquent(三)——模型间关系(关联)

    Eloquent是什么 Eloquent 是一个 ORM,全称为 Object Relational Mapping,翻译为 "对象关系映射"(如果只把它当成 Database A ...

  2. onmouseover和onmouseout的那些事

    这篇文章来自一个偶然...以前刚开始学习javascript事件的时候就被一个东西搞得晕头撞向的.就是一对名字很相近的事件.一组是onmouseover()和onmouseout().另一组就是onm ...

  3. startssl

    Validation Success You have successfully authenticated domain "xxx.com.cn".You will be abl ...

  4. ubuntu下基于sqlite3后台的php环境的搭建

    最近准备把公司的服务器换成linux 数据库sqlite3 搭建过程记录如下: 1 sqlite3安装.. apt-get install sqlite 2.PHP服务器搭建. apt-get ins ...

  5. archlinux 内核编译笔记

    # cp linux-3.10.5.tar.gz /usr/src/linux-3.10.5.tar.gz# cd /usr/src# tar xvzf linux-3.10.5.tar.gz lin ...

  6. VBA_Excel_教程:过程,函数

    Sub s1() Debug.Print "s1" '调用过程:无括号,加call提升可读性 s2 Call s2 End Sub Sub s2() Debug.Print &qu ...

  7. Python从题目中学习:List comprehension

    九九乘法表作业其实有更简单的做法,就是用列表推导式. ------------------------------------------------------------------------- ...

  8. input自动获取焦点

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>te ...

  9. 'Invalid parameter not satisfying: body'

    afnetwork图片上传的时候出错,出现错误 2015-11-09 15:47:59.086 videoPro[3207:132795] *** Assertion failure in -[AFS ...

  10. ASP.Net将图片以二进制方式存入数据库,并读取

    把图片转换成二进制--把二进制转换成图片 private void button1_Click(object sender, EventArgs e) { string path = this.tex ...