/*
将 _variant_t 类型的变量,转换成字符串(比如如果是数字,则转换成数字对应的字符串 :如 10.2 -> "10.200000")
*/
_variant_t var;
CString str; //转换以后的字符串
//以下代码演示如何转换为C标准字符串型
if (var.vt == VT_I4)
{
long lNum;
char szCh[21];
str=var.bstrVal;
WideCharToMultiByte
(CP_ACP, 0, var.bstrVal, -1,
szCh, sizeof(szCh), NULL, NULL);
}
//以下代码演示如何转换成逻辑型
if( var.vt == VT_BOOL)
{
BOOL bVar;
lNum=var.lVal;
bVar= var.boolVar==0? FALSE : TRUE;
}
//以下代码演示为其余类型(补充)
switch(var.vt)
{
case VT_BSTR: //var is BSTR type
str=var.bstrVal;
break;
case VT_I2: //var is short int type
str.Format("%d",(int)var.iVal);
break;
case VT_I4: //var is long int type
str.Format("%d",var.lVal);
break;
case VT_R4: //var is float type
str.Format("%10.6f",(double)var.fltVal);
break;
case VT_R8: //var is double type
str.Format("%10.6f",var.dblVal);
break;
case VT_CY: //var is CY type
str=COleCurrency(var).Format();
break;
case VT_DATE: //var is DATE type
str=COleDateTime(var).Format();
break;
case VT_BOOL: //var is VARIANT_BOOL
str= (var.boolVal==0) ?"FALSE": "TRUE";
break;
default:
str.Format("Unk type %d\n",var.vt);
TRACE("Unknown type %d\n",var.vt);
}