Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

iframe

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:

Watch List

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.

like image 243
Fenistil Avatar asked Dec 07 '16 20:12

Fenistil


1 Answers

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
  ....
like image 110
David Heffernan Avatar answered Nov 02 '22 03:11

David Heffernan