Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

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

瀏覽次數:0 次
跳到第一則未讀訊息

Rich JOnes

未讀,
2001年5月24日 凌晨12:48:142001/5/24
收件者:
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)

未讀,
2001年5月24日 清晨6:08:542001/5/24
收件者:
<<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 則新訊息