📄 comutil.h
字号:
// Compare two _bstr_t objects
//
inline int _bstr_t::_Compare(const _bstr_t& str) const throw()
{
if (m_Data == str.m_Data) {
return 0;
}
if (m_Data == NULL) {
return -1;
}
if (str.m_Data == NULL) {
return 1;
}
return m_Data->Compare(*str.m_Data);
}
//////////////////////////////////////////////////////////////////////////////
//
// Reference counted wrapper - Constructors
//
//////////////////////////////////////////////////////////////////////////////
// Construct a Data_t from a const char*
//
inline _bstr_t::Data_t::Data_t(const char* s) throw(_com_error)
: m_str(NULL), m_RefCount(1)
{
m_wstr = _com_util::ConvertStringToBSTR(s);
if (m_wstr == NULL && s != NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
// Construct a Data_t from a const wchar_t*
//
inline _bstr_t::Data_t::Data_t(const wchar_t* s) throw(_com_error)
: m_str(NULL), m_RefCount(1)
{
m_wstr = ::SysAllocString(s);
if (m_wstr == NULL && s != NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
// Construct a Data_t from a BSTR. If fCopy is FALSE, give control of
// data to the Data_t without doing a SysAllocStringByteLen.
//
inline _bstr_t::Data_t::Data_t(BSTR bstr, bool fCopy) throw(_com_error)
: m_str(NULL), m_RefCount(1)
{
if (fCopy && bstr != NULL) {
m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
::SysStringByteLen(bstr));
if (m_wstr == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
else {
m_wstr = bstr;
}
}
// Construct a Data_t from the concatenation of two _bstr_t objects
//
inline _bstr_t::Data_t::Data_t(const _bstr_t& s1, const _bstr_t& s2) throw(_com_error)
: m_str(NULL), m_RefCount(1)
{
const unsigned int l1 = s1.length();
const unsigned int l2 = s2.length();
m_wstr = ::SysAllocStringByteLen(NULL, (l1 + l2) * sizeof(wchar_t));
if (m_wstr == NULL) {
if (l1 + l2 == 0) {
return;
}
_com_issue_error(E_OUTOFMEMORY);
}
const wchar_t* wstr1 = static_cast<const wchar_t*>(s1);
if (wstr1 != NULL) {
memcpy(m_wstr, wstr1, (l1 + 1) * sizeof(wchar_t));
}
const wchar_t* wstr2 = static_cast<const wchar_t*>(s2);
if (wstr2 != NULL) {
memcpy(m_wstr + l1, wstr2, (l2 + 1) * sizeof(wchar_t));
}
}
//////////////////////////////////////////////////////////////////////////////
//
// Reference counted wrapper - reference counting routines
//
//////////////////////////////////////////////////////////////////////////////
inline unsigned long _bstr_t::Data_t::AddRef() throw()
{
InterlockedIncrement(reinterpret_cast<long*>(&m_RefCount));
return m_RefCount;
}
inline unsigned long _bstr_t::Data_t::Release() throw()
{
if (!InterlockedDecrement(reinterpret_cast<long*>(&m_RefCount))) {
delete this;
return 0;
}
return m_RefCount;
}
//////////////////////////////////////////////////////////////////////////////
//
// Reference counted wrapper - extractors
//
//////////////////////////////////////////////////////////////////////////////
// Extract a const wchar_t*
//
inline _bstr_t::Data_t::operator const wchar_t*() const throw()
{
return m_wstr;
}
// Extract a const char_t*
//
inline _bstr_t::Data_t::operator const char*() const throw(_com_error)
{
return GetString();
}
//////////////////////////////////////////////////////////////////////////////
//
// Reference counted wrapper - helper functions
//
//////////////////////////////////////////////////////////////////////////////
inline const wchar_t* _bstr_t::Data_t::GetWString() const throw()
{
return m_wstr;
}
inline const char* _bstr_t::Data_t::GetString() const throw(_com_error)
{
if (m_str == NULL) {
m_str = _com_util::ConvertBSTRToString(m_wstr);
if (m_str == NULL && m_wstr != NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
return m_str;
}
// Return a copy of the wrapped BSTR
//
inline BSTR _bstr_t::Data_t::Copy() const throw(_com_error)
{
if (m_wstr != NULL) {
BSTR bstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(m_wstr),
::SysStringByteLen(m_wstr));
if (bstr == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
return bstr;
}
return NULL;
}
inline void _bstr_t::Data_t::Assign(BSTR s) throw(_com_error)
{
_Free();
if (s != NULL) {
wchar_t* tmp = ::SysAllocStringByteLen(reinterpret_cast<char*>(s),
::SysStringByteLen(s));
if (tmp == NULL) {
_com_issue_error(E_OUTOFMEMORY);
} else {
m_wstr = tmp;
}
}
}
// Return the length of the wrapper BSTR
//
inline unsigned int _bstr_t::Data_t::Length() const throw()
{
return m_wstr ? ::SysStringLen(m_wstr) : 0;
}
// Compare two wrapped BSTRs
//
inline int _bstr_t::Data_t::Compare(const _bstr_t::Data_t& str) const throw()
{
if (m_wstr == NULL) {
return str.m_wstr ? -1 : 0;
}
if (str.m_wstr == NULL) {
return 1;
}
const unsigned int l1 = ::SysStringLen(m_wstr);
const unsigned int l2 = ::SysStringLen(str.m_wstr);
unsigned int len = l1;
if (len > l2) {
len = l2;
}
BSTR bstr1 = m_wstr;
BSTR bstr2 = str.m_wstr;
while (len-- > 0) {
if (*bstr1++ != *bstr2++) {
return bstr1[-1] - bstr2[-1];
}
}
return (l1 < l2) ? -1 : (l1 == l2) ? 0 : 1;
}
// Destruct this object
//
inline _bstr_t::Data_t::~Data_t() throw()
{
_Free();
}
// Free up this object
//
inline void _bstr_t::Data_t::_Free() throw()
{
if (m_wstr != NULL) {
::SysFreeString(m_wstr);
}
if (m_str != NULL) {
delete [] m_str;
}
}
//////////////////////////////////////////////////////////////////////////////
//
// Wrapper class for VARIANT
//
//////////////////////////////////////////////////////////////////////////////
/*
* VARENUM usage key,
*
* * [V] - may appear in a VARIANT
* * [T] - may appear in a TYPEDESC
* * [P] - may appear in an OLE property set
* * [S] - may appear in a Safe Array
* * [C] - supported by class _variant_t
*
*
* VT_EMPTY [V] [P] nothing
* VT_NULL [V] [P] SQL style Null
* VT_I2 [V][T][P][S][C] 2 byte signed int
* VT_I4 [V][T][P][S][C] 4 byte signed int
* VT_R4 [V][T][P][S][C] 4 byte real
* VT_R8 [V][T][P][S][C] 8 byte real
* VT_CY [V][T][P][S][C] currency
* VT_DATE [V][T][P][S][C] date
* VT_BSTR [V][T][P][S][C] OLE Automation string
* VT_DISPATCH [V][T][P][S][C] IDispatch *
* VT_ERROR [V][T] [S][C] SCODE
* VT_BOOL [V][T][P][S][C] True=-1, False=0
* VT_VARIANT [V][T][P][S] VARIANT *
* VT_UNKNOWN [V][T] [S][C] IUnknown *
* VT_DECIMAL [V][T] [S][C] 16 byte fixed point
* VT_I1 [T] signed char
* VT_UI1 [V][T][P][S][C] unsigned char
* VT_UI2 [T][P] unsigned short
* VT_UI4 [T][P] unsigned short
* VT_I8 [T][P] signed 64-bit int
* VT_UI8 [T][P] unsigned 64-bit int
* VT_INT [T] signed machine int
* VT_UINT [T] unsigned machine int
* VT_VOID [T] C style void
* VT_HRESULT [T] Standard return type
* VT_PTR [T] pointer type
* VT_SAFEARRAY [T] (use VT_ARRAY in VARIANT)
* VT_CARRAY [T] C style array
* VT_USERDEFINED [T] user defined type
* VT_LPSTR [T][P] null terminated string
* VT_LPWSTR [T][P] wide null terminated string
* VT_FILETIME [P] FILETIME
* VT_BLOB [P] Length prefixed bytes
* VT_STREAM [P] Name of the stream follows
* VT_STORAGE [P] Name of the storage follows
* VT_STREAMED_OBJECT [P] Stream contains an object
* VT_STORED_OBJECT [P] Storage contains an object
* VT_BLOB_OBJECT [P] Blob contains an object
* VT_CF [P] Clipboard format
* VT_CLSID [P] A Class ID
* VT_VECTOR [P] simple counted array
* VT_ARRAY [V] SAFEARRAY*
* VT_BYREF [V] void* for local use
*/
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
_variant_t(LONGLONG llSrc) throw(); // Creates a VT_I8
_variant_t(ULONGLONG ullSrc) throw(); // Creates a VT_UI8
// 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
operator LONGLONG() const throw(_com_error); // Extracts a LONGLONG from a VT_I8
operator ULONGLONG() const throw(_com_error); // Extracts a ULONGLONG from a VT_UI8
// 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
_variant_t& operator=(LONGLONG llSrc) throw(_com_error); // Assign a VT_I8
_variant_t& operator=(ULONGLONG ullSrc) throw(_com_error); // Assign a VT_UI8
// 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
};
//////////////////////////////////////////////////////////////////////////////////////////
//
// Constructors
//
//////////////////////////////////////////////////////////////////////////////////////////
// Default constructor
//
inline _variant_t::_variant_t() throw()
{
::VariantInit(this);
}
// Construct a _variant_t from a const VARIANT&
//
inline _variant_t::_variant_t(const VARIANT& varSrc) throw(_com_error)
{
::VariantInit(this);
_com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
}
// Construct a _variant_t from a const VARIANT*
//
inline _variant_t::_variant_t(const VARIANT* pSrc) throw(_com_error)
{
::VariantInit(this);
_com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
}
// Construct a _variant_t from a const _variant_t&
//
inline _variant_t::_variant_t(const _variant_t& varSrc) throw(_com_error)
{
::VariantInit(this);
_com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
}
// Construct a _variant_t from a VARIANT&. If fCopy is FALSE, give control of
// data to the _variant_t without doing a VariantCopy.
//
inline _variant_t::_variant_t(VARIANT& varSrc, bool fCopy) throw(_com_error)
{
if (fCopy) {
::VariantInit(this);
_com_util::CheckError(::VariantCopy(this, &varSrc));
} else {
memcpy(this, &varSrc, sizeof(varSrc));
V_VT(&varSrc) = VT_EMPTY;
}
}
// Construct either a VT_I2 VARIANT or a VT_BOOL VARIANT from
// a short (the default is VT_I2)
//
inline _variant_t::_variant_t(short sSrc, VARTYPE vtSrc) throw(_com_error)
{
if ((vtSrc != VT_I2) && (vtSrc != VT_BOOL)) {
_com_issue_error(E_INVALIDARG);
}
if (vtSrc == VT_BOOL) {
V_VT(this) = VT_BOOL;
V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
}
else {
V_VT(this) = VT_I2;
V_I2(this) = sSrc;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -