Re: How to return string through output VARIANT from C++ COM object

From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 07/26/04


Date: Mon, 26 Jul 2004 18:35:05 -0400


"Kim Gräsman" <kim@mvps.org> wrote in message
news:uo5FR7ncEHA.2408@tk2msftngp13.phx.gbl
>> "If the variant to be cleared is a COM object that is passed by
>> reference, the vt field of the pvarg parameter is VT_DISPATCH |
>> VT_BYREF or VT_UNKNOWN | VT_BYREF. In this case, VariantClear does
>> not release the object. Because the variant being cleared is a
>> pointer to a reference to an object, VariantClear has no way to
>> determine if it is necessary to release the object. It is therefore
>> the responsibility of the caller to release the object or not, as
>> appropriate."
>
> Gah! I had no idea... I thought VariantClear was smarter than that.

It is plenty smart. A VT_BYREF variant does not own the resource it
holds a reference to. Hence VariantClear is not supposed to release it.
You can legally have

VARIANT var;
var.vt = VT_BSTR;
var.bstrVal = SysAllocString(OLESTR("blah"));

VARIANT varRef;
varRef.vt = VT_BSTR | VT_BYREF;
varRef.pbstrVal = &var.bstrVal;

VariantClear(&varRef); // does not relase the string
VariantClear(&var); // releases the string

> You would have to dereference the VARIANT then, before clearing its
> contents.

Illegal. The server is not supposed to touch the referred-to data, as
opposed to the data directly contained in the VARIANT. Straight
VariantClear implements correct semantics.

-- 
With best wishes,
    Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Relevant Pages

  • Re: UPDATE: Possibly Unknown Virus? Care to help me analyze?!?
    ... Contains IRC commands, most certainly includes a IRC client embedded. ... Possibly a variant of "Randon": ... maybe a nickname or a reference. ...
    (Incidents)
  • Macro RunCode Action Syntax for Referring to a Control on a Form ?
    ... reference to the form, an object reference to the control (usually a ... Public Function SetListBox(aForm As Variant, lbxBox As Variant, lItemNo ... Control, for example) have all failed to work. ...
    (microsoft.public.access.macros)
  • RE: ReadFile
    ... Sebastienm ... "Kevin" wrote: ... Am I missing a Reference? ... I changed the datatypes to variant and was able ...
    (microsoft.public.excel.programming)
  • Invalid Callee when passing param by reference
    ... I'm creating a third-party object on the server that's using COM+ ... to pass several parameter by reference, so I can continues the precess. ... Local cAcsUrl as Variant, nPaReq as Variant, nVerifyId as Variant ... because passing parameters by reference for an RPC call doesn't sound ...
    (microsoft.public.win32.programmer.ole)
  • Re: Accessing binary field in a Microsoft Access database table
    ... mismatched data types. ... There is absolutely no guarantee that the variant contains a long. ... The variants should be used _BY VALUE_ not by pointer. ... pointer (reference) to the variant, ...
    (microsoft.public.data.ado)