Re: TListview storing pointers to Com objects?

From: Chris Cheney (cjc1_at_nospam%ucs.cam.ac.uk%no%spam%please)
Date: 02/18/04


Date: 18 Feb 2004 11:57:24 GMT


"Savannah_Alan" <fake@email.com> wrote in
news:XMKdnQcLZ4fkua7dRVn-gQ@comcast.com:

> I have a TListview, which I am trying to get to store pointers to a com
> object. Whenever the following code is run, I can only access the very
> last object added. Anything else gives the error:
> ---------------------------
> Debugger Exception Notification
> ---------------------------
> Project raised exception class EOleException with message 'No such
> interface supported'. Process stopped. Use Step or Run to continue.
> ---------------------------
> OK Help
> ---------------------------
>
> Am I missing something really obvious here?
>
> Thanks.
>
> procedure TfrmMain.ShowMessages();
> var
> TempMessage: CSMailLib_TLB.Message;
> SearchRecord: TSearchRec;
> NewItem: TListItem;
> begin
> //This routine will populate the inbox listview with the messages
> saved lvwMessages.Clear;
> try
> if (FindFirst(AppPath() + '\Messages\' + cboAccounts.Text +
> '\*.msg',
> faAnyFile, SearchRecord))=0 then
> begin
> TempMessage:=coMessage.Create;
> TempMessage.LoadFromFile(AppPath() + '\Messages\' +
> cboAccounts.Text +
> '\' + SearchRecord.Name);
> NewItem:=lvwMessages.Items.Add;
> NewItem.Data:=Pointer(TempMessage);

By inspection (i.e. not by testing the code), I reckon the problem starts in
the above line. When Delphi assigns a COM object, it implicitly compiles in
the necessary _AddRef/_Release calls. By typecasting the RHS as Pointer, you
are telling the compiler that it is to treat the value as a Pointer, not as
a COM object.

You could, I think:

a) leave the code as it is and add the necessary _AddRef call.

or

b) typecast the LHS to CSMailLib_TLB.Message, removing the typecast from the
RHS

but, either way, you must ensure that _Release gets called when the
TListItem gets destroyed.

HTH



Relevant Pages

  • Re: Pointers and string lists?
    ... Of course one may upgrade to a DVersion which doesn't accept ... >of objects and then giving the Data pointer property the address of the ... typecast the Data to your object class when you use the treeview pointer. ...
    (alt.comp.lang.borland-delphi)
  • Re: Ping Source Code Question: ICMP Echo_Request
    ... It's basically a C style typecast. ... Referencing outpack without the array index results in a ... pointer to the first element (there isn't really that much difference ...
    (alt.os.linux)
  • function called through a non-compatible type
    ... When I typecast a function and call it trough the casted pointer (se ... code below) I get the warnings: ... int foo{ ...
    (comp.lang.c)
  • Re: What are your favorite features?
    ... TWhatever, or even a pointer. ... You don't typecast in the for in statement, ... Ptr: Pointer; ... TeamB don't see posts sent via Google or ISPs ...
    (borland.public.delphi.non-technical)