short vartoi(const _variant_t& var)
{
short value = 0;
switch (var.vt)
{
case VT_BOOL:
value = var.boolVal;
break;
case VT_UI1:
case VT_I1:
value = var.bVal;
break;
case VT_I2:
case VT_UI2:
value = var.iVal;
break;
case VT_NULL:
case VT_EMPTY:
value = 0;
break;
default:
TRACE(_T("Warning: 未处理的 _variant_t 类型值; 文件: %s; 行: %d\n"), __FILE__, __LINE__);
}
return value;
}
long vartol(const _variant_t& var)
{
long value = 0;
switch (var.vt)
{
case VT_BOOL:
value = var.boolVal;
break;
case VT_UI1:
case VT_I1:
value = var.bVal;
break;
case VT_UI2:
case VT_I2:
value = var.iVal;
break;
case VT_I4:
case VT_UI4:
value = var.lVal;
break;
case VT_INT:
value = var.intVal;
break;
case VT_R4:
value = (long)(var.fltVal + 0.5);
break;
case VT_R8:
value = (long)(var.dblVal + 0.5);
break;
case VT_DECIMAL:
value = (long)var;
break;
case VT_CY:
value = (long)var;
break;
case VT_BSTR://字符串
case VT_LPSTR://字符串
case VT_LPWSTR://字符串
value = atol((LPCTSTR)(_bstr_t)var);
break;
case VT_NULL:
case VT_EMPTY:
value = 0;
break;
default:
TRACE(_T("Warning: 未处理的 _variant_t 类型值; 文件: %s; 行: %d\n"), __FILE__, __LINE__);
}
return value;
}
double vartof(const _variant_t& var)
{
double value = 0;
switch (var.vt)
{
case VT_R4:
value = var.fltVal;
break;
case VT_R8:
value = var.dblVal;
break;
case VT_DECIMAL:
value = (double)var;
break;
case VT_CY:
value = (double)var;
break;
case VT_BOOL:
value = var.boolVal;
break;
case VT_UI1:
case VT_I1:
value = var.bVal;
break;
case VT_UI2:
case VT_I2:
value = var.iVal;
break;
case VT_UI4:
case VT_I4:
value = var.lVal;
break;
case VT_INT:
value = var.intVal;
break;
case VT_BSTR://字符串
case VT_LPSTR://字符串
case VT_LPWSTR://字符串
value = atof((LPCTSTR)(_bstr_t)var);
break;
case VT_NULL:
case VT_EMPTY:
value = 0;
break;
default:
value = 0;
TRACE(_T("Warning: 未处理的 _variant_t 类型值; 文件: %s; 行: %d\n"), __FILE__, __LINE__);
}
return value;
}
CString COMSApp::VarToCString(_variant_t var)
{
CString strValue;
_variant_t var_t;
_bstr_t bstr_t;
time_t cur_time;
CTime time_value;
COleCurrency var_currency;
switch(var.vt)
{
case VT_EMPTY:
case VT_NULL:strValue=_T("");break;
case VT_UI1:strValue.Format("%d",var.bVal);break;
case VT_I2:strValue.Format("%d",var.iVal);break;
case VT_I4:strValue.Format("%d",var.lVal);break;
case VT_R4:strValue.Format("%f",var.fltVal);break;
case VT_R8:strValue.Format("%f",var.dblVal);break;
case VT_CY:
var_currency=var;
strValue=var_currency.Format(0);break;
case VT_BSTR:
var_t =var;
bstr_t=var_t;
strValue.Format("%s",(const char *)bstr_t);break;
case VT_DATE:
cur_time=var.date;
time_value=cur_time;
strValue.Format("%A,%B,%d,%Y");break;
case VT_BOOL:strValue.Format("%d",var.boolVal);break;
default:strValue=_T("");break;
}
return strValue;
}