Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

IHTMLElement stored as Object in CheckListBox

I am storing the "pointer' to an IHTMLElement in a CheckListbox like this:

  Root := DomNode.parentnode.parentnode.parentnode.parentnode as IHTMLElement;
  CheckListBox1.Items.AddObject(GetName(DomNode.nodeValue), TObject(Root));

Now I want to get the object back so I can get to it's outerHTML value.

These aren't working:
TObject(CheckListBox1.Items.Objects[i]^) as IHTMLElement;
TObject(CheckListBox1.Items.Objects[i]) as IHTMLElement;
IHTMLElement(TObject(CheckListBox1.Items.Objects[i]));

what am I doing wrong, here?


Avatar of Russell Libby
Russell Libby
Flag of United States of America image


The smart pointer is getting in your way (I believe, hard to tell with the code clip), because Root is a local var, and it is dropping the ref count and releasing the interface when it goes out of scope. This leaves you with a couple options.

1. Manually _addref when adding to the checklistbox, and _release when you are done with it.
2. Use a TInterfaceList to add the interfaces to, and store the index of the list as the checklist item object
3. Use a pointer to store the interface so that the compiler does not auto addref/release the interface, eg:

var  pvItem:           Pointer;
begin

  // Make sure pointer is cleared, because the interface cast will cause the
  // pointer to be released if non nil, which would be bad...
  pvItem:=nil;

  // Get addref'd interface as pointer, which will keep the compiler from releasing it
  IHTMLElement(pvItem):=DomNode.parentnode.parentnode.parentnode.parentnode as IHTMLElement;

  // Add to list
  CheckListBox1.Items.AddObject(GetName(DomNode.nodeValue), TObject(pvItem));

  ...

  // To cleanup correctly and perform the release
  IHTMLElement(pvItem):=nil;

Number 2 will most likely be the least error prone, but all 3 are valid.

Regards,
Russell


Avatar of Eddie Shipman

ASKER

root is a global var to the form.

You didn't show how to get it back into another IHTMLElement object.
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Once again Russell, you have the correct answer.

I didn't know about TInterfaceList. Now I have it working...

Thanks.
Hey, Russell, if I store the interface in the InterfaceList and loop the document again, will
the outerHTML change? I am doing this and it is not returning the same outerHTML ass when
I placed it in the list.
nevermind, I'll post another question about commenting out sections of code in an IHTMLDocument2.