Board index » delphi » SOLVED: By Value Variant Parameters in VC++ ActiveX Control Events

SOLVED: By Value Variant Parameters in VC++ ActiveX Control Events

According to the VC++ documentation, when an ActiveX control wishes to pass
a VARIANT parameter by value in an event, it specifies the type as
VTS_VARIANT and passes the parameter as a const reference.  This is
supposedly distinct from a by reference VARIANT parameter which is passed as
VTS_PVARIANT, and a pointer passed.  Someone somewhere seems to have
forgotten that a const reference _is_ a pointer!

Unfortunately, what this means is that the parameter arrives in the Delphi
event handler as a POINTER.  When the compiler generated event handler
procedure entry code tries to apply _Var System routines to this parameter,
it finds itself confronted with a pointer, and unsurprisingly an exception
is raised.

Fortunately, you can fool the compiler into generating the write code by
manually adding the "const" directive to the parameter in the event handler.

I don't think this could properly be attributed as a Delphi bug, since the
type library generated for the ActiveX control is clearly lying about the
nature of the parameter.  Unless there is a reference somewhere explaining
how all variants are passed by address, even when specified as passed by
value, this seems to me to be a Microsoft fault.

Interestingly, VB has no problem interpreting such an incorrectly described
parameter, suggesting that either this is documented as the correct
behaviour somewhere, or alternatively that Microsoft are up to their usual
tricks of mandating one behaviour for the rest of the world, whilst they
happily make and break their own rules as they go along.

Has anyone ever tried writing a VB event handler for a Delphi sourced event
which does actually pass a VARIANT, rather than a pointer to a variant?  If
so, what happened?

Jolyon Smith
Neot Software Services Ltd.

begin 666 Jolyon Smith.vcf
M(#$T.# @,S@U-3<Q#0I414P[5T]22SM&05@Z*S0T(#$T.# @,S4Y,#<Y#0I!
M<VAI<F4],$0],$%53DE4...@2TE.1T1/30T*55),.@T*55),.FAT=' Z+R]W
M=W<N;F5O="YC;RYU:PT*14U!24P[4%)%1CM)3E1%4DY%5#IJ<VUI=&A ;F5O
J="YC;RYU:PT*4D56.C(P,# P-S(W5#$S,30U,5H-"D5.1#I60T%21 T*


Re:SOLVED: By Value Variant Parameters in VC++ ActiveX Control Events

> ...write code...

Ooops.  That should of course read "right code".  <smacks-head>

Jolyon Smith
Neot Software Services Ltd.

Other Threads