Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
COleVariant olevar;
rst.GetFieldValue(a, olevar);
CString str = (LPCTSTR)olevar.bstrVal;(carshing)


it is crashing in the str ..
when olevar is having double value. please let me know why it is crashing.

What I have tried:

COleVariant olevar;
rst.GetFieldValue(a, olevar);
CString str = (LPCTSTR)olevar.bstrVal;(carshing)
Posted
Updated 11-Aug-16 21:15pm

1 solution

COleVariant is derived from VARIANT (VARIANT and VARIANTARG Data Types [Automation][^]) which is just a union.

So you must check the vt member and access the matching data field only. If your olevar contains a double (vt is VT_R8), the corresponding field is dblVal. Accessing any other fields may result in wrong values or undefined behaviour.

You can do the conversion yourself:
C++
CString str;
if (oleVar.vt == VT_R8)
    str.Format(_T("%E"), oleVar.dblVal);

Another option is converting the variant type using COleVariant::ChangeType[^]:
oleVar.ChangeType(VT_BSTR);
CString str = oleVar.bstrVal;

Note also that no casting to LPCTSTR is used in the above example. BSTR is always Unicode and the above assignment will convert the string to ANSI with non-Unicode builds while casting to LPCTSTR would not work with non-Unicode builds (it would copy only the first character).
 
Share this answer
 
Comments
Gayle123 12-Aug-16 3:25am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900