一个网页, 用 <IFrame> 引用了另一个网站的网页,
我想在CHtmlView 中获得 这个 IFrame 的IHTMLWindow2 。
 
但是试了很多 办法,都会得到拒绝访问的 错误。看到一些不完整 的资料 介绍, ie 处于安全考虑, 禁止 读取 不同域 的网页。
比如:
[ 网页url ] http://aaa.com/aaa.html 
[ iframe url ]  http://bbb.com/bbb.html 
则 不能读取 iframe 中的内容。但是 始终不死心,所以请教 有经验的朋友,看是不是能够得到。

解决方案 »

  1.   

    CComPtr<IHTMLDocument3> pDoc3;
    hr = pDoc2->QueryInterface(IID_IHTMLDocument3,(void**)&pDoc3) ;
    if(hr==S_OK)
    {
    CComBSTR bstrName("FRAME");
    CComPtr<IHTMLElementCollection> pElemCollFrame;
    hr=pDoc3->getElementsByTagName(bstrName,&pElemCollFrame);
    if (hr==S_OK)
    {
    long pLength;
    hr=pElemCollFrame->get_length(&pLength);
    if(hr==S_OK)
    {
    for(int i=0;i<pLength;i++)
    {
    IDispatch *pDispFrame=NULL;
    CComVariant vIndex=i;
    hr=pElemCollFrame->item(vIndex,vIndex,&pDispFrame);
    if(hr==S_OK)
    {
    CComPtr<IHTMLElement> pElemFrame;
    hr=pDispFrame->QueryInterface(IID_IHTMLElement,(void**)&pElemFrame); 
    if(hr==S_OK)
    {
    CComPtr<IHTMLFrameBase2> pFrameBase2;
    hr=pElemFrame->QueryInterface(IID_IHTMLFrameBase2,(void**)&pFrameBase2);
    if(hr==S_OK)
    {
    CComPtr<IHTMLWindow2> pWindow2;
    hr=pFrameBase2->get_contentWindow(&pWindow2);
    if(hr==S_OK)
    {
    CComPtr<IHTMLDocument2> pDoc2Frame;
    hr=pWindow2->get_document(&pDoc2Frame);
    if (hr==S_OK)
    {
    //得到IHTMLDocument2
    }
    }
    }
    }
    }
    pDispFrame->Release();
    }
    }
    }
    }
      

  2.   

    void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 )
    {
    if ( !pIHTMLDocument2 ) return; HRESULT hr; CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
    pIHTMLDocument2->get_frames( &spFramesCollection2 ); //取得框架frame的集合 long nFrameCount=0; //取得子框架个数
    hr = spFramesCollection2->get_length( &nFrameCount );
    if ( FAILED ( hr ) || 0 == nFrameCount ) return; for(long i=0; i<nFrameCount; i++)
    {
    CComVariant vDispWin2; //取得子框架的自动化接口
    hr = spFramesCollection2->item( &CComVariant(i), &vDispWin2 );
    if ( FAILED ( hr ) ) continue; CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
    if( !spWin2 ) continue; //取得子框架的 IHTMLWindow2 接口 CComPtr < IHTMLDocument2 > spDoc2;
    spWin2->get_document( &spDoc2 ); //取得字框架的 IHTMLDocument2 接口
    }
    }