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