📄 comutil.h
字号:
return *this;
}
// Assign a double creating either a VT_R8 VARIANT, or a VT_DATE
// VARIANT (VT_R8 is the default)
//
inline _variant_t& _variant_t::operator=(double dblSrc) throw(_com_error)
{
if (V_VT(this) == VT_R8) {
V_R8(this) = dblSrc;
}
else if(V_VT(this) == VT_DATE) {
V_DATE(this) = dblSrc;
}
else {
// Clear the VARIANT and create a VT_R8
//
Clear();
V_VT(this) = VT_R8;
V_R8(this) = dblSrc;
}
return *this;
}
// Assign a CY creating a VT_CY VARIANT
//
inline _variant_t& _variant_t::operator=(const CY& cySrc) throw(_com_error)
{
if (V_VT(this) != VT_CY) {
// Clear the VARIANT and create a VT_CY
//
Clear();
V_VT(this) = VT_CY;
}
V_CY(this) = cySrc;
return *this;
}
// Assign a const _bstr_t& creating a VT_BSTR VARIANT
//
inline _variant_t& _variant_t::operator=(const _bstr_t& bstrSrc) throw(_com_error)
{
// Clear the VARIANT (This will SysFreeString() any previous occupant)
//
Clear();
if (!bstrSrc) {
V_VT(this) = VT_BSTR;
V_BSTR(this) = NULL;
}
else {
BSTR bstr = static_cast<wchar_t*>(bstrSrc);
wchar_t*tmp = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
::SysStringByteLen(bstr));
if (tmp == NULL) {
_com_issue_error(E_OUTOFMEMORY);
} else {
V_VT(this) = VT_BSTR;
V_BSTR(this) = tmp;
}
}
return *this;
}
// Assign a const wchar_t* creating a VT_BSTR VARIANT
//
inline _variant_t& _variant_t::operator=(const wchar_t* pSrc) throw(_com_error)
{
// Clear the VARIANT (This will SysFreeString() any previous occupant)
//
Clear();
if (pSrc == NULL) {
V_VT(this) = VT_BSTR;
V_BSTR(this) = NULL;
}
else {
wchar_t*tmp = ::SysAllocString(pSrc);
if (tmp == NULL) {
_com_issue_error(E_OUTOFMEMORY);
} else {
V_VT(this) = VT_BSTR;
V_BSTR(this) = tmp;
}
}
return *this;
}
// Assign a const char* creating a VT_BSTR VARIANT
//
inline _variant_t& _variant_t::operator=(const char* pSrc) throw(_com_error)
{
// Clear the VARIANT (This will SysFreeString() any previous occupant)
//
Clear();
V_VT(this) = VT_BSTR;
V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
if (V_BSTR(this) == NULL && pSrc != NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
return *this;
}
// Assign an IDispatch* creating a VT_DISPATCH VARIANT
//
inline _variant_t& _variant_t::operator=(IDispatch* pSrc) throw(_com_error)
{
// Clear the VARIANT (This will Release() any previous occupant)
//
Clear();
V_VT(this) = VT_DISPATCH;
V_DISPATCH(this) = pSrc;
// Need the AddRef() as VariantClear() calls Release()
//
V_DISPATCH(this)->AddRef();
return *this;
}
// Assign a bool creating a VT_BOOL VARIANT
//
inline _variant_t& _variant_t::operator=(bool bSrc) throw(_com_error)
{
if (V_VT(this) != VT_BOOL) {
// Clear the VARIANT and create a VT_BOOL
//
Clear();
V_VT(this) = VT_BOOL;
}
V_BOOL(this) = (bSrc ? VARIANT_TRUE : VARIANT_FALSE);
return *this;
}
// Assign an IUnknown* creating a VT_UNKNOWN VARIANT
//
inline _variant_t& _variant_t::operator=(IUnknown* pSrc) throw(_com_error)
{
// Clear VARIANT (This will Release() any previous occupant)
//
Clear();
V_VT(this) = VT_UNKNOWN;
V_UNKNOWN(this) = pSrc;
// Need the AddRef() as VariantClear() calls Release()
//
V_UNKNOWN(this)->AddRef();
return *this;
}
// Assign a DECIMAL creating a VT_DECIMAL VARIANT
//
inline _variant_t& _variant_t::operator=(const DECIMAL& decSrc) throw(_com_error)
{
if (V_VT(this) != VT_DECIMAL) {
// Clear the VARIANT
//
Clear();
}
// Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
V_DECIMAL(this) = decSrc;
V_VT(this) = VT_DECIMAL;
return *this;
}
// Assign a BTYE (unsigned char) creating a VT_UI1 VARIANT
//
inline _variant_t& _variant_t::operator=(BYTE bSrc) throw(_com_error)
{
if (V_VT(this) != VT_UI1) {
// Clear the VARIANT and create a VT_UI1
//
Clear();
V_VT(this) = VT_UI1;
}
V_UI1(this) = bSrc;
return *this;
}
// Assign a LONGLONG creating a VT_I8 VARIANT
//
inline _variant_t& _variant_t::operator=(LONGLONG llSrc) throw(_com_error)
{
if (V_VT(this) != VT_I8) {
// Clear the VARIANT and create a VT_I8
//
Clear();
V_VT(this) = VT_I8;
}
V_I8(this) = llSrc;
return *this;
}
// Assign a ULONGLONG creating a VT_UI8 VARIANT
//
inline _variant_t& _variant_t::operator=(ULONGLONG ullSrc) throw(_com_error)
{
if (V_VT(this) != VT_UI8) {
// Clear the VARIANT and create a VT_UI8
//
Clear();
V_VT(this) = VT_UI8;
}
V_UI8(this) = ullSrc;
return *this;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// Comparison operations
//
//////////////////////////////////////////////////////////////////////////////////////////
// Compare a _variant_t against a const VARIANT& for equality
//
inline bool _variant_t::operator==(const VARIANT& varSrc) const throw()
{
return *this == &varSrc;
}
// Compare a _variant_t against a const VARIANT* for equality
//
inline bool _variant_t::operator==(const VARIANT* pSrc) const throw()
{
if (this == pSrc) {
return true;
}
//
// Variants not equal if types don't match
//
if (V_VT(this) != V_VT(pSrc)) {
return false;
}
//
// Check type specific values
//
switch (V_VT(this)) {
case VT_EMPTY:
case VT_NULL:
return true;
case VT_I2:
return V_I2(this) == V_I2(pSrc);
case VT_I4:
return V_I4(this) == V_I4(pSrc);
case VT_I8:
return V_I8(this) == V_I8(pSrc);
case VT_R4:
return V_R4(this) == V_R4(pSrc);
case VT_R8:
return V_R8(this) == V_R8(pSrc);
case VT_CY:
return memcmp(&(V_CY(this)), &(V_CY(pSrc)), sizeof(CY)) == 0;
case VT_DATE:
return V_DATE(this) == V_DATE(pSrc);
case VT_BSTR:
return (::SysStringByteLen(V_BSTR(this)) == ::SysStringByteLen(V_BSTR(pSrc))) &&
(memcmp(V_BSTR(this), V_BSTR(pSrc), ::SysStringByteLen(V_BSTR(this))) == 0);
case VT_DISPATCH:
return V_DISPATCH(this) == V_DISPATCH(pSrc);
case VT_ERROR:
return V_ERROR(this) == V_ERROR(pSrc);
case VT_BOOL:
return V_BOOL(this) == V_BOOL(pSrc);
case VT_UNKNOWN:
return V_UNKNOWN(this) == V_UNKNOWN(pSrc);
case VT_DECIMAL:
return memcmp(&(V_DECIMAL(this)), &(V_DECIMAL(pSrc)), sizeof(DECIMAL)) == 0;
case VT_UI1:
return V_UI1(this) == V_UI1(pSrc);
default:
_com_issue_error(E_INVALIDARG);
// fall through
}
return false;
}
// Compare a _variant_t against a const VARIANT& for in-equality
//
inline bool _variant_t::operator!=(const VARIANT& varSrc) const throw()
{
return !(*this == &varSrc);
}
// Compare a _variant_t against a const VARIANT* for in-equality
//
inline bool _variant_t::operator!=(const VARIANT* pSrc) const throw()
{
return !(*this == pSrc);
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// Low-level operations
//
//////////////////////////////////////////////////////////////////////////////////////////
// Clear the _variant_t
//
inline void _variant_t::Clear() throw(_com_error)
{
_com_util::CheckError(::VariantClear(this));
}
inline void _variant_t::Attach(VARIANT& varSrc) throw(_com_error)
{
//
// Free up previous VARIANT
//
Clear();
//
// Give control of data to _variant_t
//
memcpy(this, &varSrc, sizeof(varSrc));
V_VT(&varSrc) = VT_EMPTY;
}
inline VARIANT _variant_t::Detach() throw(_com_error)
{
VARIANT varResult = *this;
V_VT(this) = VT_EMPTY;
return varResult;
}
// Change the type and contents of this _variant_t to the type vartype and
// contents of pSrc
//
inline void _variant_t::ChangeType(VARTYPE vartype, const _variant_t* pSrc) throw(_com_error)
{
//
// If pDest is NULL, convert type in place
//
if (pSrc == NULL) {
pSrc = this;
}
if ((this != pSrc) || (vartype != V_VT(this))) {
_com_util::CheckError(::VariantChangeType(static_cast<VARIANT*>(this),
const_cast<VARIANT*>(static_cast<const VARIANT*>(pSrc)),
0, vartype));
}
}
inline void _variant_t::SetString(const char* pSrc) throw(_com_error)
{
//
// Free up previous VARIANT
//
Clear();
V_VT(this) = VT_BSTR;
V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
if (V_BSTR(this) == NULL && pSrc != NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// Destructor
//
//////////////////////////////////////////////////////////////////////////////////////////
inline _variant_t::~_variant_t() throw(_com_error)
{
_com_util::CheckError(::VariantClear(this));
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// Mutually-dependent definitions
//
//////////////////////////////////////////////////////////////////////////////////////////
// Construct a _bstr_t from a const _variant_t&
//
inline _bstr_t::_bstr_t(const _variant_t &var) throw(_com_error)
: m_Data(NULL)
{
if (V_VT(&var) == VT_BSTR) {
*this = V_BSTR(&var);
return;
}
_variant_t varDest;
varDest.ChangeType(VT_BSTR, &var);
*this = V_BSTR(&varDest);
}
// Assign a const _variant_t& to a _bstr_t
//
inline _bstr_t& _bstr_t::operator=(const _variant_t &var) throw(_com_error)
{
if (V_VT(&var) == VT_BSTR) {
*this = V_BSTR(&var);
return *this;
}
_variant_t varDest;
varDest.ChangeType(VT_BSTR, &var);
*this = V_BSTR(&varDest);
return *this;
}
extern _variant_t vtMissing;
#ifndef _USE_RAW
#define bstr_t _bstr_t
#define variant_t _variant_t
#endif
#if _MSC_VER >= 1200
#pragma warning(pop)
#endif
#endif // _INC_COMUTIL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -