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:
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).