comutil.h 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /***
  2. * comutil.h - Native C++ compiler COM support - BSTR, VARIANT wrappers header
  3. *
  4. * Copyright (C) 1996-2001 Microsoft Corporation
  5. * All rights reserved.
  6. *
  7. ****/
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif
  11. #if !defined(_INC_COMUTIL)
  12. #define _INC_COMUTIL
  13. #include <ole2.h>
  14. #if !defined(_COM_ASSERT)
  15. # if defined(_DEBUG)
  16. # include <assert.h>
  17. # define _COM_ASSERT(x) assert(x)
  18. # else
  19. # define _COM_ASSERT(x) ((void)0)
  20. # endif
  21. #endif
  22. #pragma warning(push)
  23. #pragma warning(disable: 4290)
  24. #pragma warning(disable: 4310)
  25. #pragma push_macro("new")
  26. #undef new
  27. class _com_error;
  28. void __stdcall _com_issue_error(HRESULT);
  29. //////////////////////////////////////////////////////////////////////////////
  30. //
  31. // Forward class declarations
  32. //
  33. //////////////////////////////////////////////////////////////////////////////
  34. class _bstr_t;
  35. class _variant_t;
  36. //////////////////////////////////////////////////////////////////////////////
  37. //
  38. // Error checking routines
  39. //
  40. //////////////////////////////////////////////////////////////////////////////
  41. namespace _com_util {
  42. inline void CheckError(HRESULT hr) throw(_com_error)
  43. {
  44. if (FAILED(hr)) {
  45. _com_issue_error(hr);
  46. }
  47. }
  48. }
  49. //////////////////////////////////////////////////////////////////////////////
  50. //
  51. // Routines for handling conversions between BSTR and char*
  52. //
  53. //////////////////////////////////////////////////////////////////////////////
  54. namespace _com_util {
  55. // Convert char * to BSTR
  56. //
  57. BSTR __stdcall ConvertStringToBSTR(const char* pSrc) throw(_com_error);
  58. // Convert BSTR to char *
  59. //
  60. char* __stdcall ConvertBSTRToString(BSTR pSrc) throw(_com_error);
  61. }
  62. //////////////////////////////////////////////////////////////////////////////
  63. //
  64. // Wrapper class for BSTR
  65. //
  66. //////////////////////////////////////////////////////////////////////////////
  67. class _bstr_t {
  68. public:
  69. // Constructors
  70. //
  71. _bstr_t() throw();
  72. _bstr_t(const _bstr_t& s) throw();
  73. _bstr_t(const char* s) throw(_com_error);
  74. _bstr_t(const wchar_t* s) throw(_com_error);
  75. _bstr_t(const _variant_t& var) throw(_com_error);
  76. _bstr_t(BSTR bstr, bool fCopy) throw(_com_error);
  77. // Destructor
  78. //
  79. ~_bstr_t() throw();
  80. // Assignment operators
  81. //
  82. _bstr_t& operator=(const _bstr_t& s) throw();
  83. _bstr_t& operator=(const char* s) throw(_com_error);
  84. _bstr_t& operator=(const wchar_t* s) throw(_com_error);
  85. _bstr_t& operator=(const _variant_t& var) throw(_com_error);
  86. // Operators
  87. //
  88. _bstr_t& operator+=(const _bstr_t& s) throw(_com_error);
  89. _bstr_t operator+(const _bstr_t& s) const throw(_com_error);
  90. // Friend operators
  91. //
  92. friend _bstr_t operator+(const char* s1, const _bstr_t& s2) throw(_com_error);
  93. friend _bstr_t operator+(const wchar_t* s1, const _bstr_t& s2) throw(_com_error);
  94. // Extractors
  95. //
  96. operator const wchar_t*() const throw();
  97. operator wchar_t*() const throw();
  98. operator const char*() const throw(_com_error);
  99. operator char*() const throw(_com_error);
  100. // Comparison operators
  101. //
  102. bool operator!() const throw();
  103. bool operator==(const _bstr_t& str) const throw();
  104. bool operator!=(const _bstr_t& str) const throw();
  105. bool operator<(const _bstr_t& str) const throw();
  106. bool operator>(const _bstr_t& str) const throw();
  107. bool operator<=(const _bstr_t& str) const throw();
  108. bool operator>=(const _bstr_t& str) const throw();
  109. // Low-level helper functions
  110. //
  111. BSTR copy(bool fCopy = true) const throw(_com_error);
  112. unsigned int length() const throw();
  113. // Binary string assign
  114. //
  115. void Assign(BSTR s) throw(_com_error);
  116. // Get the physical BSTR
  117. //
  118. BSTR& GetBSTR() throw(_com_error);
  119. BSTR* GetAddress() throw(_com_error);
  120. // Attach to the internal BSTR w/o copying
  121. //
  122. void Attach(BSTR s) throw(_com_error);
  123. // Detach the internal BSTR
  124. //
  125. BSTR Detach() throw();
  126. private:
  127. // Referenced counted wrapper
  128. //
  129. class Data_t {
  130. public:
  131. // Constructors
  132. //
  133. Data_t(const char* s) throw(_com_error);
  134. Data_t(const wchar_t* s) throw(_com_error);
  135. Data_t(BSTR bstr, bool fCopy) throw(_com_error);
  136. Data_t(const _bstr_t& s1, const _bstr_t& s2) throw(_com_error);
  137. // Reference counting routines
  138. //
  139. unsigned long AddRef() throw();
  140. unsigned long Release() throw();
  141. unsigned long RefCount() const throw();
  142. // Extractors
  143. //
  144. operator const wchar_t*() const throw();
  145. operator const char*() const throw(_com_error);
  146. // Low-level helper functions
  147. //
  148. const wchar_t* GetWString() const throw();
  149. wchar_t*& GetWString() throw();
  150. const char* GetString() const throw(_com_error);
  151. BSTR Copy() const throw(_com_error);
  152. void Assign(BSTR s) throw(_com_error);
  153. void Attach(BSTR s) throw();
  154. unsigned int Length() const throw();
  155. int Compare(const Data_t& str) const throw();
  156. // Exception agnostic wrapper for new
  157. //
  158. void* operator new(size_t sz);
  159. private:
  160. wchar_t* m_wstr;
  161. mutable char* m_str;
  162. unsigned long m_RefCount;
  163. // Never allow default construction
  164. //
  165. Data_t() throw();
  166. // Never allow copy
  167. //
  168. Data_t(const Data_t& s) throw();
  169. // Prevent deletes from outside. Release() must be used.
  170. //
  171. ~Data_t() throw();
  172. void _Free() throw();
  173. };
  174. private:
  175. // Reference counted representation
  176. //
  177. Data_t* m_Data;
  178. private:
  179. // Low-level utilities
  180. //
  181. void _AddRef() throw();
  182. void _Free() throw();
  183. int _Compare(const _bstr_t& str) const throw();
  184. };
  185. //////////////////////////////////////////////////////////////////////////////
  186. //
  187. // Constructors
  188. //
  189. //////////////////////////////////////////////////////////////////////////////
  190. // Default constructor
  191. //
  192. inline _bstr_t::_bstr_t() throw()
  193. : m_Data(NULL)
  194. {
  195. }
  196. // Copy constructor
  197. //
  198. inline _bstr_t::_bstr_t(const _bstr_t& s) throw()
  199. : m_Data(s.m_Data)
  200. {
  201. _AddRef();
  202. }
  203. // Construct a _bstr_t from a const char*
  204. //
  205. inline _bstr_t::_bstr_t(const char* s) throw(_com_error)
  206. : m_Data(new Data_t(s))
  207. {
  208. if (m_Data == NULL) {
  209. _com_issue_error(E_OUTOFMEMORY);
  210. }
  211. }
  212. // Construct a _bstr_t from a const whar_t*
  213. //
  214. inline _bstr_t::_bstr_t(const wchar_t* s) throw(_com_error)
  215. : m_Data(new Data_t(s))
  216. {
  217. if (m_Data == NULL) {
  218. _com_issue_error(E_OUTOFMEMORY);
  219. }
  220. }
  221. // Construct a _bstr_t from a BSTR. If fCopy is FALSE, give control of
  222. // data to the _bstr_t without making a new copy.
  223. //
  224. inline _bstr_t::_bstr_t(BSTR bstr, bool fCopy) throw(_com_error)
  225. : m_Data(new Data_t(bstr, fCopy))
  226. {
  227. if (m_Data == NULL) {
  228. _com_issue_error(E_OUTOFMEMORY);
  229. }
  230. }
  231. // Destructor
  232. //
  233. inline _bstr_t::~_bstr_t() throw()
  234. {
  235. _Free();
  236. }
  237. //////////////////////////////////////////////////////////////////////////////
  238. //
  239. // Assignment operators
  240. //
  241. //////////////////////////////////////////////////////////////////////////////
  242. // Default assign operator
  243. //
  244. inline _bstr_t& _bstr_t::operator=(const _bstr_t& s) throw()
  245. {
  246. if (this != &s) {
  247. _Free();
  248. m_Data = s.m_Data;
  249. _AddRef();
  250. }
  251. return *this;
  252. }
  253. // Assign a const char* to a _bstr_t
  254. //
  255. inline _bstr_t& _bstr_t::operator=(const char* s) throw(_com_error)
  256. {
  257. _COM_ASSERT((const char*) *this != s);
  258. _Free();
  259. m_Data = new Data_t(s);
  260. if (m_Data == NULL) {
  261. _com_issue_error(E_OUTOFMEMORY);
  262. }
  263. return *this;
  264. }
  265. // Assign a const wchar_t* to a _bstr_t
  266. //
  267. inline _bstr_t& _bstr_t::operator=(const wchar_t* s) throw(_com_error)
  268. {
  269. _COM_ASSERT((const wchar_t*) *this != s);
  270. _Free();
  271. m_Data = new Data_t(s);
  272. if (m_Data == NULL) {
  273. _com_issue_error(E_OUTOFMEMORY);
  274. }
  275. return *this;
  276. }
  277. //////////////////////////////////////////////////////////////////////////////
  278. //
  279. // Operators
  280. //
  281. //////////////////////////////////////////////////////////////////////////////
  282. // Concatenate a _bstr_t onto this _bstr_t
  283. //
  284. inline _bstr_t& _bstr_t::operator+=(const _bstr_t& s) throw(_com_error)
  285. {
  286. Data_t* newData = new Data_t(*this, s);
  287. if (newData == NULL) {
  288. _com_issue_error(E_OUTOFMEMORY);
  289. }
  290. _Free();
  291. m_Data = newData;
  292. return *this;
  293. }
  294. // Return the concatenation of this _bstr_t with another _bstr_t
  295. //
  296. inline _bstr_t _bstr_t::operator+(const _bstr_t& s) const throw(_com_error)
  297. {
  298. _bstr_t b = *this;
  299. b += s;
  300. return b;
  301. }
  302. //////////////////////////////////////////////////////////////////////////////
  303. //
  304. // Friend Operators
  305. //
  306. //////////////////////////////////////////////////////////////////////////////
  307. // Return the concatenation of a const char* with a _bstr_t
  308. //
  309. inline _bstr_t operator+(const char* s1, const _bstr_t& s2) throw(_com_error)
  310. {
  311. _bstr_t b = s1;
  312. b += s2;
  313. return b;
  314. }
  315. // Return the concatenation of a const char* with a _bstr_t
  316. //
  317. inline _bstr_t operator+(const wchar_t* s1, const _bstr_t& s2) throw(_com_error)
  318. {
  319. _bstr_t b = s1;
  320. b += s2;
  321. return b;
  322. }
  323. //////////////////////////////////////////////////////////////////////////////
  324. //
  325. // Extractors
  326. //
  327. //////////////////////////////////////////////////////////////////////////////
  328. // Extract a const wchar_t*
  329. //
  330. inline _bstr_t::operator const wchar_t*() const throw()
  331. {
  332. return (m_Data != NULL) ? m_Data->GetWString() : NULL;
  333. }
  334. // Extract a wchar_t*
  335. //
  336. inline _bstr_t::operator wchar_t*() const throw()
  337. {
  338. return const_cast<wchar_t*>((m_Data != NULL) ? m_Data->GetWString() : NULL);
  339. }
  340. // Extract a const char_t*
  341. //
  342. inline _bstr_t::operator const char*() const throw(_com_error)
  343. {
  344. return (m_Data != NULL) ? m_Data->GetString() : NULL;
  345. }
  346. // Extract a char_t*
  347. //
  348. inline _bstr_t::operator char*() const throw(_com_error)
  349. {
  350. return const_cast<char*>((m_Data != NULL) ? m_Data->GetString() : NULL);
  351. }
  352. //////////////////////////////////////////////////////////////////////////////
  353. //
  354. // Comparison operators
  355. //
  356. //////////////////////////////////////////////////////////////////////////////
  357. inline bool _bstr_t::operator!() const throw()
  358. {
  359. return (m_Data != NULL) ? !m_Data->GetWString() : true;
  360. }
  361. inline bool _bstr_t::operator==(const _bstr_t& str) const throw()
  362. {
  363. return _Compare(str) == 0;
  364. }
  365. inline bool _bstr_t::operator!=(const _bstr_t& str) const throw()
  366. {
  367. return _Compare(str) != 0;
  368. }
  369. inline bool _bstr_t::operator<(const _bstr_t& str) const throw()
  370. {
  371. return _Compare(str) < 0;
  372. }
  373. inline bool _bstr_t::operator>(const _bstr_t& str) const throw()
  374. {
  375. return _Compare(str) > 0;
  376. }
  377. inline bool _bstr_t::operator<=(const _bstr_t& str) const throw()
  378. {
  379. return _Compare(str) <= 0;
  380. }
  381. inline bool _bstr_t::operator>=(const _bstr_t& str) const throw()
  382. {
  383. return _Compare(str) >= 0;
  384. }
  385. //////////////////////////////////////////////////////////////////////////////
  386. //
  387. // Low-level help functions
  388. //
  389. //////////////////////////////////////////////////////////////////////////////
  390. // Extract a copy of the wrapped BSTR
  391. //
  392. inline BSTR _bstr_t::copy(bool fCopy) const throw(_com_error)
  393. {
  394. return (m_Data != NULL) ? (fCopy ? m_Data->Copy() : m_Data->GetWString()) : NULL;
  395. }
  396. // Return the length of the wrapped BSTR
  397. //
  398. inline unsigned int _bstr_t::length() const throw()
  399. {
  400. return (m_Data != NULL) ? m_Data->Length() : 0;
  401. }
  402. // Binary string assign
  403. //
  404. inline void _bstr_t::Assign(BSTR s) throw(_com_error)
  405. {
  406. _COM_ASSERT( m_Data->GetWString() != s);
  407. _Free();
  408. m_Data = new Data_t(s, TRUE);
  409. if (m_Data == NULL) {
  410. _com_issue_error(E_OUTOFMEMORY);
  411. }
  412. }
  413. // Get the physical BSTR
  414. //
  415. inline BSTR& _bstr_t::GetBSTR() throw(_com_error)
  416. {
  417. if (m_Data == NULL) {
  418. m_Data = new Data_t(0, FALSE);
  419. if (m_Data == NULL) {
  420. _com_issue_error(E_OUTOFMEMORY);
  421. }
  422. }
  423. return m_Data->GetWString();
  424. }
  425. // Get the address of the physical BSTR to pass as an 'out'-parameter
  426. //
  427. inline BSTR* _bstr_t::GetAddress() throw(_com_error)
  428. {
  429. Attach(0);
  430. return &m_Data->GetWString();
  431. }
  432. // Attach to the internal BSTR w/o copying
  433. //
  434. inline void _bstr_t::Attach(BSTR s) throw(_com_error)
  435. {
  436. _Free();
  437. m_Data = new Data_t(s, FALSE);
  438. if (m_Data == NULL) {
  439. _com_issue_error(E_OUTOFMEMORY);
  440. }
  441. }
  442. // Detach the internal BSTR
  443. //
  444. inline BSTR _bstr_t::Detach() throw()
  445. {
  446. _COM_ASSERT(m_Data->RefCount() == 1);
  447. BSTR b = m_Data->GetWString();
  448. m_Data->GetWString() = NULL;
  449. _Free();
  450. return b;
  451. }
  452. // AddRef the BSTR
  453. //
  454. inline void _bstr_t::_AddRef() throw()
  455. {
  456. if (m_Data != NULL) {
  457. m_Data->AddRef();
  458. }
  459. }
  460. // Free the BSTR
  461. //
  462. inline void _bstr_t::_Free() throw()
  463. {
  464. if (m_Data != NULL) {
  465. m_Data->Release();
  466. m_Data = NULL;
  467. }
  468. }
  469. // Compare two _bstr_t objects
  470. //
  471. inline int _bstr_t::_Compare(const _bstr_t& str) const throw()
  472. {
  473. if (m_Data == str.m_Data) {
  474. return 0;
  475. }
  476. if (m_Data == NULL) {
  477. return -1;
  478. }
  479. if (str.m_Data == NULL) {
  480. return 1;
  481. }
  482. return m_Data->Compare(*str.m_Data);
  483. }
  484. //////////////////////////////////////////////////////////////////////////////
  485. //
  486. // Reference counted wrapper - Constructors
  487. //
  488. //////////////////////////////////////////////////////////////////////////////
  489. // Construct a Data_t from a const char*
  490. //
  491. inline _bstr_t::Data_t::Data_t(const char* s) throw(_com_error)
  492. : m_str(NULL), m_RefCount(1)
  493. {
  494. m_wstr = _com_util::ConvertStringToBSTR(s);
  495. }
  496. // Construct a Data_t from a const wchar_t*
  497. //
  498. inline _bstr_t::Data_t::Data_t(const wchar_t* s) throw(_com_error)
  499. : m_str(NULL), m_RefCount(1)
  500. {
  501. m_wstr = ::SysAllocString(s);
  502. if (m_wstr == NULL && s != NULL) {
  503. _com_issue_error(E_OUTOFMEMORY);
  504. }
  505. }
  506. // Construct a Data_t from a BSTR. If fCopy is FALSE, give control of
  507. // data to the Data_t without doing a SysAllocStringByteLen.
  508. //
  509. inline _bstr_t::Data_t::Data_t(BSTR bstr, bool fCopy) throw(_com_error)
  510. : m_str(NULL), m_RefCount(1)
  511. {
  512. if (fCopy && bstr != NULL) {
  513. m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
  514. ::SysStringByteLen(bstr));
  515. if (m_wstr == NULL) {
  516. _com_issue_error(E_OUTOFMEMORY);
  517. }
  518. }
  519. else {
  520. m_wstr = bstr;
  521. }
  522. }
  523. // Construct a Data_t from the concatenation of two _bstr_t objects
  524. //
  525. inline _bstr_t::Data_t::Data_t(const _bstr_t& s1, const _bstr_t& s2) throw(_com_error)
  526. : m_str(NULL), m_RefCount(1)
  527. {
  528. const unsigned int l1 = s1.length();
  529. const unsigned int l2 = s2.length();
  530. m_wstr = ::SysAllocStringByteLen(NULL, (l1 + l2) * sizeof(wchar_t));
  531. if (m_wstr == NULL) {
  532. if (l1 + l2 == 0) {
  533. return;
  534. }
  535. _com_issue_error(E_OUTOFMEMORY);
  536. }
  537. const wchar_t* wstr1 = static_cast<const wchar_t*>(s1);
  538. if (wstr1 != NULL) {
  539. memcpy(m_wstr, wstr1, (l1 + 1) * sizeof(wchar_t));
  540. }
  541. const wchar_t* wstr2 = static_cast<const wchar_t*>(s2);
  542. if (wstr2 != NULL) {
  543. memcpy(m_wstr + l1, wstr2, (l2 + 1) * sizeof(wchar_t));
  544. }
  545. }
  546. //////////////////////////////////////////////////////////////////////////////
  547. //
  548. // Reference counted wrapper - reference counting routines
  549. //
  550. //////////////////////////////////////////////////////////////////////////////
  551. inline unsigned long _bstr_t::Data_t::AddRef() throw()
  552. {
  553. InterlockedIncrement(reinterpret_cast<long*>(&m_RefCount));
  554. return m_RefCount;
  555. }
  556. inline unsigned long _bstr_t::Data_t::Release() throw()
  557. {
  558. if (!InterlockedDecrement(reinterpret_cast<long*>(&m_RefCount))) {
  559. delete this;
  560. return 0;
  561. }
  562. return m_RefCount;
  563. }
  564. inline unsigned long _bstr_t::Data_t::RefCount() const throw()
  565. {
  566. return m_RefCount;
  567. }
  568. //////////////////////////////////////////////////////////////////////////////
  569. //
  570. // Reference counted wrapper - extractors
  571. //
  572. //////////////////////////////////////////////////////////////////////////////
  573. // Extract a const wchar_t*
  574. //
  575. inline _bstr_t::Data_t::operator const wchar_t*() const throw()
  576. {
  577. return m_wstr;
  578. }
  579. // Extract a const char_t*
  580. //
  581. inline _bstr_t::Data_t::operator const char*() const throw(_com_error)
  582. {
  583. return GetString();
  584. }
  585. //////////////////////////////////////////////////////////////////////////////
  586. //
  587. // Reference counted wrapper - helper functions
  588. //
  589. //////////////////////////////////////////////////////////////////////////////
  590. inline const wchar_t* _bstr_t::Data_t::GetWString() const throw()
  591. {
  592. return m_wstr;
  593. }
  594. inline wchar_t*& _bstr_t::Data_t::GetWString() throw()
  595. {
  596. return m_wstr;
  597. }
  598. inline const char* _bstr_t::Data_t::GetString() const throw(_com_error)
  599. {
  600. if (m_str == NULL) {
  601. m_str = _com_util::ConvertBSTRToString(m_wstr);
  602. }
  603. return m_str;
  604. }
  605. // Return a copy of the wrapped BSTR
  606. //
  607. inline BSTR _bstr_t::Data_t::Copy() const throw(_com_error)
  608. {
  609. if (m_wstr != NULL) {
  610. BSTR bstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(m_wstr), ::SysStringByteLen(m_wstr));
  611. if (bstr == NULL) {
  612. _com_issue_error(E_OUTOFMEMORY);
  613. }
  614. return bstr;
  615. }
  616. return NULL;
  617. }
  618. inline void _bstr_t::Data_t::Assign(BSTR s) throw(_com_error)
  619. {
  620. _Free();
  621. if (s != NULL) {
  622. m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(s), ::SysStringByteLen(s));
  623. m_str = 0;
  624. }
  625. }
  626. inline void _bstr_t::Data_t::Attach(BSTR s) throw()
  627. {
  628. _Free();
  629. m_wstr = s;
  630. m_str = 0;
  631. m_RefCount = 1;
  632. }
  633. // Return the length of the wrapper BSTR
  634. //
  635. inline unsigned int _bstr_t::Data_t::Length() const throw()
  636. {
  637. return m_wstr ? ::SysStringLen(m_wstr) : 0;
  638. }
  639. // Compare two wrapped BSTRs
  640. //
  641. inline int _bstr_t::Data_t::Compare(const _bstr_t::Data_t& str) const throw()
  642. {
  643. if (m_wstr == NULL) {
  644. return str.m_wstr ? -1 : 0;
  645. }
  646. if (str.m_wstr == NULL) {
  647. return 1;
  648. }
  649. const unsigned int l1 = ::SysStringLen(m_wstr);
  650. const unsigned int l2 = ::SysStringLen(str.m_wstr);
  651. unsigned int len = l1;
  652. if (len > l2) {
  653. len = l2;
  654. }
  655. BSTR bstr1 = m_wstr;
  656. BSTR bstr2 = str.m_wstr;
  657. while (len-- > 0) {
  658. if (*bstr1++ != *bstr2++) {
  659. return bstr1[-1] - bstr2[-1];
  660. }
  661. }
  662. return (l1 < l2) ? -1 : (l1 == l2) ? 0 : 1;
  663. }
  664. // Exception agnostic wrapper for new
  665. //
  666. #ifdef _COM_OPERATOR_NEW_THROWS
  667. inline void* _bstr_t::Data_t::operator new(size_t sz) {
  668. try {
  669. return ::operator new(sz);
  670. } catch (...) {
  671. return NULL;
  672. }
  673. }
  674. #else // _COM_OPERATOR_NEW_THROWS
  675. inline void* _bstr_t::Data_t::operator new(size_t sz) {
  676. return ::operator new(sz);
  677. }
  678. #endif // _COM_OPERATOR_NEW_THROWS
  679. // Destruct this object
  680. //
  681. inline _bstr_t::Data_t::~Data_t() throw()
  682. {
  683. _Free();
  684. }
  685. // Free up this object
  686. //
  687. inline void _bstr_t::Data_t::_Free() throw()
  688. {
  689. if (m_wstr != NULL) {
  690. ::SysFreeString(m_wstr);
  691. }
  692. if (m_str != NULL) {
  693. delete [] m_str;
  694. }
  695. }
  696. //////////////////////////////////////////////////////////////////////////////
  697. //
  698. // Wrapper class for VARIANT
  699. //
  700. //////////////////////////////////////////////////////////////////////////////
  701. /*
  702. * VARENUM usage key,
  703. *
  704. * * [V] - may appear in a VARIANT
  705. * * [T] - may appear in a TYPEDESC
  706. * * [P] - may appear in an OLE property set
  707. * * [S] - may appear in a Safe Array
  708. * * [C] - supported by class _variant_t
  709. *
  710. *
  711. * VT_EMPTY [V] [P] nothing
  712. * VT_NULL [V] [P] SQL style Null
  713. * VT_I2 [V][T][P][S][C] 2 byte signed int
  714. * VT_I4 [V][T][P][S][C] 4 byte signed int
  715. * VT_R4 [V][T][P][S][C] 4 byte real
  716. * VT_R8 [V][T][P][S][C] 8 byte real
  717. * VT_CY [V][T][P][S][C] currency
  718. * VT_DATE [V][T][P][S][C] date
  719. * VT_BSTR [V][T][P][S][C] OLE Automation string
  720. * VT_DISPATCH [V][T][P][S][C] IDispatch *
  721. * VT_ERROR [V][T] [S][C] SCODE
  722. * VT_BOOL [V][T][P][S][C] True=-1, False=0
  723. * VT_VARIANT [V][T][P][S] VARIANT *
  724. * VT_UNKNOWN [V][T] [S][C] IUnknown *
  725. * VT_DECIMAL [V][T] [S][C] 16 byte fixed point
  726. * VT_I1 [T] signed char
  727. * VT_UI1 [V][T][P][S][C] unsigned char
  728. * VT_UI2 [T][P] unsigned short
  729. * VT_UI4 [T][P] unsigned short
  730. * VT_I8 [T][P] signed 64-bit int
  731. * VT_UI8 [T][P] unsigned 64-bit int
  732. * VT_INT [T] signed machine int
  733. * VT_UINT [T] unsigned machine int
  734. * VT_VOID [T] C style void
  735. * VT_HRESULT [T] Standard return type
  736. * VT_PTR [T] pointer type
  737. * VT_SAFEARRAY [T] (use VT_ARRAY in VARIANT)
  738. * VT_CARRAY [T] C style array
  739. * VT_USERDEFINED [T] user defined type
  740. * VT_LPSTR [T][P] null terminated string
  741. * VT_LPWSTR [T][P] wide null terminated string
  742. * VT_FILETIME [P] FILETIME
  743. * VT_BLOB [P] Length prefixed bytes
  744. * VT_STREAM [P] Name of the stream follows
  745. * VT_STORAGE [P] Name of the storage follows
  746. * VT_STREAMED_OBJECT [P] Stream contains an object
  747. * VT_STORED_OBJECT [P] Storage contains an object
  748. * VT_BLOB_OBJECT [P] Blob contains an object
  749. * VT_CF [P] Clipboard format
  750. * VT_CLSID [P] A Class ID
  751. * VT_VECTOR [P] simple counted array
  752. * VT_ARRAY [V] SAFEARRAY*
  753. * VT_BYREF [V] void* for local use
  754. */
  755. class _variant_t : public ::tagVARIANT {
  756. public:
  757. // Constructors
  758. //
  759. _variant_t() throw();
  760. _variant_t(const VARIANT& varSrc) throw(_com_error);
  761. _variant_t(const VARIANT* pSrc) throw(_com_error);
  762. _variant_t(const _variant_t& varSrc) throw(_com_error);
  763. _variant_t(VARIANT& varSrc, bool fCopy) throw(_com_error); // Attach VARIANT if !fCopy
  764. _variant_t(short sSrc, VARTYPE vtSrc = VT_I2) throw(_com_error); // Creates a VT_I2, or a VT_BOOL
  765. _variant_t(long lSrc, VARTYPE vtSrc = VT_I4) throw(_com_error); // Creates a VT_I4, a VT_ERROR, or a VT_BOOL
  766. _variant_t(float fltSrc) throw(); // Creates a VT_R4
  767. _variant_t(double dblSrc, VARTYPE vtSrc = VT_R8) throw(_com_error); // Creates a VT_R8, or a VT_DATE
  768. _variant_t(const CY& cySrc) throw(); // Creates a VT_CY
  769. _variant_t(const _bstr_t& bstrSrc) throw(_com_error); // Creates a VT_BSTR
  770. _variant_t(const wchar_t *pSrc) throw(_com_error); // Creates a VT_BSTR
  771. _variant_t(const char* pSrc) throw(_com_error); // Creates a VT_BSTR
  772. _variant_t(IDispatch* pSrc, bool fAddRef = true) throw(); // Creates a VT_DISPATCH
  773. _variant_t(bool boolSrc) throw(); // Creates a VT_BOOL
  774. _variant_t(IUnknown* pSrc, bool fAddRef = true) throw(); // Creates a VT_UNKNOWN
  775. _variant_t(const DECIMAL& decSrc) throw(); // Creates a VT_DECIMAL
  776. _variant_t(BYTE bSrc) throw(); // Creates a VT_UI1
  777. _variant_t(char cSrc) throw(); // Creates a VT_I1
  778. _variant_t(unsigned short usSrc) throw(); // Creates a VT_UI2
  779. _variant_t(unsigned long ulSrc) throw(); // Creates a VT_UI4
  780. _variant_t(int iSrc) throw(); // Creates a VT_INT
  781. _variant_t(unsigned int uiSrc) throw(); // Creates a VT_UINT
  782. _variant_t(__int64 i8Src) throw(); // Creates a VT_I8
  783. _variant_t(unsigned __int64 ui8Src) throw(); // Creates a VT_UI8
  784. // Destructor
  785. //
  786. ~_variant_t() throw(_com_error);
  787. // Extractors
  788. //
  789. operator short() const throw(_com_error); // Extracts a short from a VT_I2
  790. operator long() const throw(_com_error); // Extracts a long from a VT_I4
  791. operator float() const throw(_com_error); // Extracts a float from a VT_R4
  792. operator double() const throw(_com_error); // Extracts a double from a VT_R8
  793. operator CY() const throw(_com_error); // Extracts a CY from a VT_CY
  794. operator _bstr_t() const throw(_com_error); // Extracts a _bstr_t from a VT_BSTR
  795. operator IDispatch*() const throw(_com_error); // Extracts a IDispatch* from a VT_DISPATCH
  796. operator bool() const throw(_com_error); // Extracts a bool from a VT_BOOL
  797. operator IUnknown*() const throw(_com_error); // Extracts a IUnknown* from a VT_UNKNOWN
  798. operator DECIMAL() const throw(_com_error); // Extracts a DECIMAL from a VT_DECIMAL
  799. operator BYTE() const throw(_com_error); // Extracts a BTYE (unsigned char) from a VT_UI1
  800. operator VARIANT() const throw();
  801. operator char() const throw(_com_error); // Extracts a char from a VT_I1
  802. operator unsigned short() const throw(_com_error); // Extracts a unsigned short from a VT_UI2
  803. operator unsigned long() const throw(_com_error); // Extracts a unsigned long from a VT_UI4
  804. operator int() const throw(_com_error); // Extracts a int from a VT_INT
  805. operator unsigned int() const throw(_com_error); // Extracts a unsigned int from a VT_UINT
  806. operator __int64() const throw(_com_error); // Extracts a __int64 from a VT_I8
  807. operator unsigned __int64() const throw(_com_error); // Extracts a unsigned __int64 from a VT_UI8
  808. // Assignment operations
  809. //
  810. _variant_t& operator=(const VARIANT& varSrc) throw(_com_error);
  811. _variant_t& operator=(const VARIANT* pSrc) throw(_com_error);
  812. _variant_t& operator=(const _variant_t& varSrc) throw(_com_error);
  813. _variant_t& operator=(short sSrc) throw(_com_error); // Assign a VT_I2, or a VT_BOOL
  814. _variant_t& operator=(long lSrc) throw(_com_error); // Assign a VT_I4, a VT_ERROR or a VT_BOOL
  815. _variant_t& operator=(float fltSrc) throw(_com_error); // Assign a VT_R4
  816. _variant_t& operator=(double dblSrc) throw(_com_error); // Assign a VT_R8, or a VT_DATE
  817. _variant_t& operator=(const CY& cySrc) throw(_com_error); // Assign a VT_CY
  818. _variant_t& operator=(const _bstr_t& bstrSrc) throw(_com_error); // Assign a VT_BSTR
  819. _variant_t& operator=(const wchar_t* pSrc) throw(_com_error); // Assign a VT_BSTR
  820. _variant_t& operator=(const char* pSrc) throw(_com_error); // Assign a VT_BSTR
  821. _variant_t& operator=(IDispatch* pSrc) throw(_com_error); // Assign a VT_DISPATCH
  822. _variant_t& operator=(bool boolSrc) throw(_com_error); // Assign a VT_BOOL
  823. _variant_t& operator=(IUnknown* pSrc) throw(_com_error); // Assign a VT_UNKNOWN
  824. _variant_t& operator=(const DECIMAL& decSrc) throw(_com_error); // Assign a VT_DECIMAL
  825. _variant_t& operator=(BYTE bSrc) throw(_com_error); // Assign a VT_UI1
  826. _variant_t& operator=(char cSrc) throw(_com_error); // Assign a VT_I1
  827. _variant_t& operator=(unsigned short usSrc) throw(_com_error); // Assign a VT_UI2
  828. _variant_t& operator=(unsigned long ulSrc) throw(_com_error); // Assign a VT_UI4
  829. _variant_t& operator=(int iSrc) throw(_com_error); // Assign a VT_INT
  830. _variant_t& operator=(unsigned int uiSrc) throw(_com_error); // Assign a VT_UINT
  831. _variant_t& operator=(__int64 i8Src) throw(_com_error); // Assign a VT_I8
  832. _variant_t& operator=(unsigned __int64 ui8Src) throw(_com_error); // Assign a VT_UI8
  833. // Comparison operations
  834. //
  835. bool operator==(const VARIANT& varSrc) const throw(_com_error);
  836. bool operator==(const VARIANT* pSrc) const throw(_com_error);
  837. bool operator!=(const VARIANT& varSrc) const throw(_com_error);
  838. bool operator!=(const VARIANT* pSrc) const throw(_com_error);
  839. // Low-level operations
  840. //
  841. void Clear() throw(_com_error);
  842. void Attach(VARIANT& varSrc) throw(_com_error);
  843. VARIANT Detach() throw(_com_error);
  844. VARIANT& GetVARIANT() throw();
  845. VARIANT* GetAddress() throw(_com_error);
  846. void ChangeType(VARTYPE vartype, const _variant_t* pSrc = NULL) throw(_com_error);
  847. void SetString(const char* pSrc) throw(_com_error); // used to set ANSI string
  848. };
  849. //////////////////////////////////////////////////////////////////////////////////////////
  850. //
  851. // Constructors
  852. //
  853. //////////////////////////////////////////////////////////////////////////////////////////
  854. // Default constructor
  855. //
  856. inline _variant_t::_variant_t() throw()
  857. {
  858. ::VariantInit(this);
  859. }
  860. // Construct a _variant_t from a const VARIANT&
  861. //
  862. inline _variant_t::_variant_t(const VARIANT& varSrc) throw(_com_error)
  863. {
  864. ::VariantInit(this);
  865. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
  866. }
  867. // Construct a _variant_t from a const VARIANT*
  868. //
  869. inline _variant_t::_variant_t(const VARIANT* pSrc) throw(_com_error)
  870. {
  871. ::VariantInit(this);
  872. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
  873. }
  874. // Construct a _variant_t from a const _variant_t&
  875. //
  876. inline _variant_t::_variant_t(const _variant_t& varSrc) throw(_com_error)
  877. {
  878. ::VariantInit(this);
  879. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
  880. }
  881. // Construct a _variant_t from a VARIANT&. If fCopy is FALSE, give control of
  882. // data to the _variant_t without doing a VariantCopy.
  883. //
  884. inline _variant_t::_variant_t(VARIANT& varSrc, bool fCopy) throw(_com_error)
  885. {
  886. if (fCopy) {
  887. ::VariantInit(this);
  888. _com_util::CheckError(::VariantCopy(this, &varSrc));
  889. } else {
  890. memcpy(this, &varSrc, sizeof(varSrc));
  891. V_VT(&varSrc) = VT_EMPTY;
  892. }
  893. }
  894. // Construct either a VT_I2 VARIANT or a VT_BOOL VARIANT from
  895. // a short (the default is VT_I2)
  896. //
  897. inline _variant_t::_variant_t(short sSrc, VARTYPE vtSrc) throw(_com_error)
  898. {
  899. if ((vtSrc != VT_I2) && (vtSrc != VT_BOOL)) {
  900. _com_issue_error(E_INVALIDARG);
  901. }
  902. if (vtSrc == VT_BOOL) {
  903. V_VT(this) = VT_BOOL;
  904. V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
  905. }
  906. else {
  907. V_VT(this) = VT_I2;
  908. V_I2(this) = sSrc;
  909. }
  910. }
  911. // Construct either a VT_I4 VARIANT, a VT_BOOL VARIANT, or a
  912. // VT_ERROR VARIANT from a long (the default is VT_I4)
  913. //
  914. inline _variant_t::_variant_t(long lSrc, VARTYPE vtSrc) throw(_com_error)
  915. {
  916. if ((vtSrc != VT_I4) && (vtSrc != VT_ERROR) && (vtSrc != VT_BOOL)) {
  917. _com_issue_error(E_INVALIDARG);
  918. }
  919. if (vtSrc == VT_ERROR) {
  920. V_VT(this) = VT_ERROR;
  921. V_ERROR(this) = lSrc;
  922. }
  923. else if (vtSrc == VT_BOOL) {
  924. V_VT(this) = VT_BOOL;
  925. V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
  926. }
  927. else {
  928. V_VT(this) = VT_I4;
  929. V_I4(this) = lSrc;
  930. }
  931. }
  932. // Construct a VT_R4 VARIANT from a float
  933. //
  934. inline _variant_t::_variant_t(float fltSrc) throw()
  935. {
  936. V_VT(this) = VT_R4;
  937. V_R4(this) = fltSrc;
  938. }
  939. // Construct either a VT_R8 VARIANT, or a VT_DATE VARIANT from
  940. // a double (the default is VT_R8)
  941. //
  942. inline _variant_t::_variant_t(double dblSrc, VARTYPE vtSrc) throw(_com_error)
  943. {
  944. if ((vtSrc != VT_R8) && (vtSrc != VT_DATE)) {
  945. _com_issue_error(E_INVALIDARG);
  946. }
  947. if (vtSrc == VT_DATE) {
  948. V_VT(this) = VT_DATE;
  949. V_DATE(this) = dblSrc;
  950. }
  951. else {
  952. V_VT(this) = VT_R8;
  953. V_R8(this) = dblSrc;
  954. }
  955. }
  956. // Construct a VT_CY from a CY
  957. //
  958. inline _variant_t::_variant_t(const CY& cySrc) throw()
  959. {
  960. V_VT(this) = VT_CY;
  961. V_CY(this) = cySrc;
  962. }
  963. // Construct a VT_BSTR VARIANT from a const _bstr_t&
  964. //
  965. inline _variant_t::_variant_t(const _bstr_t& bstrSrc) throw(_com_error)
  966. {
  967. V_VT(this) = VT_BSTR;
  968. BSTR bstr = static_cast<wchar_t*>(bstrSrc);
  969. V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
  970. ::SysStringByteLen(bstr));
  971. if (V_BSTR(this) == NULL) {
  972. _com_issue_error(E_OUTOFMEMORY);
  973. }
  974. }
  975. // Construct a VT_BSTR VARIANT from a const wchar_t*
  976. //
  977. inline _variant_t::_variant_t(const wchar_t* pSrc) throw(_com_error)
  978. {
  979. V_VT(this) = VT_BSTR;
  980. V_BSTR(this) = ::SysAllocString(pSrc);
  981. if (V_BSTR(this) == NULL && pSrc != NULL) {
  982. _com_issue_error(E_OUTOFMEMORY);
  983. }
  984. }
  985. // Construct a VT_BSTR VARIANT from a const char*
  986. //
  987. inline _variant_t::_variant_t(const char* pSrc) throw(_com_error)
  988. {
  989. V_VT(this) = VT_BSTR;
  990. V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
  991. }
  992. // Construct a VT_DISPATCH VARIANT from an IDispatch*
  993. //
  994. inline _variant_t::_variant_t(IDispatch* pSrc, bool fAddRef) throw()
  995. {
  996. V_VT(this) = VT_DISPATCH;
  997. V_DISPATCH(this) = pSrc;
  998. // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
  999. // false indicates we're taking ownership
  1000. //
  1001. if (fAddRef) {
  1002. V_DISPATCH(this)->AddRef();
  1003. }
  1004. }
  1005. // Construct a VT_BOOL VARIANT from a bool
  1006. //
  1007. inline _variant_t::_variant_t(bool boolSrc) throw()
  1008. {
  1009. V_VT(this) = VT_BOOL;
  1010. V_BOOL(this) = (boolSrc ? VARIANT_TRUE : VARIANT_FALSE);
  1011. }
  1012. // Construct a VT_UNKNOWN VARIANT from an IUnknown*
  1013. //
  1014. inline _variant_t::_variant_t(IUnknown* pSrc, bool fAddRef) throw()
  1015. {
  1016. V_VT(this) = VT_UNKNOWN;
  1017. V_UNKNOWN(this) = pSrc;
  1018. // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
  1019. // false indicates we're taking ownership
  1020. //
  1021. if (fAddRef) {
  1022. V_UNKNOWN(this)->AddRef();
  1023. }
  1024. }
  1025. // Construct a VT_DECIMAL VARIANT from a DECIMAL
  1026. //
  1027. inline _variant_t::_variant_t(const DECIMAL& decSrc) throw()
  1028. {
  1029. // Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
  1030. //
  1031. V_DECIMAL(this) = decSrc;
  1032. V_VT(this) = VT_DECIMAL;
  1033. }
  1034. // Construct a VT_UI1 VARIANT from a BYTE (unsigned char)
  1035. //
  1036. inline _variant_t::_variant_t(BYTE bSrc) throw()
  1037. {
  1038. V_VT(this) = VT_UI1;
  1039. V_UI1(this) = bSrc;
  1040. }
  1041. // Construct a VT_I1 VARIANT from a char
  1042. //
  1043. inline _variant_t::_variant_t(char cSrc) throw()
  1044. {
  1045. V_VT(this) = VT_I1;
  1046. V_I1(this) = cSrc;
  1047. }
  1048. // Construct a VT_UI2 VARIANT from a unsigned short
  1049. //
  1050. inline _variant_t::_variant_t(unsigned short usSrc) throw()
  1051. {
  1052. V_VT(this) = VT_UI2;
  1053. V_UI2(this) = usSrc;
  1054. }
  1055. // Construct a VT_UI4 VARIANT from a unsigned long
  1056. //
  1057. inline _variant_t::_variant_t(unsigned long ulSrc) throw()
  1058. {
  1059. V_VT(this) = VT_UI4;
  1060. V_UI4(this) = ulSrc;
  1061. }
  1062. // Construct a VT_INT VARIANT from a int
  1063. //
  1064. inline _variant_t::_variant_t(int iSrc) throw()
  1065. {
  1066. V_VT(this) = VT_INT;
  1067. V_INT(this) = iSrc;
  1068. }
  1069. // Construct a VT_UINT VARIANT from a unsigned int
  1070. //
  1071. inline _variant_t::_variant_t(unsigned int uiSrc) throw()
  1072. {
  1073. V_VT(this) = VT_UINT;
  1074. V_UINT(this) = uiSrc;
  1075. }
  1076. // Construct a VT_I8 VARIANT from a __int64
  1077. //
  1078. inline _variant_t::_variant_t(__int64 i8Src) throw()
  1079. {
  1080. V_VT(this) = VT_I8;
  1081. V_I8(this) = i8Src;
  1082. }
  1083. // Construct a VT_UI8 VARIANT from a unsigned __int64
  1084. //
  1085. inline _variant_t::_variant_t(unsigned __int64 ui8Src) throw()
  1086. {
  1087. V_VT(this) = VT_UI8;
  1088. V_UI8(this) = ui8Src;
  1089. }
  1090. //////////////////////////////////////////////////////////////////////////////////////////
  1091. //
  1092. // Extractors
  1093. //
  1094. //////////////////////////////////////////////////////////////////////////////////////////
  1095. // Extracts a VT_I2 into a short
  1096. //
  1097. inline _variant_t::operator short() const throw(_com_error)
  1098. {
  1099. if (V_VT(this) == VT_I2) {
  1100. return V_I2(this);
  1101. }
  1102. _variant_t varDest;
  1103. varDest.ChangeType(VT_I2, this);
  1104. return V_I2(&varDest);
  1105. }
  1106. // Extracts a VT_I4 into a long
  1107. //
  1108. inline _variant_t::operator long() const throw(_com_error)
  1109. {
  1110. if (V_VT(this) == VT_I4) {
  1111. return V_I4(this);
  1112. }
  1113. _variant_t varDest;
  1114. varDest.ChangeType(VT_I4, this);
  1115. return V_I4(&varDest);
  1116. }
  1117. // Extracts a VT_R4 into a float
  1118. //
  1119. inline _variant_t::operator float() const throw(_com_error)
  1120. {
  1121. if (V_VT(this) == VT_R4) {
  1122. return V_R4(this);
  1123. }
  1124. _variant_t varDest;
  1125. varDest.ChangeType(VT_R4, this);
  1126. return V_R4(&varDest);
  1127. }
  1128. // Extracts a VT_R8 into a double
  1129. //
  1130. inline _variant_t::operator double() const throw(_com_error)
  1131. {
  1132. if (V_VT(this) == VT_R8) {
  1133. return V_R8(this);
  1134. }
  1135. _variant_t varDest;
  1136. varDest.ChangeType(VT_R8, this);
  1137. return V_R8(&varDest);
  1138. }
  1139. // Extracts a VT_CY into a CY
  1140. //
  1141. inline _variant_t::operator CY() const throw(_com_error)
  1142. {
  1143. if (V_VT(this) == VT_CY) {
  1144. return V_CY(this);
  1145. }
  1146. _variant_t varDest;
  1147. varDest.ChangeType(VT_CY, this);
  1148. return V_CY(&varDest);
  1149. }
  1150. // Extracts a VT_BSTR into a _bstr_t
  1151. //
  1152. inline _variant_t::operator _bstr_t() const throw(_com_error)
  1153. {
  1154. if (V_VT(this) == VT_BSTR) {
  1155. return V_BSTR(this);
  1156. }
  1157. _variant_t varDest;
  1158. varDest.ChangeType(VT_BSTR, this);
  1159. return V_BSTR(&varDest);
  1160. }
  1161. // Extracts a VT_DISPATCH into an IDispatch*
  1162. //
  1163. inline _variant_t::operator IDispatch*() const throw(_com_error)
  1164. {
  1165. if (V_VT(this) == VT_DISPATCH) {
  1166. V_DISPATCH(this)->AddRef();
  1167. return V_DISPATCH(this);
  1168. }
  1169. _variant_t varDest;
  1170. varDest.ChangeType(VT_DISPATCH, this);
  1171. V_DISPATCH(&varDest)->AddRef();
  1172. return V_DISPATCH(&varDest);
  1173. }
  1174. // Extract a VT_BOOL into a bool
  1175. //
  1176. inline _variant_t::operator bool() const throw(_com_error)
  1177. {
  1178. if (V_VT(this) == VT_BOOL) {
  1179. return V_BOOL(this) ? true : false;
  1180. }
  1181. _variant_t varDest;
  1182. varDest.ChangeType(VT_BOOL, this);
  1183. return V_BOOL(&varDest) ? true : false;
  1184. }
  1185. // Extracts a VT_UNKNOWN into an IUnknown*
  1186. //
  1187. inline _variant_t::operator IUnknown*() const throw(_com_error)
  1188. {
  1189. if (V_VT(this) == VT_UNKNOWN) {
  1190. V_UNKNOWN(this)->AddRef();
  1191. return V_UNKNOWN(this);
  1192. }
  1193. _variant_t varDest;
  1194. varDest.ChangeType(VT_UNKNOWN, this);
  1195. V_UNKNOWN(&varDest)->AddRef();
  1196. return V_UNKNOWN(&varDest);
  1197. }
  1198. // Extracts a VT_DECIMAL into a DECIMAL
  1199. //
  1200. inline _variant_t::operator DECIMAL() const throw(_com_error)
  1201. {
  1202. if (V_VT(this) == VT_DECIMAL) {
  1203. return V_DECIMAL(this);
  1204. }
  1205. _variant_t varDest;
  1206. varDest.ChangeType(VT_DECIMAL, this);
  1207. return V_DECIMAL(&varDest);
  1208. }
  1209. // Extracts a VT_UI1 into a BYTE (unsigned char)
  1210. //
  1211. inline _variant_t::operator BYTE() const throw(_com_error)
  1212. {
  1213. if (V_VT(this) == VT_UI1) {
  1214. return V_UI1(this);
  1215. }
  1216. _variant_t varDest;
  1217. varDest.ChangeType(VT_UI1, this);
  1218. return V_UI1(&varDest);
  1219. }
  1220. // Extract the physical VARIANT
  1221. //
  1222. inline _variant_t::operator VARIANT() const throw()
  1223. {
  1224. return *(VARIANT*) this;
  1225. }
  1226. // Extracts a VT_I1 into a char
  1227. //
  1228. inline _variant_t::operator char() const throw(_com_error)
  1229. {
  1230. if (V_VT(this) == VT_I1) {
  1231. return V_I1(this);
  1232. }
  1233. _variant_t varDest;
  1234. varDest.ChangeType(VT_I1, this);
  1235. return V_I1(&varDest);
  1236. }
  1237. // Extracts a VT_UI2 into a unsigned short
  1238. //
  1239. inline _variant_t::operator unsigned short() const throw(_com_error)
  1240. {
  1241. if (V_VT(this) == VT_UI2) {
  1242. return V_UI2(this);
  1243. }
  1244. _variant_t varDest;
  1245. varDest.ChangeType(VT_UI2, this);
  1246. return V_UI2(&varDest);
  1247. }
  1248. // Extracts a VT_UI4 into a unsigned long
  1249. //
  1250. inline _variant_t::operator unsigned long() const throw(_com_error)
  1251. {
  1252. if (V_VT(this) == VT_UI4) {
  1253. return V_UI4(this);
  1254. }
  1255. _variant_t varDest;
  1256. varDest.ChangeType(VT_UI4, this);
  1257. return V_UI4(&varDest);
  1258. }
  1259. // Extracts a VT_INT into a int
  1260. //
  1261. inline _variant_t::operator int() const throw(_com_error)
  1262. {
  1263. if (V_VT(this) == VT_INT) {
  1264. return V_INT(this);
  1265. }
  1266. _variant_t varDest;
  1267. varDest.ChangeType(VT_INT, this);
  1268. return V_INT(&varDest);
  1269. }
  1270. // Extracts a VT_UINT into a unsigned int
  1271. //
  1272. inline _variant_t::operator unsigned int() const throw(_com_error)
  1273. {
  1274. if (V_VT(this) == VT_UINT) {
  1275. return V_UINT(this);
  1276. }
  1277. _variant_t varDest;
  1278. varDest.ChangeType(VT_UINT, this);
  1279. return V_UINT(&varDest);
  1280. }
  1281. // Extracts a VT_I8 into a __int64
  1282. //
  1283. inline _variant_t::operator __int64() const throw(_com_error)
  1284. {
  1285. if (V_VT(this) == VT_I8) {
  1286. return V_I8(this);
  1287. }
  1288. _variant_t varDest;
  1289. varDest.ChangeType(VT_I8, this);
  1290. return V_I8(&varDest);
  1291. }
  1292. // Extracts a VT_UI8 into a unsigned __int64
  1293. //
  1294. inline _variant_t::operator unsigned __int64() const throw(_com_error)
  1295. {
  1296. if (V_VT(this) == VT_UI8) {
  1297. return V_UI8(this);
  1298. }
  1299. _variant_t varDest;
  1300. varDest.ChangeType(VT_UI8, this);
  1301. return V_UI8(&varDest);
  1302. }
  1303. //////////////////////////////////////////////////////////////////////////////////////////
  1304. //
  1305. // Assignment operations
  1306. //
  1307. //////////////////////////////////////////////////////////////////////////////////////////
  1308. // Assign a const VARIANT& (::VariantCopy handles everything)
  1309. //
  1310. inline _variant_t& _variant_t::operator=(const VARIANT& varSrc) throw(_com_error)
  1311. {
  1312. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
  1313. return *this;
  1314. }
  1315. // Assign a const VARIANT* (::VariantCopy handles everything)
  1316. //
  1317. inline _variant_t& _variant_t::operator=(const VARIANT* pSrc) throw(_com_error)
  1318. {
  1319. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
  1320. return *this;
  1321. }
  1322. // Assign a const _variant_t& (::VariantCopy handles everything)
  1323. //
  1324. inline _variant_t& _variant_t::operator=(const _variant_t& varSrc) throw(_com_error)
  1325. {
  1326. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
  1327. return *this;
  1328. }
  1329. // Assign a short creating either VT_I2 VARIANT or a
  1330. // VT_BOOL VARIANT (VT_I2 is the default)
  1331. //
  1332. inline _variant_t& _variant_t::operator=(short sSrc) throw(_com_error)
  1333. {
  1334. if (V_VT(this) == VT_I2) {
  1335. V_I2(this) = sSrc;
  1336. }
  1337. else if (V_VT(this) == VT_BOOL) {
  1338. V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
  1339. }
  1340. else {
  1341. // Clear the VARIANT and create a VT_I2
  1342. //
  1343. Clear();
  1344. V_VT(this) = VT_I2;
  1345. V_I2(this) = sSrc;
  1346. }
  1347. return *this;
  1348. }
  1349. // Assign a long creating either VT_I4 VARIANT, a VT_ERROR VARIANT
  1350. // or a VT_BOOL VARIANT (VT_I4 is the default)
  1351. //
  1352. inline _variant_t& _variant_t::operator=(long lSrc) throw(_com_error)
  1353. {
  1354. if (V_VT(this) == VT_I4) {
  1355. V_I4(this) = lSrc;
  1356. }
  1357. else if (V_VT(this) == VT_ERROR) {
  1358. V_ERROR(this) = lSrc;
  1359. }
  1360. else if (V_VT(this) == VT_BOOL) {
  1361. V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
  1362. }
  1363. else {
  1364. // Clear the VARIANT and create a VT_I4
  1365. //
  1366. Clear();
  1367. V_VT(this) = VT_I4;
  1368. V_I4(this) = lSrc;
  1369. }
  1370. return *this;
  1371. }
  1372. // Assign a float creating a VT_R4 VARIANT
  1373. //
  1374. inline _variant_t& _variant_t::operator=(float fltSrc) throw(_com_error)
  1375. {
  1376. if (V_VT(this) != VT_R4) {
  1377. // Clear the VARIANT and create a VT_R4
  1378. //
  1379. Clear();
  1380. V_VT(this) = VT_R4;
  1381. }
  1382. V_R4(this) = fltSrc;
  1383. return *this;
  1384. }
  1385. // Assign a double creating either a VT_R8 VARIANT, or a VT_DATE
  1386. // VARIANT (VT_R8 is the default)
  1387. //
  1388. inline _variant_t& _variant_t::operator=(double dblSrc) throw(_com_error)
  1389. {
  1390. if (V_VT(this) == VT_R8) {
  1391. V_R8(this) = dblSrc;
  1392. }
  1393. else if(V_VT(this) == VT_DATE) {
  1394. V_DATE(this) = dblSrc;
  1395. }
  1396. else {
  1397. // Clear the VARIANT and create a VT_R8
  1398. //
  1399. Clear();
  1400. V_VT(this) = VT_R8;
  1401. V_R8(this) = dblSrc;
  1402. }
  1403. return *this;
  1404. }
  1405. // Assign a CY creating a VT_CY VARIANT
  1406. //
  1407. inline _variant_t& _variant_t::operator=(const CY& cySrc) throw(_com_error)
  1408. {
  1409. if (V_VT(this) != VT_CY) {
  1410. // Clear the VARIANT and create a VT_CY
  1411. //
  1412. Clear();
  1413. V_VT(this) = VT_CY;
  1414. }
  1415. V_CY(this) = cySrc;
  1416. return *this;
  1417. }
  1418. // Assign a const _bstr_t& creating a VT_BSTR VARIANT
  1419. //
  1420. inline _variant_t& _variant_t::operator=(const _bstr_t& bstrSrc) throw(_com_error)
  1421. {
  1422. // Clear the VARIANT (This will SysFreeString() any previous occupant)
  1423. //
  1424. _COM_ASSERT(V_BSTR(this) != (BSTR) bstrSrc);
  1425. Clear();
  1426. V_VT(this) = VT_BSTR;
  1427. if (!bstrSrc) {
  1428. V_BSTR(this) = NULL;
  1429. }
  1430. else {
  1431. BSTR bstr = static_cast<wchar_t*>(bstrSrc);
  1432. V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
  1433. ::SysStringByteLen(bstr));
  1434. if (V_BSTR(this) == NULL) {
  1435. _com_issue_error(E_OUTOFMEMORY);
  1436. }
  1437. }
  1438. return *this;
  1439. }
  1440. // Assign a const wchar_t* creating a VT_BSTR VARIANT
  1441. //
  1442. inline _variant_t& _variant_t::operator=(const wchar_t* pSrc) throw(_com_error)
  1443. {
  1444. // Clear the VARIANT (This will SysFreeString() any previous occupant)
  1445. //
  1446. _COM_ASSERT(V_BSTR(this) != pSrc);
  1447. Clear();
  1448. V_VT(this) = VT_BSTR;
  1449. if (pSrc == NULL) {
  1450. V_BSTR(this) = NULL;
  1451. }
  1452. else {
  1453. V_BSTR(this) = ::SysAllocString(pSrc);
  1454. if (V_BSTR(this) == NULL) {
  1455. _com_issue_error(E_OUTOFMEMORY);
  1456. }
  1457. }
  1458. return *this;
  1459. }
  1460. // Assign a const char* creating a VT_BSTR VARIANT
  1461. //
  1462. inline _variant_t& _variant_t::operator=(const char* pSrc) throw(_com_error)
  1463. {
  1464. // Clear the VARIANT (This will SysFreeString() any previous occupant)
  1465. //
  1466. _COM_ASSERT(V_I1REF(this) != pSrc);
  1467. Clear();
  1468. V_VT(this) = VT_BSTR;
  1469. V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
  1470. return *this;
  1471. }
  1472. // Assign an IDispatch* creating a VT_DISPATCH VARIANT
  1473. //
  1474. inline _variant_t& _variant_t::operator=(IDispatch* pSrc) throw(_com_error)
  1475. {
  1476. // Clear the VARIANT (This will Release() any previous occupant)
  1477. //
  1478. _COM_ASSERT(V_DISPATCH(this) != pSrc);
  1479. Clear();
  1480. V_VT(this) = VT_DISPATCH;
  1481. V_DISPATCH(this) = pSrc;
  1482. // Need the AddRef() as VariantClear() calls Release()
  1483. //
  1484. V_DISPATCH(this)->AddRef();
  1485. return *this;
  1486. }
  1487. // Assign a bool creating a VT_BOOL VARIANT
  1488. //
  1489. inline _variant_t& _variant_t::operator=(bool boolSrc) throw(_com_error)
  1490. {
  1491. if (V_VT(this) != VT_BOOL) {
  1492. // Clear the VARIANT and create a VT_BOOL
  1493. //
  1494. Clear();
  1495. V_VT(this) = VT_BOOL;
  1496. }
  1497. V_BOOL(this) = (boolSrc ? VARIANT_TRUE : VARIANT_FALSE);
  1498. return *this;
  1499. }
  1500. // Assign an IUnknown* creating a VT_UNKNOWN VARIANT
  1501. //
  1502. inline _variant_t& _variant_t::operator=(IUnknown* pSrc) throw(_com_error)
  1503. {
  1504. // Clear VARIANT (This will Release() any previous occupant)
  1505. //
  1506. _COM_ASSERT(V_UNKNOWN(this) != pSrc);
  1507. Clear();
  1508. V_VT(this) = VT_UNKNOWN;
  1509. V_UNKNOWN(this) = pSrc;
  1510. // Need the AddRef() as VariantClear() calls Release()
  1511. //
  1512. V_UNKNOWN(this)->AddRef();
  1513. return *this;
  1514. }
  1515. // Assign a DECIMAL creating a VT_DECIMAL VARIANT
  1516. //
  1517. inline _variant_t& _variant_t::operator=(const DECIMAL& decSrc) throw(_com_error)
  1518. {
  1519. if (V_VT(this) != VT_DECIMAL) {
  1520. // Clear the VARIANT
  1521. //
  1522. Clear();
  1523. }
  1524. // Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
  1525. V_DECIMAL(this) = decSrc;
  1526. V_VT(this) = VT_DECIMAL;
  1527. return *this;
  1528. }
  1529. // Assign a BTYE (unsigned char) creating a VT_UI1 VARIANT
  1530. //
  1531. inline _variant_t& _variant_t::operator=(BYTE bSrc) throw(_com_error)
  1532. {
  1533. if (V_VT(this) != VT_UI1) {
  1534. // Clear the VARIANT and create a VT_UI1
  1535. //
  1536. Clear();
  1537. V_VT(this) = VT_UI1;
  1538. }
  1539. V_UI1(this) = bSrc;
  1540. return *this;
  1541. }
  1542. // Assign a char creating a VT_I1 VARIANT
  1543. //
  1544. inline _variant_t& _variant_t::operator=(char cSrc) throw(_com_error){
  1545. if (V_VT(this) != VT_I1) {
  1546. // Clear the VARIANT and create a VT_I1
  1547. //
  1548. Clear();
  1549. V_VT(this) = VT_I1;
  1550. }
  1551. V_I1(this) = cSrc;
  1552. return *this;
  1553. }
  1554. // Assign a char creating a VT_UI2 VARIANT
  1555. //
  1556. inline _variant_t& _variant_t::operator=(unsigned short usSrc) throw(_com_error){
  1557. if (V_VT(this) != VT_UI2) {
  1558. // Clear the VARIANT and create a VT_UI2
  1559. //
  1560. Clear();
  1561. V_VT(this) = VT_UI2;
  1562. }
  1563. V_UI2(this) = usSrc;
  1564. return *this;
  1565. }
  1566. // Assign a char creating a VT_UI4 VARIANT
  1567. //
  1568. inline _variant_t& _variant_t::operator=(unsigned long ulSrc) throw(_com_error){
  1569. if (V_VT(this) != VT_UI4) {
  1570. // Clear the VARIANT and create a VT_UI4
  1571. //
  1572. Clear();
  1573. V_VT(this) = VT_UI4;
  1574. }
  1575. V_UI4(this) = ulSrc;
  1576. return *this;
  1577. }
  1578. // Assign a char creating a VT_INT VARIANT
  1579. //
  1580. inline _variant_t& _variant_t::operator=(int iSrc) throw(_com_error){
  1581. if (V_VT(this) != VT_INT) {
  1582. // Clear the VARIANT and create a VT_INT
  1583. //
  1584. Clear();
  1585. V_VT(this) = VT_INT;
  1586. }
  1587. V_INT(this) = iSrc;
  1588. return *this;
  1589. }
  1590. // Assign a char creating a VT_UINT VARIANT
  1591. //
  1592. inline _variant_t& _variant_t::operator=(unsigned int uiSrc) throw(_com_error){
  1593. if (V_VT(this) != VT_UINT) {
  1594. // Clear the VARIANT and create a VT_UINT
  1595. //
  1596. Clear();
  1597. V_VT(this) = VT_UINT;
  1598. }
  1599. V_UINT(this) = uiSrc;
  1600. return *this;
  1601. }
  1602. // Assign a char creating a VT_I8 VARIANT
  1603. //
  1604. inline _variant_t& _variant_t::operator=(__int64 i8Src) throw(_com_error){
  1605. if (V_VT(this) != VT_I8) {
  1606. // Clear the VARIANT and create a VT_I8
  1607. //
  1608. Clear();
  1609. V_VT(this) = VT_I8;
  1610. }
  1611. V_I8(this) = i8Src;
  1612. return *this;
  1613. }
  1614. // Assign a char creating a VT_UI8 VARIANT
  1615. //
  1616. inline _variant_t& _variant_t::operator=(unsigned __int64 ui8Src) throw(_com_error){
  1617. if (V_VT(this) != VT_UI8) {
  1618. // Clear the VARIANT and create a VT_UI8
  1619. //
  1620. Clear();
  1621. V_VT(this) = VT_UI8;
  1622. }
  1623. V_UI8(this) = ui8Src;
  1624. return *this;
  1625. }
  1626. //////////////////////////////////////////////////////////////////////////////////////////
  1627. //
  1628. // Comparison operations
  1629. //
  1630. //////////////////////////////////////////////////////////////////////////////////////////
  1631. // Compare a _variant_t against a const VARIANT& for equality
  1632. //
  1633. inline bool _variant_t::operator==(const VARIANT& varSrc) const throw()
  1634. {
  1635. return *this == &varSrc;
  1636. }
  1637. // Compare a _variant_t against a const VARIANT* for equality
  1638. //
  1639. inline bool _variant_t::operator==(const VARIANT* pSrc) const throw()
  1640. {
  1641. if (this == pSrc) {
  1642. return true;
  1643. }
  1644. //
  1645. // Variants not equal if types don't match
  1646. //
  1647. if (V_VT(this) != V_VT(pSrc)) {
  1648. return false;
  1649. }
  1650. //
  1651. // Check type specific values
  1652. //
  1653. switch (V_VT(this)) {
  1654. case VT_EMPTY:
  1655. case VT_NULL:
  1656. return true;
  1657. case VT_I2:
  1658. return V_I2(this) == V_I2(pSrc);
  1659. case VT_I4:
  1660. return V_I4(this) == V_I4(pSrc);
  1661. case VT_R4:
  1662. return V_R4(this) == V_R4(pSrc);
  1663. case VT_R8:
  1664. return V_R8(this) == V_R8(pSrc);
  1665. case VT_CY:
  1666. return memcmp(&(V_CY(this)), &(V_CY(pSrc)), sizeof(CY)) == 0;
  1667. case VT_DATE:
  1668. return V_DATE(this) == V_DATE(pSrc);
  1669. case VT_BSTR:
  1670. return (::SysStringByteLen(V_BSTR(this)) == ::SysStringByteLen(V_BSTR(pSrc))) &&
  1671. (memcmp(V_BSTR(this), V_BSTR(pSrc), ::SysStringByteLen(V_BSTR(this))) == 0);
  1672. case VT_DISPATCH:
  1673. return V_DISPATCH(this) == V_DISPATCH(pSrc);
  1674. case VT_ERROR:
  1675. return V_ERROR(this) == V_ERROR(pSrc);
  1676. case VT_BOOL:
  1677. return V_BOOL(this) == V_BOOL(pSrc);
  1678. case VT_UNKNOWN:
  1679. return V_UNKNOWN(this) == V_UNKNOWN(pSrc);
  1680. case VT_DECIMAL:
  1681. return memcmp(&(V_DECIMAL(this)), &(V_DECIMAL(pSrc)), sizeof(DECIMAL)) == 0;
  1682. case VT_UI1:
  1683. return V_UI1(this) == V_UI1(pSrc);
  1684. case VT_I1:
  1685. return V_I1(this) == V_I1(pSrc);
  1686. case VT_UI2:
  1687. return V_UI2(this) == V_UI2(pSrc);
  1688. case VT_UI4:
  1689. return V_UI4(this) == V_UI4(pSrc);
  1690. case VT_INT:
  1691. return V_INT(this) == V_INT(pSrc);
  1692. case VT_UINT:
  1693. return V_UINT(this) == V_UINT(pSrc);
  1694. case VT_I8:
  1695. return V_I8(this) == V_I8(pSrc);
  1696. case VT_UI8:
  1697. return V_UI8(this) == V_UI8(pSrc);
  1698. default:
  1699. _com_issue_error(E_INVALIDARG);
  1700. // fall through
  1701. }
  1702. return false;
  1703. }
  1704. // Compare a _variant_t against a const VARIANT& for in-equality
  1705. //
  1706. inline bool _variant_t::operator!=(const VARIANT& varSrc) const throw()
  1707. {
  1708. return !(*this == &varSrc);
  1709. }
  1710. // Compare a _variant_t against a const VARIANT* for in-equality
  1711. //
  1712. inline bool _variant_t::operator!=(const VARIANT* pSrc) const throw()
  1713. {
  1714. return !(*this == pSrc);
  1715. }
  1716. //////////////////////////////////////////////////////////////////////////////////////////
  1717. //
  1718. // Low-level operations
  1719. //
  1720. //////////////////////////////////////////////////////////////////////////////////////////
  1721. // Clear the _variant_t
  1722. //
  1723. inline void _variant_t::Clear() throw(_com_error)
  1724. {
  1725. _com_util::CheckError(::VariantClear(this));
  1726. }
  1727. inline void _variant_t::Attach(VARIANT& varSrc) throw(_com_error)
  1728. {
  1729. //
  1730. // Free up previous VARIANT
  1731. //
  1732. Clear();
  1733. //
  1734. // Give control of data to _variant_t
  1735. //
  1736. memcpy(this, &varSrc, sizeof(varSrc));
  1737. V_VT(&varSrc) = VT_EMPTY;
  1738. }
  1739. inline VARIANT _variant_t::Detach() throw(_com_error)
  1740. {
  1741. VARIANT varResult = *this;
  1742. V_VT(this) = VT_EMPTY;
  1743. return varResult;
  1744. }
  1745. inline VARIANT& _variant_t::GetVARIANT() throw()
  1746. {
  1747. return *(VARIANT*) this;
  1748. }
  1749. inline VARIANT* _variant_t::GetAddress() throw(_com_error)
  1750. {
  1751. Clear();
  1752. return (VARIANT*) this;
  1753. }
  1754. // Change the type and contents of this _variant_t to the type vartype and
  1755. // contents of pSrc
  1756. //
  1757. inline void _variant_t::ChangeType(VARTYPE vartype, const _variant_t* pSrc) throw(_com_error)
  1758. {
  1759. //
  1760. // If pDest is NULL, convert type in place
  1761. //
  1762. if (pSrc == NULL) {
  1763. pSrc = this;
  1764. }
  1765. if ((this != pSrc) || (vartype != V_VT(this))) {
  1766. _com_util::CheckError(::VariantChangeType(static_cast<VARIANT*>(this),
  1767. const_cast<VARIANT*>(static_cast<const VARIANT*>(pSrc)),
  1768. 0, vartype));
  1769. }
  1770. }
  1771. inline void _variant_t::SetString(const char* pSrc) throw(_com_error)
  1772. {
  1773. //
  1774. // Free up previous VARIANT
  1775. //
  1776. Clear();
  1777. V_VT(this) = VT_BSTR;
  1778. V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
  1779. }
  1780. //////////////////////////////////////////////////////////////////////////////////////////
  1781. //
  1782. // Destructor
  1783. //
  1784. //////////////////////////////////////////////////////////////////////////////////////////
  1785. inline _variant_t::~_variant_t() throw(_com_error)
  1786. {
  1787. _com_util::CheckError(::VariantClear(this));
  1788. }
  1789. //////////////////////////////////////////////////////////////////////////////////////////
  1790. //
  1791. // Mutually-dependent definitions
  1792. //
  1793. //////////////////////////////////////////////////////////////////////////////////////////
  1794. // Construct a _bstr_t from a const _variant_t&
  1795. //
  1796. inline _bstr_t::_bstr_t(const _variant_t &var) throw(_com_error)
  1797. : m_Data(NULL)
  1798. {
  1799. if (V_VT(&var) == VT_BSTR) {
  1800. *this = V_BSTR(&var);
  1801. return;
  1802. }
  1803. _variant_t varDest;
  1804. varDest.ChangeType(VT_BSTR, &var);
  1805. *this = V_BSTR(&varDest);
  1806. }
  1807. // Assign a const _variant_t& to a _bstr_t
  1808. //
  1809. inline _bstr_t& _bstr_t::operator=(const _variant_t &var) throw(_com_error)
  1810. {
  1811. if (V_VT(&var) == VT_BSTR) {
  1812. *this = V_BSTR(&var);
  1813. return *this;
  1814. }
  1815. _variant_t varDest;
  1816. varDest.ChangeType(VT_BSTR, &var);
  1817. *this = V_BSTR(&varDest);
  1818. return *this;
  1819. }
  1820. extern _variant_t vtMissing;
  1821. #ifndef _USE_RAW
  1822. #define bstr_t _bstr_t
  1823. #define variant_t _variant_t
  1824. #endif
  1825. #pragma pop_macro("new")
  1826. #pragma warning(pop)
  1827. #endif // _INC_COMUTIL