IHTMLElement stored as Object in CheckListBox

Posted on 2006-04-17
Last Modified: 2010-08-05
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?

Question by:EddieShipman
    LVL 26

    Expert Comment

    by:Russell Libby

    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.


    LVL 26

    Author Comment

    root is a global var to the form.

    You didn't show how to get it back into another IHTMLElement object.
    LVL 26

    Accepted Solution

    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;

    LVL 26

    Author Comment

    Once again Russell, you have the correct answer.

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

    LVL 26

    Author Comment

    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.
    LVL 26

    Author Comment

    nevermind, I'll post another question about commenting out sections of code in an IHTMLDocument2.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now