I have a COleVariant which is set to VT_NULL, when i try and convert the bstrVal member to a CString I get garbage. In older versions of my program however I do not get garbage, I get an empty CString which is my desired behaviour. Here is a simplified version of the offending code:
COleVariant myCOleVariant;
myCOleVariant.vt = VT_NULL;
CString myCString = myCOleVariant.bstrVal;
myCOleVariant is set from a database field which can contain a string or could contain NULL. The databse contains mixed types hence using COleVariant. In the specific case which is causing my problems, the database field is NULL and the COleVariant before the conversion to CString is set as VT_NULL.
A simple workaround is to add a function:
CString COleVariantAsCString(COleVariant pVar)
{
CString retval;
if(pVar.vt == VT_NULL || pVar.vt == VT_EMPTY)
retval = _T("");
else
retval = pVar.bstrVal;
return retval;
}
My question is this: Has anything changed in recent versions of visual studio or in windows 7 that would affect the way that COleVariants work? I use COlevariants a lot in my program and am worried that there could be other changes which will cause bugs like this.
I am using VS 2008 professional edition. Any input or ideas would be appreciated as I am quite perplexed by this unexplained change in behaviour. Apologies for the vaugue question. If i knew enough to ask it properly, I probably wouldnt need to ask it.
There is simply no guarantee that when VARIANT.vt == VT_EMPTY or VARIANT.vt == VT_NULL the rest of fields are zeroed.
You workaround function is the right approach. I would only recommend to use const COleVariant& as parameter to avoid unnecessary COleVariant object coping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With