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;

what am I doing wrong, here?

LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell LibbySoftware Engineer, Advisory Commented:

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;

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

  // 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

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


Eddie ShipmanAll-around developerAuthor Commented:
root is a global var to the form.

You didn't show how to get it back into another IHTMLElement object.
Russell LibbySoftware Engineer, Advisory Commented:
If Root is global, then you will still run into problems if you use it in a loop, eg:

for x = 0 to count
   get interface reference into Root
  add Root to the check list box

As each attempt to get a new reference will clear the existing reference. As far as obtaining a reference back, you already posted the code to do this.


This would only change if you used code for method 2, as the object would be holding the index:

ref = IntfList[Integer(CheckListBox1.Items.Objects[i])] as IHTMLElement;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Eddie ShipmanAll-around developerAuthor Commented:
Once again Russell, you have the correct answer.

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

Eddie ShipmanAll-around developerAuthor Commented:
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.
Eddie ShipmanAll-around developerAuthor Commented:
nevermind, I'll post another question about commenting out sections of code in an IHTMLDocument2.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.