_bstr_t和_variant_t是怎样定义的
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了_bstr_t和_variant_t是怎样定义的相关的知识,希望对你有一定的参考价值。
不要告诉我怎样使用。我只需要知道它们定义的源码
_bstr_t的定义:COMUTIL.Hclass _bstr_t
public:
// Constructors
//
_bstr_t() throw();
_bstr_t(const _bstr_t& s) throw();
_bstr_t(const char* s) throw(_com_error);
_bstr_t(const wchar_t* s) throw(_com_error);
_bstr_t(const _variant_t& var) throw(_com_error);
_bstr_t(BSTR bstr, bool fCopy) throw(_com_error);
// Destructor
//
~_bstr_t() throw();
// Assignment operators
//
_bstr_t& operator=(const _bstr_t& s) throw();
_bstr_t& operator=(const char* s) throw(_com_error);
_bstr_t& operator=(const wchar_t* s) throw(_com_error);
_bstr_t& operator=(const _variant_t& var) throw(_com_error);
// Operators
//
_bstr_t& operator+=(const _bstr_t& s) throw(_com_error);
_bstr_t operator+(const _bstr_t& s) const throw(_com_error);
// Friend operators
//
friend _bstr_t operator+(const char* s1, const _bstr_t& s2) throw(_com_error);
friend _bstr_t operator+(const wchar_t* s1, const _bstr_t& s2) throw(_com_error);
// Extractors
//
operator const wchar_t*() const throw();
operator wchar_t*() const throw();
operator const char*() const throw(_com_error);
operator char*() const throw(_com_error);
// Comparison operators
//
bool operator!() const throw();
bool operator==(const _bstr_t& str) const throw();
bool operator!=(const _bstr_t& str) const throw();
bool operator<(const _bstr_t& str) const throw();
bool operator>(const _bstr_t& str) const throw();
bool operator<=(const _bstr_t& str) const throw();
bool operator>=(const _bstr_t& str) const throw();
// Low-level helper functions
//
BSTR copy() const throw(_com_error);
unsigned int length() const throw();
// Binary string assign
//
void Assign(BSTR s) throw(_com_error);
private:
// Referenced counted wrapper
//
class Data_t
public:
// Constructors
//
Data_t(const char* s) throw(_com_error);
Data_t(const wchar_t* s) throw(_com_error);
Data_t(BSTR bstr, bool fCopy) throw(_com_error);
Data_t(const _bstr_t& s1, const _bstr_t& s2) throw(_com_error);
// Reference counting routines
//
unsigned long AddRef() throw();
unsigned long Release() throw();
// Extractors
//
operator const wchar_t*() const throw();
operator const char*() const throw(_com_error);
// Low-level helper functions
//
const wchar_t* GetWString() const throw();
const char* GetString() const throw(_com_error);
BSTR Copy() const throw(_com_error);
void Assign(BSTR s) throw(_com_error);
unsigned int Length() const throw();
int Compare(const Data_t& str) const throw();
private:
wchar_t* m_wstr;
mutable char* m_str;
unsigned long m_RefCount;
// Never allow default construction
//
Data_t() throw();
// Never allow copy
//
Data_t(const Data_t& s) throw();
// Prevent deletes from outside. Release() must be used.
//
~Data_t() throw();
void _Free() throw();
;
private:
// Reference counted representation
//
Data_t* m_Data;
private:
// Low-level utilities
//
void _AddRef() throw();
void _Free() throw();
int _Compare(const _bstr_t& str) const throw();
;
_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
; 参考技术A _variant_t和_bstr_t这两个类分别封装并管理VARIANT和BSTR这两种数据类型,VARIANT和BSTR这两种类型是COM中使用的数据类型。为了C++中的变量应用到ADO编程中,只能进行数据类型的转换。_variant_t和_bstr_t这两个类,就可以方便的把C++类型变量转换成COM中的变量了
参考资料:http://zhidao.baidu.com/question/86443910.html?si=2
参考技术B 在sys/types.h头文件中C++ 和 .NET - 从 'System::String ^' 转换为 '_variant_t'
【中文标题】C++ 和 .NET - 从 \'System::String ^\' 转换为 \'_variant_t\'【英文标题】:C++ and .NET - Converting from 'System::String ^' to '_variant_t'C++ 和 .NET - 从 'System::String ^' 转换为 '_variant_t' 【发布时间】:2014-01-07 01:22:11 【问题描述】:我正在开发一个托管 C++ 应用程序,该应用程序利用 C# 库来填充 ADO 记录集中的字段:
recordset->Fields->GetItem(L"Id")->Value = _variant_t(Library::IdGenerator->GenerateNewId());
但是,在将库返回的 .NET 字符串转换为 _variant_t
之前,我在将其添加到记录集之前遇到了错误。
这是我得到的错误:
error C2440: '<function-style-cast>' : cannot convert from 'System::String ^' to '_variant_t'
我是否错过了转换或强制转换以使其正常工作?
【问题讨论】:
【参考方案1】:是的,需要转换。 _variant_t
类不是一个非常愉快的匹配,由于一个深不可测的原因,它缺少一个采用 BSTR 的构造函数,采用 _bstr_t
的构造函数没有吸引力,因为它复制了字符串。回退到原生 VARIANT 类型,如下所示:
using namespace System::Runtime::InteropServices;
...
String^ s = Library::IdGenerator->GenerateNewId();
VARIANT v = VT_BSTR ;
v.bstrVal = (BSTR)Marshal::StringToBSTR(s).ToPointer();
recordset->Fields->GetItem(L"Id")->Value = v;
【讨论】:
工作就像一个魅力!非常感谢:)以上是关于_bstr_t和_variant_t是怎样定义的的主要内容,如果未能解决你的问题,请参考以下文章
CComVariant 与 _variant_t、CComBSTR 与 _bstr_t