📄 comutil.h
字号:
/***
* comutil.h - Native C++ compiler COM support - BSTR, VARIANT wrappers header
*
* Copyright (C) 1996-1999 Microsoft Corporation
* All rights reserved.
*
****/
#if !defined(_INC_COMUTIL)
#define _INC_COMUTIL
#if _MSC_VER > 1000
#pragma once
#endif
#include <ole2.h>
#if _MSC_VER >= 1200
#pragma warning(push)
#endif
#pragma warning(disable:4290)
#pragma warning(disable:4310)
class _com_error;
void __stdcall _com_issue_error(HRESULT);
//////////////////////////////////////////////////////////////////////////////
//
// Forward class declarations
//
//////////////////////////////////////////////////////////////////////////////
class _bstr_t;
class _variant_t;
//////////////////////////////////////////////////////////////////////////////
//
// Error checking routines
//
//////////////////////////////////////////////////////////////////////////////
namespace _com_util {
inline void CheckError(HRESULT hr) throw(_com_error)
{
if (FAILED(hr)) {
_com_issue_error(hr);
}
}
}
//////////////////////////////////////////////////////////////////////////////
//
// Routines for handling conversions between BSTR and char*
//
//////////////////////////////////////////////////////////////////////////////
namespace _com_util {
// Convert char * to BSTR
//
BSTR __stdcall ConvertStringToBSTR(const char* pSrc) throw(_com_error);
// Convert BSTR to char *
//
char* __stdcall ConvertBSTRToString(BSTR pSrc) throw(_com_error);
}
//////////////////////////////////////////////////////////////////////////////
//
// Wrapper class for BSTR
//
//////////////////////////////////////////////////////////////////////////////
class _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();
};
//////////////////////////////////////////////////////////////////////////////
//
// Constructors
//
//////////////////////////////////////////////////////////////////////////////
// Default constructor
//
inline _bstr_t::_bstr_t() throw()
: m_Data(NULL)
{
}
// Copy constructor
//
inline _bstr_t::_bstr_t(const _bstr_t& s) throw()
: m_Data(s.m_Data)
{
_AddRef();
}
// Construct a _bstr_t from a const char*
//
inline _bstr_t::_bstr_t(const char* s) throw(_com_error)
: m_Data(new Data_t(s))
{
if (m_Data == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
// Construct a _bstr_t from a const whar_t*
//
inline _bstr_t::_bstr_t(const wchar_t* s) throw(_com_error)
: m_Data(new Data_t(s))
{
if (m_Data == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
// Construct a _bstr_t from a BSTR. If fCopy is FALSE, give control of
// data to the _bstr_t without making a new copy.
//
inline _bstr_t::_bstr_t(BSTR bstr, bool fCopy) throw(_com_error)
: m_Data(new Data_t(bstr, fCopy))
{
if (m_Data == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
// Destructor
//
inline _bstr_t::~_bstr_t() throw()
{
_Free();
}
//////////////////////////////////////////////////////////////////////////////
//
// Assignment operators
//
//////////////////////////////////////////////////////////////////////////////
// Default assign operator
//
inline _bstr_t& _bstr_t::operator=(const _bstr_t& s) throw()
{
const_cast<_bstr_t*>(&s)->_AddRef();
_Free();
m_Data = s.m_Data;
return *this;
}
// Assign a const char* to a _bstr_t
//
inline _bstr_t& _bstr_t::operator=(const char* s) throw(_com_error)
{
_Free();
m_Data = new Data_t(s);
return *this;
}
// Assign a const wchar_t* to a _bstr_t
//
inline _bstr_t& _bstr_t::operator=(const wchar_t* s) throw(_com_error)
{
_Free();
m_Data = new Data_t(s);
return *this;
}
//////////////////////////////////////////////////////////////////////////////
//
// Operators
//
//////////////////////////////////////////////////////////////////////////////
// Concatenate a _bstr_t onto this _bstr_t
//
inline _bstr_t& _bstr_t::operator+=(const _bstr_t& s) throw(_com_error)
{
Data_t* newData = new Data_t(*this, s);
_Free();
m_Data = newData;
return *this;
}
// Return the concatenation of this _bstr_t with another _bstr_t
//
inline _bstr_t _bstr_t::operator+(const _bstr_t& s) const throw(_com_error)
{
_bstr_t b = *this;
b += s;
return b;
}
//////////////////////////////////////////////////////////////////////////////
//
// Friend Operators
//
//////////////////////////////////////////////////////////////////////////////
// Return the concatenation of a const char* with a _bstr_t
//
inline _bstr_t operator+(const char* s1, const _bstr_t& s2) throw(_com_error)
{
_bstr_t b = s1;
b += s2;
return b;
}
// Return the concatenation of a const char* with a _bstr_t
//
inline _bstr_t operator+(const wchar_t* s1, const _bstr_t& s2) throw(_com_error)
{
_bstr_t b = s1;
b += s2;
return b;
}
//////////////////////////////////////////////////////////////////////////////
//
// Extractors
//
//////////////////////////////////////////////////////////////////////////////
// Extract a const wchar_t*
//
inline _bstr_t::operator const wchar_t*() const throw()
{
return (m_Data != NULL) ? m_Data->GetWString() : NULL;
}
// Extract a wchar_t*
//
inline _bstr_t::operator wchar_t*() const throw()
{
return const_cast<wchar_t*>((m_Data != NULL) ? m_Data->GetWString() : NULL);
}
// Extract a const char_t*
//
inline _bstr_t::operator const char*() const throw(_com_error)
{
return (m_Data != NULL) ? m_Data->GetString() : NULL;
}
// Extract a char_t*
//
inline _bstr_t::operator char*() const throw(_com_error)
{
return const_cast<char*>((m_Data != NULL) ? m_Data->GetString() : NULL);
}
//////////////////////////////////////////////////////////////////////////////
//
// Comparison operators
//
//////////////////////////////////////////////////////////////////////////////
inline bool _bstr_t::operator!() const throw()
{
return (m_Data != NULL) ? !m_Data->GetWString() : true;
}
inline bool _bstr_t::operator==(const _bstr_t& str) const throw()
{
return _Compare(str) == 0;
}
inline bool _bstr_t::operator!=(const _bstr_t& str) const throw()
{
return _Compare(str) != 0;
}
inline bool _bstr_t::operator<(const _bstr_t& str) const throw()
{
return _Compare(str) < 0;
}
inline bool _bstr_t::operator>(const _bstr_t& str) const throw()
{
return _Compare(str) > 0;
}
inline bool _bstr_t::operator<=(const _bstr_t& str) const throw()
{
return _Compare(str) <= 0;
}
inline bool _bstr_t::operator>=(const _bstr_t& str) const throw()
{
return _Compare(str) >= 0;
}
//////////////////////////////////////////////////////////////////////////////
//
// Low-level help functions
//
//////////////////////////////////////////////////////////////////////////////
// Extract a copy of the wrapped BSTR
//
inline BSTR _bstr_t::copy() const throw(_com_error)
{
return (m_Data != NULL) ? m_Data->Copy() : NULL;
}
// Return the length of the wrapped BSTR
//
inline unsigned int _bstr_t::length() const throw()
{
return (m_Data != NULL) ? m_Data->Length() : 0;
}
// Binary string assign
//
inline void _bstr_t::Assign(BSTR s) throw(_com_error)
{
if (m_Data != NULL) {
m_Data->Assign(s);
}
else {
m_Data = new Data_t(s, TRUE);
if (m_Data == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
}
// AddRef the BSTR
//
inline void _bstr_t::_AddRef() throw()
{
if (m_Data != NULL) {
m_Data->AddRef();
}
}
// Free the BSTR
//
inline void _bstr_t::_Free() throw()
{
if (m_Data != NULL) {
m_Data->Release();
m_Data = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -