?
Solved

IHTMLElement stored as Object in CheckListBox

Posted on 2006-04-17
6
Medium Priority
?
394 Views
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;
IHTMLElement(TObject(CheckListBox1.Items.Objects[i]));

what am I doing wrong, here?


0
Comment
Question by:Eddie Shipman
  • 4
  • 2
6 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16470233

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


0
 
LVL 26

Author Comment

by:Eddie Shipman
ID: 16470324
root is a global var to the form.

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

Accepted Solution

by:
Russell Libby earned 2000 total points
ID: 16470602
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.

IHTMLElement(TObject(CheckListBox1.Items.Objects[i]));

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;

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 26

Author Comment

by:Eddie Shipman
ID: 16471938
Once again Russell, you have the correct answer.

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

Thanks.
0
 
LVL 26

Author Comment

by:Eddie Shipman
ID: 16472080
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.
0
 
LVL 26

Author Comment

by:Eddie Shipman
ID: 16472165
nevermind, I'll post another question about commenting out sections of code in an IHTMLDocument2.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 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