简体   繁体   中英

How to find if an IHTMLDocument2 is equal to IDispatch document in Delphi?

I have a TEmbeddedWB ( https://sourceforge.net/projects/embeddedwb/ ) with an iFrame in it. I have to find out that a specific HTML Tag is inside that iFrame or not. My iFrame object is a IHTMLFrameBase2 , while the Tag is a IHTMLElement . I know that the iFrame.contentWindow.document (which is a IHTMLDocument2 ) is the same as Tag.document . But the Tag.document is an IDispatch object, therefore the following gives a false:

if iFrame.contentWindow.document = Tag.document then ShowMessage('In iFrame')
else ShowMessage('Not in iFrame');

I know that the two object is the same, because the Watch List can show their memory address:

观察名单

But I can't get their addresses from code. What I've tried:

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

Note: If I run line-by-line the addresses that the Watch List is showing change after EVERY line of code, no matter the code affects the WebBrowser or not.

From the 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).

So, ask them both for their IUnknown interface, and compare.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM