Tip: Accessing the IHTMLDocument Interface of an HTML Frame Within Internet Explorer

When developing applications that embed the IE webbrowser control as an application rendering engine or when developing browser debugging tools, the cross-domain scripting restrictions in the browser can limit the effectiveness of developing those solutions. Sometimes you need to be able to manipulate the contents of an iframe programatically from these tools and using the DOM doesn't work because of the safeguards that the browser has in place to safeguard against cross-domain scripting attacks.

Microsoft provides the following snippet for enumerating the frames within an IHTMLDocument in the following article, How to get the WebBrowser object model of an HTML frame. This is only of limited usefulness since it only demonstrates iterating through all frames and refreshing them. The following snippet shows how to obtain the IHTMLDocument interface for an iframe by its id.

HRESULT getFrameDocumentById(IHTMLDocument2* topLevelDocument,
                             LPOLESTR frameId,
                             IHTMLDocument2** frameDocument)
{
   HRESULT hresult = E_FAIL;
   CComPtr<IOleContainer> container;
   HRESULT hr = topLevelDocument->QueryInterface(IID_IOleContainer,
                                                 (void**)&container);
   if (hr == S_OK) 
   {
      CComPtr<IEnumUnknown> enumerator;
      // Get an enumerator for the frames 
      hr = container->EnumObjects(OLECONTF_EMBEDDINGS, &enumerator);
      if (hr == S_OK) 
      {
         CComPtr<IUnknown> unk;
         ULONG uFetched;
         // Enumerate and refresh all the frames
         for (UINT i = 0; S_OK == enumerator->Next(1, &unk,
              &uFetched); i++) 
         {
            CComPtr<IWebBrowser2> browser;
            // QI for IWebBrowser here to see if we have an embedded
            // browser
            hr = unk->QueryInterface(IID_IWebBrowser2,
                                     (void**)&browser);
            if (hr == S_OK) 
            { 
               CComPtr<IHTMLElement> htmlElement;
               unk->QueryInterface(IID_IHTMLElement,
                                   (void**)&htmlElement);
               CComBSTR id;
               if (SUCCEEDED(htmlElement->get_id(&id)) && id)
               {
                  CComPtr<IDispatch> disp;
                  browser->get_Document(&disp);
                  if (disp != NULL )
                  { 
                     CComPtr<IHTMLDocument2> frameDoc;
                     hr = disp->QueryInterface( IID_IHTMLDocument2,
                                               (void**)&frameDoc);
                        if (hr == S_OK)
                        {
                           if (!wcscmp(id, frameId))
                           {
                              *frameDocument = frameDoc;
                              (*frameDocument)->AddRef();
                              hresult = S_OK;
                              break;
                        }
                     }
                  }
               }
            }
         }
      }
   }
   return hresult;
}

The author maintains a blog at http://www.znsoftware.com with more programming tips, snippets, and software.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds