_variant_t的案例
我定义了一组函数可以方便的实现_variant_t类型转换
//_variant_t转WORD类型
BOOL AfxVariantConvert(WORD& val, const _variant_t& vt)
{
if (vt.vt == VT_UI2)
{
val = (WORD)vt.uiVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
_variant_t转DWORD类型
BOOL AfxVariantConvert(DWORD& val, const _variant_t& vt)
{
if (vt.vt == VT_UI4 )
{
val = (DWORD)vt.ulVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
_variant_t转DWORD64类型
BOOL AfxVariantConvert(DWORD64& val, const _variant_t& vt)
{
if (vt.vt != VT_UI8 )
{
val = (DWORD64)vt.ullVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
_variant_t转CString类型
BOOL AfxVariantConvert(CString& val, const _variant_t& vt)
{
if (vt.vt == VT_BSTR)
{
val = (LPCTSTR)vt.bstrVal;
return TRUE;
}
else
{
val = _T();
return FALSE;
}
}
_variant_t转BOOL类型
BOOL AfxVariantConvert(BOOL& val, const _variant_t& vt)
{
if (vt.vt == VT_BOOL)
{
val = vt.boolVal;
return TRUE;
}
else
{
val = FALSE;
return FALSE;
}
}
_variant_t转CTime类型(精度变短,可能丢失数据)
BOOL AfxVariantConvert(CTime& val, const _variant_t& vt)
{
if (vt.vt == VT_DATE)
{
val = vt.date;
return TRUE;
}
else
{
val = NULL;
return FALSE;
}
}
_variant_t转double类型
BOOL AfxVariantConvert(DOUBLE& val, const _variant_t& vt)
{
if (vt.vt == VT_R8)
{
val = vt.dblVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
_variant_t转byte类型
BOOL AfxVariantConvert(BYTE& val, const _variant_t& vt)
{
if (vt.vt == VT_UI1 || vt.vt == VT_I1)
{
val = vt.bVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
_variant_t转float类型
BOOL AfxVariantConvert( FLOAT& val, const _variant_t& vt )
{
if (vt.vt == VT_R4)
{
val = vt.fltVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
_variant_t转long类型
BOOL AfxVariantConvert( long& val,const _variant_t& vt )
{
if (vt.vt == VT_I4)
{
val = vt.lVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
_variant_t转INT64类型
BOOL AfxVariantConvert( INT64& val,const _variant_t& vt )
{
if (vt.vt == VT_I8)
{
val = vt.llVal;
return TRUE;
}
else
{
val = 0;
return FALSE;
}
}
2024-10-13 广告