_variant_t

_variant_t和_bstr_t這兩個類分別封裝並管理VARIANT和BSTR這兩種數據類型,VARIANT和BSTR這兩種類型是COM中使用的數據類型。

簡介

為了C++中的變數套用到ADO編程中,只能進行數據類型的轉換。

通過_variant_t和_bstr_t這兩個類,就可以方便的把C++類型變數轉換成COM中的變數了。

定義

_variant_t的定義: COMUTIL.H

class _variant_t : public ::tagVARIANT {

public:

// Constructors

//

_variant_t() throw();

_variant_t(const VARIANT& varSrc) throw(_com_error);

_variant_t(const VARIANT* pSrc) throw(_com_error);

_variant_t(const _variant_t& varSrc) throw(_com_error);

_variant_t(VARIANT& varSrc, bool fCopy) throw(_com_error); // Attach VARIANT if !fCopy

_variant_t(short sSrc, VARTYPE vtSrc = VT_I2) throw(_com_error); // Creates a VT_I2, or a VT_BOOL

_variant_t(long lSrc, VARTYPE vtSrc = VT_I4) throw(_com_error); // Creates a VT_I4, a VT_ERROR, or a VT_BOOL

_variant_t(float fltSrc) throw(); // Creates a VT_R4

_variant_t(double dblSrc, VARTYPE vtSrc = VT_R8) throw(_com_error); // Creates a VT_R8, or a VT_DATE

_variant_t(const CY& cySrc) throw(); // Creates a VT_CY

_variant_t(const _bstr_t& bstrSrc) throw(_com_error); // Creates a VT_BSTR

_variant_t(const wchar_t*pSrc) throw(_com_error); // Creates a VT_BSTR

_variant_t(const char* pSrc) throw(_com_error); // Creates a VT_BSTR

_variant_t(IDispatch* pSrc, bool fAddRef = true) throw(); // Creates a VT_DISPATCH

_variant_t(bool bSrc) throw(); // Creates a VT_BOOL

_variant_t(IUnknown* pSrc, bool fAddRef = true) throw(); // Creates a VT_UNKNOWN

_variant_t(const DECIMAL& decSrc) throw(); // Creates a VT_DECIMAL

_variant_t(BYTE bSrc) throw(); // Creates a VT_UI1

// Destructor

//

~_variant_t() throw(_com_error);

// Extractors

//

operator short() const throw(_com_error); // Extracts a short from a VT_I2

operator long() const throw(_com_error); // Extracts a long from a VT_I4

operator float() const throw(_com_error); // Extracts a float from a VT_R4

operator double() const throw(_com_error); // Extracts a double from a VT_R8

operator CY() const throw(_com_error); // Extracts a CY from a VT_CY

operator _bstr_t() const throw(_com_error); // Extracts a _bstr_t from a VT_BSTR

operator IDispatch*() const throw(_com_error); // Extracts a IDispatch* from a VT_DISPATCH

operator bool() const throw(_com_error); // Extracts a bool from a VT_BOOL

operator IUnknown*() const throw(_com_error); // Extracts a IUnknown* from a VT_UNKNOWN

operator DECIMAL() const throw(_com_error); // Extracts a DECIMAL from a VT_DECIMAL

operator BYTE() const throw(_com_error); // Extracts a BTYE (unsigned char) from a VT_UI1

// Assignment operations

//

_variant_t& operator=(const VARIANT& varSrc) throw(_com_error);

_variant_t& operator=(const VARIANT* pSrc) throw(_com_error);

_variant_t& operator=(const _variant_t& varSrc) throw(_com_error);

_variant_t& operator=(short sSrc) throw(_com_error); // Assign a VT_I2, or a VT_BOOL

_variant_t& operator=(long lSrc) throw(_com_error); // Assign a VT_I4, a VT_ERROR or a VT_BOOL

_variant_t& operator=(float fltSrc) throw(_com_error); // Assign a VT_R4

_variant_t& operator=(double dblSrc) throw(_com_error); // Assign a VT_R8, or a VT_DATE

_variant_t& operator=(const CY& cySrc) throw(_com_error); // Assign a VT_CY

_variant_t& operator=(const _bstr_t& bstrSrc) throw(_com_error); // Assign a VT_BSTR

_variant_t& operator=(const wchar_t* pSrc) throw(_com_error); // Assign a VT_BSTR

_variant_t& operator=(const char* pSrc) throw(_com_error); // Assign a VT_BSTR

_variant_t& operator=(IDispatch* pSrc) throw(_com_error); // Assign a VT_DISPATCH

_variant_t& operator=(bool bSrc) throw(_com_error); // Assign a VT_BOOL

_variant_t& operator=(IUnknown* pSrc) throw(_com_error); // Assign a VT_UNKNOWN

_variant_t& operator=(const DECIMAL& decSrc) throw(_com_error); // Assign a VT_DECIMAL

_variant_t& operator=(BYTE bSrc) throw(_com_error); // Assign a VT_UI1

// Comparison operations

//

bool operator==(const VARIANT& varSrc) const throw(_com_error);

bool operator==(const VARIANT* pSrc) const throw(_com_error);

bool operator!=(const VARIANT& varSrc) const throw(_com_error);

bool operator!=(const VARIANT* pSrc) const throw(_com_error);

// Low-level operations

//

void Clear() throw(_com_error);

void Attach(VARIANT& varSrc) throw(_com_error);

VARIANT Detach() throw(_com_error);

void ChangeType(VARTYPE vartype, const _variant_t* pSrc = NULL) throw(_com_error);

void SetString(const char* pSrc) throw(_com_error); // used to set ANSI string

};

案例

我定義了一組函式可以方便的實現_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;

}

}

功能

實現_variant_t向int,String ,double,long,byte等類型轉換

相關詞條

熱門詞條

聯絡我們