请输入您要查询的百科知识:

 

词条 _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等类型转换

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/11/15 20:57:20