| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244 |
- /***
- * comutil.h - Native C++ compiler COM support - BSTR, VARIANT wrappers header
- *
- * Copyright (C) 1996-2001 Microsoft Corporation
- * All rights reserved.
- *
- ****/
- #if _MSC_VER > 1000
- #pragma once
- #endif
- #if !defined(_INC_COMUTIL)
- #define _INC_COMUTIL
- #include <ole2.h>
- #if !defined(_COM_ASSERT)
- # if defined(_DEBUG)
- # include <assert.h>
- # define _COM_ASSERT(x) assert(x)
- # else
- # define _COM_ASSERT(x) ((void)0)
- # endif
- #endif
- #pragma warning(push)
- #pragma warning(disable: 4290)
- #pragma warning(disable: 4310)
- #pragma push_macro("new")
- #undef new
- 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(bool fCopy = true) const throw(_com_error);
- unsigned int length() const throw();
- // Binary string assign
- //
- void Assign(BSTR s) throw(_com_error);
- // Get the physical BSTR
- //
- BSTR& GetBSTR() throw(_com_error);
- BSTR* GetAddress() throw(_com_error);
- // Attach to the internal BSTR w/o copying
- //
- void Attach(BSTR s) throw(_com_error);
- // Detach the internal BSTR
- //
- BSTR Detach() throw();
- 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();
- unsigned long RefCount() const 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();
- wchar_t*& GetWString() throw();
- const char* GetString() const throw(_com_error);
- BSTR Copy() const throw(_com_error);
- void Assign(BSTR s) throw(_com_error);
- void Attach(BSTR s) throw();
- unsigned int Length() const throw();
- int Compare(const Data_t& str) const throw();
- // Exception agnostic wrapper for new
- //
- void* operator new(size_t sz);
- 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()
- {
- if (this != &s) {
- _Free();
- m_Data = s.m_Data;
- _AddRef();
- }
- return *this;
- }
- // Assign a const char* to a _bstr_t
- //
- inline _bstr_t& _bstr_t::operator=(const char* s) throw(_com_error)
- {
- _COM_ASSERT((const char*) *this != s);
- _Free();
- m_Data = new Data_t(s);
- if (m_Data == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- return *this;
- }
- // Assign a const wchar_t* to a _bstr_t
- //
- inline _bstr_t& _bstr_t::operator=(const wchar_t* s) throw(_com_error)
- {
- _COM_ASSERT((const wchar_t*) *this != s);
- _Free();
- m_Data = new Data_t(s);
- if (m_Data == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- 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);
- if (newData == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- _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(bool fCopy) const throw(_com_error)
- {
- return (m_Data != NULL) ? (fCopy ? m_Data->Copy() : m_Data->GetWString()) : 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)
- {
- _COM_ASSERT( m_Data->GetWString() != s);
- _Free();
- m_Data = new Data_t(s, TRUE);
- if (m_Data == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- }
- // Get the physical BSTR
- //
- inline BSTR& _bstr_t::GetBSTR() throw(_com_error)
- {
- if (m_Data == NULL) {
- m_Data = new Data_t(0, FALSE);
- if (m_Data == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- }
- return m_Data->GetWString();
- }
- // Get the address of the physical BSTR to pass as an 'out'-parameter
- //
- inline BSTR* _bstr_t::GetAddress() throw(_com_error)
- {
- Attach(0);
- return &m_Data->GetWString();
- }
- // Attach to the internal BSTR w/o copying
- //
- inline void _bstr_t::Attach(BSTR s) throw(_com_error)
- {
- _Free();
- m_Data = new Data_t(s, FALSE);
- if (m_Data == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- }
- // Detach the internal BSTR
- //
- inline BSTR _bstr_t::Detach() throw()
- {
- _COM_ASSERT(m_Data->RefCount() == 1);
- BSTR b = m_Data->GetWString();
- m_Data->GetWString() = NULL;
- _Free();
- return b;
- }
- // 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;
- }
- }
- // 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);
- }
- // 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;
- }
- inline unsigned long _bstr_t::Data_t::RefCount() const throw()
- {
- 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 wchar_t*& _bstr_t::Data_t::GetWString() 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);
- }
- 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) {
- m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(s), ::SysStringByteLen(s));
- m_str = 0;
- }
- }
- inline void _bstr_t::Data_t::Attach(BSTR s) throw()
- {
- _Free();
- m_wstr = s;
- m_str = 0;
- m_RefCount = 1;
- }
- // 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;
- }
- // Exception agnostic wrapper for new
- //
- #ifdef _COM_OPERATOR_NEW_THROWS
- inline void* _bstr_t::Data_t::operator new(size_t sz) {
- try {
- return ::operator new(sz);
- } catch (...) {
- return NULL;
- }
- }
- #else // _COM_OPERATOR_NEW_THROWS
- inline void* _bstr_t::Data_t::operator new(size_t sz) {
- return ::operator new(sz);
- }
- #endif // _COM_OPERATOR_NEW_THROWS
- // 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 boolSrc) 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(char cSrc) throw(); // Creates a VT_I1
- _variant_t(unsigned short usSrc) throw(); // Creates a VT_UI2
- _variant_t(unsigned long ulSrc) throw(); // Creates a VT_UI4
- _variant_t(int iSrc) throw(); // Creates a VT_INT
- _variant_t(unsigned int uiSrc) throw(); // Creates a VT_UINT
- _variant_t(__int64 i8Src) throw(); // Creates a VT_I8
- _variant_t(unsigned __int64 ui8Src) 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 VARIANT() const throw();
- operator char() const throw(_com_error); // Extracts a char from a VT_I1
- operator unsigned short() const throw(_com_error); // Extracts a unsigned short from a VT_UI2
- operator unsigned long() const throw(_com_error); // Extracts a unsigned long from a VT_UI4
- operator int() const throw(_com_error); // Extracts a int from a VT_INT
- operator unsigned int() const throw(_com_error); // Extracts a unsigned int from a VT_UINT
- operator __int64() const throw(_com_error); // Extracts a __int64 from a VT_I8
- operator unsigned __int64() const throw(_com_error); // Extracts a unsigned __int64 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 boolSrc) 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=(char cSrc) throw(_com_error); // Assign a VT_I1
- _variant_t& operator=(unsigned short usSrc) throw(_com_error); // Assign a VT_UI2
- _variant_t& operator=(unsigned long ulSrc) throw(_com_error); // Assign a VT_UI4
- _variant_t& operator=(int iSrc) throw(_com_error); // Assign a VT_INT
- _variant_t& operator=(unsigned int uiSrc) throw(_com_error); // Assign a VT_UINT
- _variant_t& operator=(__int64 i8Src) throw(_com_error); // Assign a VT_I8
- _variant_t& operator=(unsigned __int64 ui8Src) 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);
- VARIANT& GetVARIANT() throw();
- VARIANT* GetAddress() 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;
- }
- }
- // Construct either a VT_I4 VARIANT, a VT_BOOL VARIANT, or a
- // VT_ERROR VARIANT from a long (the default is VT_I4)
- //
- inline _variant_t::_variant_t(long lSrc, VARTYPE vtSrc) throw(_com_error)
- {
- if ((vtSrc != VT_I4) && (vtSrc != VT_ERROR) && (vtSrc != VT_BOOL)) {
- _com_issue_error(E_INVALIDARG);
- }
- if (vtSrc == VT_ERROR) {
- V_VT(this) = VT_ERROR;
- V_ERROR(this) = lSrc;
- }
- else if (vtSrc == VT_BOOL) {
- V_VT(this) = VT_BOOL;
- V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
- }
- else {
- V_VT(this) = VT_I4;
- V_I4(this) = lSrc;
- }
- }
- // Construct a VT_R4 VARIANT from a float
- //
- inline _variant_t::_variant_t(float fltSrc) throw()
- {
- V_VT(this) = VT_R4;
- V_R4(this) = fltSrc;
- }
- // Construct either a VT_R8 VARIANT, or a VT_DATE VARIANT from
- // a double (the default is VT_R8)
- //
- inline _variant_t::_variant_t(double dblSrc, VARTYPE vtSrc) throw(_com_error)
- {
- if ((vtSrc != VT_R8) && (vtSrc != VT_DATE)) {
- _com_issue_error(E_INVALIDARG);
- }
- if (vtSrc == VT_DATE) {
- V_VT(this) = VT_DATE;
- V_DATE(this) = dblSrc;
- }
- else {
- V_VT(this) = VT_R8;
- V_R8(this) = dblSrc;
- }
- }
- // Construct a VT_CY from a CY
- //
- inline _variant_t::_variant_t(const CY& cySrc) throw()
- {
- V_VT(this) = VT_CY;
- V_CY(this) = cySrc;
- }
- // Construct a VT_BSTR VARIANT from a const _bstr_t&
- //
- inline _variant_t::_variant_t(const _bstr_t& bstrSrc) throw(_com_error)
- {
- V_VT(this) = VT_BSTR;
- BSTR bstr = static_cast<wchar_t*>(bstrSrc);
- V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
- ::SysStringByteLen(bstr));
- if (V_BSTR(this) == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- }
- // Construct a VT_BSTR VARIANT from a const wchar_t*
- //
- inline _variant_t::_variant_t(const wchar_t* pSrc) throw(_com_error)
- {
- V_VT(this) = VT_BSTR;
- V_BSTR(this) = ::SysAllocString(pSrc);
- if (V_BSTR(this) == NULL && pSrc != NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- }
- // Construct a VT_BSTR VARIANT from a const char*
- //
- inline _variant_t::_variant_t(const char* pSrc) throw(_com_error)
- {
- V_VT(this) = VT_BSTR;
- V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
- }
- // Construct a VT_DISPATCH VARIANT from an IDispatch*
- //
- inline _variant_t::_variant_t(IDispatch* pSrc, bool fAddRef) throw()
- {
- V_VT(this) = VT_DISPATCH;
- V_DISPATCH(this) = pSrc;
- // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
- // false indicates we're taking ownership
- //
- if (fAddRef) {
- V_DISPATCH(this)->AddRef();
- }
- }
- // Construct a VT_BOOL VARIANT from a bool
- //
- inline _variant_t::_variant_t(bool boolSrc) throw()
- {
- V_VT(this) = VT_BOOL;
- V_BOOL(this) = (boolSrc ? VARIANT_TRUE : VARIANT_FALSE);
- }
- // Construct a VT_UNKNOWN VARIANT from an IUnknown*
- //
- inline _variant_t::_variant_t(IUnknown* pSrc, bool fAddRef) throw()
- {
- V_VT(this) = VT_UNKNOWN;
- V_UNKNOWN(this) = pSrc;
- // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
- // false indicates we're taking ownership
- //
- if (fAddRef) {
- V_UNKNOWN(this)->AddRef();
- }
- }
- // Construct a VT_DECIMAL VARIANT from a DECIMAL
- //
- inline _variant_t::_variant_t(const DECIMAL& decSrc) throw()
- {
- // Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
- //
- V_DECIMAL(this) = decSrc;
- V_VT(this) = VT_DECIMAL;
- }
- // Construct a VT_UI1 VARIANT from a BYTE (unsigned char)
- //
- inline _variant_t::_variant_t(BYTE bSrc) throw()
- {
- V_VT(this) = VT_UI1;
- V_UI1(this) = bSrc;
- }
- // Construct a VT_I1 VARIANT from a char
- //
- inline _variant_t::_variant_t(char cSrc) throw()
- {
- V_VT(this) = VT_I1;
- V_I1(this) = cSrc;
- }
- // Construct a VT_UI2 VARIANT from a unsigned short
- //
- inline _variant_t::_variant_t(unsigned short usSrc) throw()
- {
- V_VT(this) = VT_UI2;
- V_UI2(this) = usSrc;
- }
- // Construct a VT_UI4 VARIANT from a unsigned long
- //
- inline _variant_t::_variant_t(unsigned long ulSrc) throw()
- {
- V_VT(this) = VT_UI4;
- V_UI4(this) = ulSrc;
- }
- // Construct a VT_INT VARIANT from a int
- //
- inline _variant_t::_variant_t(int iSrc) throw()
- {
- V_VT(this) = VT_INT;
- V_INT(this) = iSrc;
- }
- // Construct a VT_UINT VARIANT from a unsigned int
- //
- inline _variant_t::_variant_t(unsigned int uiSrc) throw()
- {
- V_VT(this) = VT_UINT;
- V_UINT(this) = uiSrc;
- }
- // Construct a VT_I8 VARIANT from a __int64
- //
- inline _variant_t::_variant_t(__int64 i8Src) throw()
- {
- V_VT(this) = VT_I8;
- V_I8(this) = i8Src;
- }
- // Construct a VT_UI8 VARIANT from a unsigned __int64
- //
- inline _variant_t::_variant_t(unsigned __int64 ui8Src) throw()
- {
- V_VT(this) = VT_UI8;
- V_UI8(this) = ui8Src;
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- //
- // Extractors
- //
- //////////////////////////////////////////////////////////////////////////////////////////
- // Extracts a VT_I2 into a short
- //
- inline _variant_t::operator short() const throw(_com_error)
- {
- if (V_VT(this) == VT_I2) {
- return V_I2(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_I2, this);
- return V_I2(&varDest);
- }
- // Extracts a VT_I4 into a long
- //
- inline _variant_t::operator long() const throw(_com_error)
- {
- if (V_VT(this) == VT_I4) {
- return V_I4(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_I4, this);
- return V_I4(&varDest);
- }
- // Extracts a VT_R4 into a float
- //
- inline _variant_t::operator float() const throw(_com_error)
- {
- if (V_VT(this) == VT_R4) {
- return V_R4(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_R4, this);
- return V_R4(&varDest);
- }
- // Extracts a VT_R8 into a double
- //
- inline _variant_t::operator double() const throw(_com_error)
- {
- if (V_VT(this) == VT_R8) {
- return V_R8(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_R8, this);
- return V_R8(&varDest);
- }
- // Extracts a VT_CY into a CY
- //
- inline _variant_t::operator CY() const throw(_com_error)
- {
- if (V_VT(this) == VT_CY) {
- return V_CY(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_CY, this);
- return V_CY(&varDest);
- }
- // Extracts a VT_BSTR into a _bstr_t
- //
- inline _variant_t::operator _bstr_t() const throw(_com_error)
- {
- if (V_VT(this) == VT_BSTR) {
- return V_BSTR(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_BSTR, this);
- return V_BSTR(&varDest);
- }
- // Extracts a VT_DISPATCH into an IDispatch*
- //
- inline _variant_t::operator IDispatch*() const throw(_com_error)
- {
- if (V_VT(this) == VT_DISPATCH) {
- V_DISPATCH(this)->AddRef();
- return V_DISPATCH(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_DISPATCH, this);
- V_DISPATCH(&varDest)->AddRef();
- return V_DISPATCH(&varDest);
- }
- // Extract a VT_BOOL into a bool
- //
- inline _variant_t::operator bool() const throw(_com_error)
- {
- if (V_VT(this) == VT_BOOL) {
- return V_BOOL(this) ? true : false;
- }
- _variant_t varDest;
- varDest.ChangeType(VT_BOOL, this);
- return V_BOOL(&varDest) ? true : false;
- }
- // Extracts a VT_UNKNOWN into an IUnknown*
- //
- inline _variant_t::operator IUnknown*() const throw(_com_error)
- {
- if (V_VT(this) == VT_UNKNOWN) {
- V_UNKNOWN(this)->AddRef();
- return V_UNKNOWN(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_UNKNOWN, this);
- V_UNKNOWN(&varDest)->AddRef();
- return V_UNKNOWN(&varDest);
- }
- // Extracts a VT_DECIMAL into a DECIMAL
- //
- inline _variant_t::operator DECIMAL() const throw(_com_error)
- {
- if (V_VT(this) == VT_DECIMAL) {
- return V_DECIMAL(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_DECIMAL, this);
- return V_DECIMAL(&varDest);
- }
- // Extracts a VT_UI1 into a BYTE (unsigned char)
- //
- inline _variant_t::operator BYTE() const throw(_com_error)
- {
- if (V_VT(this) == VT_UI1) {
- return V_UI1(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_UI1, this);
- return V_UI1(&varDest);
- }
- // Extract the physical VARIANT
- //
- inline _variant_t::operator VARIANT() const throw()
- {
- return *(VARIANT*) this;
- }
- // Extracts a VT_I1 into a char
- //
- inline _variant_t::operator char() const throw(_com_error)
- {
- if (V_VT(this) == VT_I1) {
- return V_I1(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_I1, this);
- return V_I1(&varDest);
- }
- // Extracts a VT_UI2 into a unsigned short
- //
- inline _variant_t::operator unsigned short() const throw(_com_error)
- {
- if (V_VT(this) == VT_UI2) {
- return V_UI2(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_UI2, this);
- return V_UI2(&varDest);
- }
- // Extracts a VT_UI4 into a unsigned long
- //
- inline _variant_t::operator unsigned long() const throw(_com_error)
- {
- if (V_VT(this) == VT_UI4) {
- return V_UI4(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_UI4, this);
- return V_UI4(&varDest);
- }
- // Extracts a VT_INT into a int
- //
- inline _variant_t::operator int() const throw(_com_error)
- {
- if (V_VT(this) == VT_INT) {
- return V_INT(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_INT, this);
- return V_INT(&varDest);
- }
- // Extracts a VT_UINT into a unsigned int
- //
- inline _variant_t::operator unsigned int() const throw(_com_error)
- {
- if (V_VT(this) == VT_UINT) {
- return V_UINT(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_UINT, this);
- return V_UINT(&varDest);
- }
- // Extracts a VT_I8 into a __int64
- //
- inline _variant_t::operator __int64() const throw(_com_error)
- {
- if (V_VT(this) == VT_I8) {
- return V_I8(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_I8, this);
- return V_I8(&varDest);
- }
- // Extracts a VT_UI8 into a unsigned __int64
- //
- inline _variant_t::operator unsigned __int64() const throw(_com_error)
- {
- if (V_VT(this) == VT_UI8) {
- return V_UI8(this);
- }
- _variant_t varDest;
- varDest.ChangeType(VT_UI8, this);
- return V_UI8(&varDest);
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- //
- // Assignment operations
- //
- //////////////////////////////////////////////////////////////////////////////////////////
- // Assign a const VARIANT& (::VariantCopy handles everything)
- //
- inline _variant_t& _variant_t::operator=(const VARIANT& varSrc) throw(_com_error)
- {
- _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
- return *this;
- }
- // Assign a const VARIANT* (::VariantCopy handles everything)
- //
- inline _variant_t& _variant_t::operator=(const VARIANT* pSrc) throw(_com_error)
- {
- _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
- return *this;
- }
- // Assign a const _variant_t& (::VariantCopy handles everything)
- //
- inline _variant_t& _variant_t::operator=(const _variant_t& varSrc) throw(_com_error)
- {
- _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
- return *this;
- }
- // Assign a short creating either VT_I2 VARIANT or a
- // VT_BOOL VARIANT (VT_I2 is the default)
- //
- inline _variant_t& _variant_t::operator=(short sSrc) throw(_com_error)
- {
- if (V_VT(this) == VT_I2) {
- V_I2(this) = sSrc;
- }
- else if (V_VT(this) == VT_BOOL) {
- V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
- }
- else {
- // Clear the VARIANT and create a VT_I2
- //
- Clear();
- V_VT(this) = VT_I2;
- V_I2(this) = sSrc;
- }
- return *this;
- }
- // Assign a long creating either VT_I4 VARIANT, a VT_ERROR VARIANT
- // or a VT_BOOL VARIANT (VT_I4 is the default)
- //
- inline _variant_t& _variant_t::operator=(long lSrc) throw(_com_error)
- {
- if (V_VT(this) == VT_I4) {
- V_I4(this) = lSrc;
- }
- else if (V_VT(this) == VT_ERROR) {
- V_ERROR(this) = lSrc;
- }
- else if (V_VT(this) == VT_BOOL) {
- V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
- }
- else {
- // Clear the VARIANT and create a VT_I4
- //
- Clear();
- V_VT(this) = VT_I4;
- V_I4(this) = lSrc;
- }
- return *this;
- }
- // Assign a float creating a VT_R4 VARIANT
- //
- inline _variant_t& _variant_t::operator=(float fltSrc) throw(_com_error)
- {
- if (V_VT(this) != VT_R4) {
- // Clear the VARIANT and create a VT_R4
- //
- Clear();
- V_VT(this) = VT_R4;
- }
- V_R4(this) = fltSrc;
- 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)
- //
- _COM_ASSERT(V_BSTR(this) != (BSTR) bstrSrc);
- Clear();
- V_VT(this) = VT_BSTR;
- if (!bstrSrc) {
- V_BSTR(this) = NULL;
- }
- else {
- BSTR bstr = static_cast<wchar_t*>(bstrSrc);
- V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
- ::SysStringByteLen(bstr));
- if (V_BSTR(this) == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- }
- 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)
- //
- _COM_ASSERT(V_BSTR(this) != pSrc);
- Clear();
- V_VT(this) = VT_BSTR;
- if (pSrc == NULL) {
- V_BSTR(this) = NULL;
- }
- else {
- V_BSTR(this) = ::SysAllocString(pSrc);
- if (V_BSTR(this) == NULL) {
- _com_issue_error(E_OUTOFMEMORY);
- }
- }
- 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)
- //
- _COM_ASSERT(V_I1REF(this) != pSrc);
- Clear();
- V_VT(this) = VT_BSTR;
- V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
- 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)
- //
- _COM_ASSERT(V_DISPATCH(this) != pSrc);
- 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 boolSrc) 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) = (boolSrc ? 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)
- //
- _COM_ASSERT(V_UNKNOWN(this) != pSrc);
- 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 char creating a VT_I1 VARIANT
- //
- inline _variant_t& _variant_t::operator=(char cSrc) throw(_com_error){
- if (V_VT(this) != VT_I1) {
- // Clear the VARIANT and create a VT_I1
- //
- Clear();
- V_VT(this) = VT_I1;
- }
- V_I1(this) = cSrc;
- return *this;
- }
- // Assign a char creating a VT_UI2 VARIANT
- //
- inline _variant_t& _variant_t::operator=(unsigned short usSrc) throw(_com_error){
- if (V_VT(this) != VT_UI2) {
- // Clear the VARIANT and create a VT_UI2
- //
- Clear();
- V_VT(this) = VT_UI2;
- }
- V_UI2(this) = usSrc;
- return *this;
- }
- // Assign a char creating a VT_UI4 VARIANT
- //
- inline _variant_t& _variant_t::operator=(unsigned long ulSrc) throw(_com_error){
- if (V_VT(this) != VT_UI4) {
- // Clear the VARIANT and create a VT_UI4
- //
- Clear();
- V_VT(this) = VT_UI4;
- }
- V_UI4(this) = ulSrc;
- return *this;
- }
- // Assign a char creating a VT_INT VARIANT
- //
- inline _variant_t& _variant_t::operator=(int iSrc) throw(_com_error){
- if (V_VT(this) != VT_INT) {
- // Clear the VARIANT and create a VT_INT
- //
- Clear();
- V_VT(this) = VT_INT;
- }
- V_INT(this) = iSrc;
- return *this;
- }
- // Assign a char creating a VT_UINT VARIANT
- //
- inline _variant_t& _variant_t::operator=(unsigned int uiSrc) throw(_com_error){
- if (V_VT(this) != VT_UINT) {
- // Clear the VARIANT and create a VT_UINT
- //
- Clear();
- V_VT(this) = VT_UINT;
- }
- V_UINT(this) = uiSrc;
- return *this;
- }
- // Assign a char creating a VT_I8 VARIANT
- //
- inline _variant_t& _variant_t::operator=(__int64 i8Src) 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) = i8Src;
- return *this;
- }
- // Assign a char creating a VT_UI8 VARIANT
- //
- inline _variant_t& _variant_t::operator=(unsigned __int64 ui8Src) 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) = ui8Src;
- 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_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);
- case VT_I1:
- return V_I1(this) == V_I1(pSrc);
- case VT_UI2:
- return V_UI2(this) == V_UI2(pSrc);
- case VT_UI4:
- return V_UI4(this) == V_UI4(pSrc);
- case VT_INT:
- return V_INT(this) == V_INT(pSrc);
- case VT_UINT:
- return V_UINT(this) == V_UINT(pSrc);
- case VT_I8:
- return V_I8(this) == V_I8(pSrc);
- case VT_UI8:
- return V_UI8(this) == V_UI8(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;
- }
- inline VARIANT& _variant_t::GetVARIANT() throw()
- {
- return *(VARIANT*) this;
- }
- inline VARIANT* _variant_t::GetAddress() throw(_com_error)
- {
- Clear();
- return (VARIANT*) this;
- }
- // 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);
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- //
- // 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
- #pragma pop_macro("new")
- #pragma warning(pop)
- #endif // _INC_COMUTIL
|