【问题标题】:Is there a way to use the latest IE9 MSHTML interfaces from C++ Builder (or Delphi) with TCppWebBrowser?有没有办法通过 TCppWebBrowser 使用来自 C++ Builder(或 Delphi)的最新 IE9 MSHTML 接口?
【发布时间】:2011-08-25 00:56:17
【问题描述】:

我有一种情况,我想使用一些通过 IE9 附带的IHTMLDocument7 接口可用的方法。特别是 getElementsByTagNameNS() 方法,因为我想使用特定的标签类型(比解析整个文档容易得多)。

我当前的代码如下所示:

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();
        }
    }

这可行,但可能有问题,但我最感兴趣的是立即获得我想要的功能。

如果我更改此代码以使用 IE9 提供的新界面:

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

我收到以下编译器错误:

[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 *)

它似乎找不到此接口的匹配项。

  • 我该怎么做才能做到这一点 接口可用?我猜是 BCB 附带的 Windows SDK 版本 可能已经过时,或者不知道 关于 IE9 的类型库 MSHTML 版本。
  • 有没有办法 制作适当的标题 可用于此接口 (IID_IHTMLDocument7),并保留 TCPpWebBrowserControl?还是我需要 导入单独的 ActiveX 控件?

我在带有 IE9 的 Windows 7 (x64) 上使用 C++ Builder Starter XE (15.0.3953.35171)。

【问题讨论】:

    标签: activex webbrowser-control c++builder mshtml


    【解决方案1】:

    IE9 标头可在http://msdn.microsoft.com/en-us/ie/aa740471 下载

    我不确定您的 BCB 链接器的使用时间。 VC 2005 的链接器需要 KB949009 hotfix 在调试配置中链接到 IE9 库。

    【讨论】:

    • VC 库一开始就不能在 BCB 中使用。
    【解决方案2】:

    至少在 C++Builder 2010 中,IHTMLDocument7 没有在 mshtml.h 中定义,但 IHTMLDocument6 是。如果您从 Microsoft 下载最新的 SDK,您可以将 IHTMLDocument7 定义直接复制到您现有的代码中。

    或者,尝试查看BCCSDK 是否已更新以支持 IHTMLDocument7。

    【讨论】:

      【解决方案3】:

      使用IHTMLDocument3 Interface代替IHTMLDocument7,或者执行javascript返回你需要的,比如:

      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();
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-03
        • 2012-01-15
        • 2016-11-21
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多