Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1895 lines
43 KiB

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