如何在Delphi中查找IHTMLDocument2是否等于IDispatch文档?

前端之家收集整理的这篇文章主要介绍了如何在Delphi中查找IHTMLDocument2是否等于IDispatch文档?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有iFrame的TEmbeddedWB( https://sourceforge.net/projects/embeddedwb/).我必须找出特定的HTML标签是否在iFrame内部.我的iFrame对象是IHTMLFrameBase2,而Tag是IHTMLElement.我知道iFrame.contentWindow.document(它是一个IHTMLDocument2)与Tag.document相同.但Tag.document是一个IDispatch对象,因此下面给出了一个false:
if iFrame.contentWindow.document = Tag.document then ShowMessage('In iFrame')
else ShowMessage('Not in iFrame');

我知道这两个对象是一样的,因为Watch List可以显示它们的内存地址:

但是我无法从代码获取他们的地址.我尝试过的:

Addr(iFrame.contentWindow.document) // Gives variable required error
@iFrame.contentWindow.document      // Gives variable required error
Pointer(iFrame.contentWindow.document)  //Compiles,but gives wrong address
Format('%p',[iFrame.contentWindow.document]) //Compiles,but gives EConvertError

注意:如果我逐行运行监视列表显示的地址在每行代码后都会发生变化,无论代码是否影响WebBrowser.

解决方法

rules of COM

It is required that any call to QueryInterface on any interface for a given object instance for the specific interface IUnknown must always return the same physical pointer value. This enables calling QueryInterface(IID_IUnknown,…) on any two interfaces and comparing the results to determine whether they point to the same instance of an object (the same COM object identity).

所以,问他们两个IUnknown接口,并进行比较.

var
  disp: IDispatch;
  doc: IHTMLDocument2;
....
if (disp as IUnknown) = (doc as IUnknown) then
  ....

猜你在找的Delphi相关文章