开发者

Is there a way to use the latest IE9 MSHTML interfaces from C++ Builder (or Delphi) with TCppWebBrowser?

I have a situation whe开发者_运维技巧re I would like to use some methods available via the IHTMLDocument7 interface shipped with IE9. In particular the getElementsByTagNameNS() method because I want to work with specific tag types (a lot easier than parsing the whole document).

My current code looks like this:

IHTMLDocument2* doc = NULL;

    if (browser->ControlInterface->Document) // make sure TCppWebBrowser is OK
    {
        if (SUCCEEDED(browser->ControlInterface->Document->QueryInterface(IID_IHTMLDocument2, (void**)&doc)))
        {
            IHTMLElement* body;

            HRESULT hr = doc->get_body(&body);
            if (SUCCEEDED(hr))
            {
                WideString innerHtml;
                body->get_innerHTML(&innerHtml);
                txtInfo->Text = innerHtml;

                body->Release();
            }

          doc->Release();
        }
    }

This works, and may have issues, but I'm most interested in getting the functionality I want right now.

If I change this code to use the new interface available with IE9:

browser->ControlInterface->Document->QueryInterface(IID_IHTMLDocument7, (void**)&doc)

I get the following compiler error:

[BCC32 Error] Unit2.cpp(134): E2451 Undefined symbol 'IID_IHTMLDocument7'
  Full parser context
    Unit2.cpp(129): parsing: void _fastcall TForm2::Button4Click(TObject *)

[BCC32 Error] Unit2.cpp(134): E2285 Could not find a match for 'IUnknown::QueryInterface(undefined,void * *)'
  Full parser context
    Unit2.cpp(129): parsing: void _fastcall TForm2::Button4Click(TObject *)

It appears that it cannot find a match for this interface.

  • What should I do to make this interface available? I'm guessing the Windows SDK version shipped with BCB may be out of date, or doesn't know about a type library for the IE9 version of MSHTML.
  • Is there a way to make the appropriate headers available for this interface (IID_IHTMLDocument7), and keep the TCppWebBrowserControl? Or do I need to import a separate ActiveX control?

I am using C++ Builder Starter XE (15.0.3953.35171) on Windows 7 (x64) with IE9.


Use IHTMLDocument3 Interface instead of IHTMLDocument7, or execute javascript to return what you need, like:

IHTMLDocument2  *doc = NULL;
IHTMLWindow2 *win;

if(SUCCEEDED(CppWebBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&doc)))  {
    HRESULT hr = doc->get_parentWindow(&win);
    if (SUCCEEDED(hr))  {
      BSTR cmd = L"function deltag(){\
    var all    = this.document.getElementsByTagNameNS('IMG');  \
    var images = [];                           \
    for(var a=0;a<all.length;++a)               \
    {                                           \
        if(all[a].tagName == 'NAME')         \
            images.push(all[a]);         \
    }                                             \
    for(var i=0;i<images.length;++i)               \
    {                                               \
        images[i].parentNode.removeChild(images[i]); \
    }  " ;           \                           

   VARIANT v;
   VariantInit(&v);
   win->execScript(cmd,NULL,&v);
   VariantClear(&v);
   win->Release();
    }
    doc->Release();
  }


IE9 headers are available for downloading at http://msdn.microsoft.com/en-us/ie/aa740471

I am not sure how old is your BCB linker. VC 2005's linker requires the KB949009 hotfix to link against IE9 libs in a debug configuration.


At least in C++Builder 2010, IHTMLDocument7 is not defined in mshtml.h, but IHTMLDocument6 is. If you download an up-to-date SDK from Microsoft, you can copy the IHTMLDocument7 definition directly into your existing code.

Alternatively, try seeing if BCCSDK has been updated to support IHTMLDocument7 yet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜