Board index » delphi » error casting class to interface

error casting class to interface

I am trying to use an ole "callback" object in the following manner :-

I have an class declared which derives from TAutoObject and my interface
ITermCtrl. I have a simple VB5 activex dll which has a property Ctrl declared
as Object. The idea is to create my TermCtrl object, create an instance of
the VB5 object, and pass the TermCtrl object to the Ctrl property of the VB
object. The VB object can then call certain methods of the Ctrl object.

If I declare the TermCtrl object as type TTermCtrl and cast it using
ITermCtrl (TermCtrl) the code compiles, but produces a run time error
referring to pointer errors. I have tried declaring the TermCtrl object as
ITermCtrl and passing it without a cast and this works, however, I want to
use additional delphi objects within the TTermCtrl class that will not map to
olevariant types. The documentation seems to indicate that if a class
implements an interface, then you should be able to cast an object of that
class to the interface type.

What am I doing wrong?

TIA

Anthony Hawes.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

 

Re:error casting class to interface


Hello,

Use the "as" operator (TermCtrl as ITermCtrl) to cast an instance to the
interface it supports. Don't use C-style casting (ITermCtrl (TermCtrl))
because that does not correctly do the QI for you.

have fun,

--
Binh Ly
Brickhouse Data Systems, Inc.
http://www.brickhouse.com

Quote
anthony.ha...@{*word*269}.net wrote in message

<6p5ktn$le...@nnrp1.dejanews.com>...
Quote
>I am trying to use an ole "callback" object in the following manner :-

>I have an class declared which derives from TAutoObject and my interface
>ITermCtrl. I have a simple VB5 activex dll which has a property Ctrl
declared
>as Object. The idea is to create my TermCtrl object, create an instance of
>the VB5 object, and pass the TermCtrl object to the Ctrl property of the VB
>object. The VB object can then call certain methods of the Ctrl object.

>If I declare the TermCtrl object as type TTermCtrl and cast it using
>ITermCtrl (TermCtrl) the code compiles, but produces a run time error
>referring to pointer errors. I have tried declaring the TermCtrl object as
>ITermCtrl and passing it without a cast and this works, however, I want to
>use additional delphi objects within the TTermCtrl class that will not map
to
>olevariant types. The documentation seems to indicate that if a class
>implements an interface, then you should be able to cast an object of that
>class to the interface type.

>What am I doing wrong?

>TIA

>Anthony Hawes.

>-----== Posted via Deja News, The Leader in Internet Discussion ==-----
>http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

Re:error casting class to interface


Or use the TObject.GetInterface method if you compiler jumps from the as(s)
:)

Richard

Other Threads