Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I "typecast" a OleVariant to a give COM object

0 views
Skip to first unread message

Rich JOnes

unread,
May 24, 2001, 12:48:14 AM5/24/01
to
Hi,

I have an OleVariant passed to me by a COM server event written in VB. This
variant is a COM object. I know what type of COM object it is because it
has a property called MessageType. I would like to pass the variant to a
function has the COM object as a parameter. I cannot figure out how to
"typecast" the variant to the COM object.

For example:
I have an event handle called OnDataMessage that passed in an OleVariant
parameter called objMessage.

Procedure TprocessMessage.OnDataMessage(Sender: Tobject; var objMessage:
OleVariant);
Begin

Case objMessage.MessageType of
MsgTypeOne: ProcessObjectAsTypeOne(objMessage);
.
end; { case }
end;

procedure ProcessObjectAsTypeOne(aTypeOneObj: ItypeOneObj);
begin
.
end;

I've tried to use the AS operator, but I constantly get the error "Operator
not applicable to this operand type." I understand this to mean that the
interface does not have a GUID, but I checked and the interface does have a
GUID. The COM object is declared to have both an interface and a
dispinterface. Any idea what I can do to make this work?

BTW, I already know I can access the COM objects properties without
"typecasting" it. The reason for doing this is: 1) I need the extra speed
of early binding and 2) I have functions already declared with parameter of
the COM object.

TIA,
Rich.


Deborah Pate (TeamB)

unread,
May 24, 2001, 6:08:54 AM5/24/01
to
<<Rich JOnes:

I cannot figure out how to "typecast" the variant to the
COM object.
>>

Variants hold IDispatch or IUnknown interfaces (if in
doubt, the VarType function will tell you which). If you
know a variant contains an IDispatch, you can just cast it:

var
Disp: IDispatch;

Disp := IDispatch(SomeVariant);

Of course you can also use the as operator to get another
interface:
SomeIntf := IDispatch(SomeVariant) as ISomeIntf;

--
Deborah Pate (TeamB)
http://delphi-jedi.org
Sorry, no email please.

0 new messages