hello every body i just join us so .....
i have a little problem with a variant , i call in first step : "variantClear(&bbvariant)"

and after when i call a member of my bbvariant " _bstr_t (bbvariant.bstrVal).length() " is not equal to 0 , so i want to someone explain to me what happen when u call VariantClear,
if the length of my bstrVal is not 0 what's hapen ?

Dani AI

Generated

Short, practical clarification for the thread (addresses ’s observation and ’s pointers).

_variant_t is a thin C++ wrapper around the VARIANT layout; its destructor (and its Clear helper) call the same cleanup the OLE API uses, so the wrapper is layout-compatible with raw VARIANT functions. That is why calling VariantClear on the wrapper’s address will compile/operate — but using the wrapper’s Clear() or letting its destructor run is the clearer, safer idiom in C++. ()

What VariantClear / Clear actually do: they release any resources the VARIANT owns (BSTRs, interfaces, SAFEARRAYs, etc.) and set the variant type to empty. They do not necessarily zero every byte of the union, so raw union fields (like the old bstr pointer) can still contain the previous bit pattern even though the variant no longer owns that memory — reading it is undefined and can crash or return stale-looking data. Treat a cleared variant as empty; don’t inspect its union contents. (reactos.org)

Safe pattern (check the type, then extract; use the _bstr_t / SysStringLen helpers):

_variant_t v = ...;        // constructed/filled by some API
v.Clear();                 // release safely if needed

if (v.vt == VT_BSTR && V_BSTR(&v) != NULL) {
    _bstr_t s(V_BSTR(&v));              // wraps the BSTR safely
    unsigned int len = SysStringLen(V_BSTR(&v));
    // use 's' or 'len' here
}

Use SysStringLen (or _bstr_t.length()) to get a BSTR’s length rather than treating the raw pointer like a C++ string. Avoid assigning NULL to an _variant_t to “reset” it — prefer v.Clear() or v = _variant_t() / default construction. These checks and patterns prevent the exact confusion that appeared in this thread. (learn.microsoft.com)

Recommended Answers

All 9 Replies

You have check _variant_t's vt member to find out the valid type of the variant. The value of vt after Clear() is called is 0, meaning that _variant_t object does not contain any valid data.

It's a very similar concept of deleting a string but not resetting the pointer to NULL. The pointer will still contain an address to some memory location where the array of characters resided but that memory location no longer is owned by the pointer and the contents of that memory location is subject to change at any time.

so thank's for u r helping nouveau dragon :) but look what i do :

-----------------------------

VariantClear(&bbvariant);
bbVariant=NULL;
if(bbVariant.vt == VT_BSTR) 
   TM_REPORT_MESSAGE(" ---> vt value is bstr ");
if((bbVariant.bstrVal).length() == 0) 
   TM_REPORT_MESSAGE(" ---> vt value is nul so i'm happy ");
 else
    TM_REPORT_MESSAGE(" ---> vt value is not null i'm frustred ");

-------------------------------------------------------------------

and the result is :

---> vt value is bstr
---> vt value is not null i'm frustred

--------------------------

no i'm inilizing the bbvariant as null so .
i supposed his member doesn't exist anywhere.!? but he is a bstr and he have a length !!! so

just for abored the last post because the result it's flase , but somone have a information or documentation for _variant_t , variantclear, variantinit , _bstr_t ? thank's for help

and the correct result is :
---------------------------------------------
---> vt value is nul so i'm happy
------------------------------------------------------

MSDN is the authority on _variant_t and VARIANT structure.

How is bbVariant declared? Is it _variant_t or VARIANT* ? If its _variant_t you don't need to call VariantClear(), but call it's Clear() method.

the bbvariant as declared as :

_variant_t bbvariant ;
and for clear variant i do :

VariantClear(&bbvariant);

i think it's right in this step , but i can't found a lot of information of _vatiant_t and his memeber methode in msdn for exemple i use
bbvaraint.bval i'm not found a documentation for this methode !???????

The docs are scarce for VARIANT types. bval is a BYTE (unsigned char) value type. You have to look in the header files where VARIANT is declared.

thank's for u help i found it , i think that he make a vt in status VT_EMPTY; i don't no what's his value but i'm going to found it , and going to make a construct response for my answers .

VariantClear() sets vt to VT_EMPTY, you don't have to do that yourself.

but in this case i do it myself , because i'm asking the variant if it is a _bstr_t and he say no , after vt is a bstr , so i init vt to vt_empty as in original case

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.