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.

1027 lines
25 KiB

  1. //
  2. // comutil.h - Native C++ compiler COM support - BSTR, VARIANT wrappers header
  3. //
  4. #pragma once
  5. //////////////////////////////////////////////////////////////////////////////
  6. class _bstr_t;
  7. class auto_var;
  8. //////////////////////////////////////////////////////////////////////////////
  9. //
  10. // Wrapper class for VARIANT
  11. //
  12. //////////////////////////////////////////////////////////////////////////////
  13. /*
  14. * VARENUM usage key,
  15. *
  16. * * [V] - may appear in a VARIANT
  17. * * [T] - may appear in a TYPEDESC
  18. * * [P] - may appear in an OLE property set
  19. * * [S] - may appear in a Safe Array
  20. * * [C] - supported by class auto_var
  21. *
  22. *
  23. * VT_EMPTY [V] [P] nothing
  24. * VT_NULL [V] [P] SQL style Null
  25. * VT_I2 [V][T][P][S][C] 2 byte signed int
  26. * VT_I4 [V][T][P][S][C] 4 byte signed int
  27. * VT_R4 [V][T][P][S][C] 4 byte real
  28. * VT_R8 [V][T][P][S][C] 8 byte real
  29. * VT_CY [V][T][P][S][C] currency
  30. * VT_DATE [V][T][P][S][C] date
  31. * VT_BSTR [V][T][P][S][C] OLE Automation string
  32. * VT_DISPATCH [V][T][P][S][C] IDispatch *
  33. * VT_ERROR [V][T] [S][C] SCODE
  34. * VT_BOOL [V][T][P][S][C] True=-1, False=0
  35. * VT_VARIANT [V][T][P][S] VARIANT *
  36. * VT_UNKNOWN [V][T] [S][C] IUnknown *
  37. * VT_DECIMAL [V][T] [S][C] 16 byte fixed point
  38. * VT_I1 [T] signed char
  39. * VT_UI1 [V][T][P][S][C] unsigned char
  40. * VT_UI2 [T][P] unsigned short
  41. * VT_UI4 [T][P] unsigned short
  42. * VT_I8 [T][P] signed 64-bit int
  43. * VT_UI8 [T][P] unsigned 64-bit int
  44. * VT_INT [T] signed machine int
  45. * VT_UINT [T] unsigned machine int
  46. * VT_VOID [T] C style void
  47. * VT_HRESULT [T] Standard return type
  48. * VT_PTR [T] pointer type
  49. * VT_SAFEARRAY [T] (use VT_ARRAY in VARIANT)
  50. * VT_CARRAY [T] C style array
  51. * VT_USERDEFINED [T] user defined type
  52. * VT_LPSTR [T][P] null terminated string
  53. * VT_LPWSTR [T][P] wide null terminated string
  54. * VT_FILETIME [P] FILETIME
  55. * VT_BLOB [P] Length prefixed bytes
  56. * VT_STREAM [P] Name of the stream follows
  57. * VT_STORAGE [P] Name of the storage follows
  58. * VT_STREAMED_OBJECT [P] Stream contains an object
  59. * VT_STORED_OBJECT [P] Storage contains an object
  60. * VT_BLOB_OBJECT [P] Blob contains an object
  61. * VT_CF [P] Clipboard format
  62. * VT_CLSID [P] A Class ID
  63. * VT_VECTOR [P] simple counted array
  64. * VT_ARRAY [V] SAFEARRAY*
  65. * VT_BYREF [V] void* for local use
  66. */
  67. class auto_var : public ::tagVARIANT {
  68. public:
  69. // Constructors
  70. //
  71. auto_var() throw();
  72. auto_var(const VARIANT& varSrc) throw(_com_error);
  73. auto_var(const VARIANT* pSrc) throw(_com_error);
  74. auto_var(const auto_var& varSrc) throw(_com_error);
  75. auto_var(VARIANT& varSrc, bool fCopy) throw(_com_error); // Attach VARIANT if !fCopy
  76. auto_var(short sSrc, VARTYPE vtSrc = VT_I2) throw(_com_error); // Creates a VT_I2, or a VT_BOOL
  77. auto_var(long lSrc, VARTYPE vtSrc = VT_I4) throw(_com_error); // Creates a VT_I4, a VT_ERROR, or a VT_BOOL
  78. auto_var(float fltSrc) throw(); // Creates a VT_R4
  79. auto_var(double dblSrc, VARTYPE vtSrc = VT_R8) throw(_com_error); // Creates a VT_R8, or a VT_DATE
  80. auto_var(const CY& cySrc) throw(); // Creates a VT_CY
  81. auto_var(const _bstr_t& bstrSrc) throw(_com_error); // Creates a VT_BSTR
  82. auto_var(const wchar_t *pSrc) throw(_com_error); // Creates a VT_BSTR
  83. auto_var(const char* pSrc) throw(_com_error); // Creates a VT_BSTR
  84. auto_var(IDispatch* pSrc, bool fAddRef = true) throw(); // Creates a VT_DISPATCH
  85. auto_var(bool bSrc) throw(); // Creates a VT_BOOL
  86. auto_var(IUnknown* pSrc, bool fAddRef = true) throw(); // Creates a VT_UNKNOWN
  87. auto_var(const DECIMAL& decSrc) throw(); // Creates a VT_DECIMAL
  88. auto_var(BYTE bSrc) throw(); // Creates a VT_UI1
  89. // Destructor
  90. //
  91. ~auto_var() throw(_com_error);
  92. // Extractors
  93. //
  94. operator short() const throw(_com_error); // Extracts a short from a VT_I2
  95. operator long() const throw(_com_error); // Extracts a long from a VT_I4
  96. operator float() const throw(_com_error); // Extracts a float from a VT_R4
  97. operator double() const throw(_com_error); // Extracts a double from a VT_R8
  98. operator CY() const throw(_com_error); // Extracts a CY from a VT_CY
  99. operator _bstr_t() const throw(_com_error); // Extracts a _bstr_t from a VT_BSTR
  100. operator IDispatch*() const throw(_com_error); // Extracts a IDispatch* from a VT_DISPATCH
  101. operator bool() const throw(_com_error); // Extracts a bool from a VT_BOOL
  102. operator IUnknown*() const throw(_com_error); // Extracts a IUnknown* from a VT_UNKNOWN
  103. operator DECIMAL() const throw(_com_error); // Extracts a DECIMAL from a VT_DECIMAL
  104. operator BYTE() const throw(_com_error); // Extracts a BTYE (unsigned char) from a VT_UI1
  105. // Assignment operations
  106. //
  107. auto_var& operator=(const VARIANT& varSrc) throw(_com_error);
  108. auto_var& operator=(const VARIANT* pSrc) throw(_com_error);
  109. auto_var& operator=(const auto_var& varSrc) throw(_com_error);
  110. auto_var& operator=(short sSrc) throw(_com_error); // Assign a VT_I2, or a VT_BOOL
  111. auto_var& operator=(long lSrc) throw(_com_error); // Assign a VT_I4, a VT_ERROR or a VT_BOOL
  112. auto_var& operator=(float fltSrc) throw(_com_error); // Assign a VT_R4
  113. auto_var& operator=(double dblSrc) throw(_com_error); // Assign a VT_R8, or a VT_DATE
  114. auto_var& operator=(const CY& cySrc) throw(_com_error); // Assign a VT_CY
  115. auto_var& operator=(const _bstr_t& bstrSrc) throw(_com_error); // Assign a VT_BSTR
  116. auto_var& operator=(const wchar_t* pSrc) throw(_com_error); // Assign a VT_BSTR
  117. auto_var& operator=(const char* pSrc) throw(_com_error); // Assign a VT_BSTR
  118. auto_var& operator=(IDispatch* pSrc) throw(_com_error); // Assign a VT_DISPATCH
  119. auto_var& operator=(bool bSrc) throw(_com_error); // Assign a VT_BOOL
  120. auto_var& operator=(IUnknown* pSrc) throw(_com_error); // Assign a VT_UNKNOWN
  121. auto_var& operator=(const DECIMAL& decSrc) throw(_com_error); // Assign a VT_DECIMAL
  122. auto_var& operator=(BYTE bSrc) throw(_com_error); // Assign a VT_UI1
  123. // Comparison operations
  124. //
  125. bool operator==(const VARIANT& varSrc) const throw(_com_error);
  126. bool operator==(const VARIANT* pSrc) const throw(_com_error);
  127. bool operator!=(const VARIANT& varSrc) const throw(_com_error);
  128. bool operator!=(const VARIANT* pSrc) const throw(_com_error);
  129. // Low-level operations
  130. //
  131. void Clear() throw(_com_error);
  132. void Attach(VARIANT& varSrc) throw(_com_error);
  133. VARIANT Detach() throw(_com_error);
  134. void ChangeType(VARTYPE vartype, const auto_var* pSrc = NULL) throw(_com_error);
  135. void SetString(const char* pSrc) throw(_com_error); // used to set ANSI string
  136. };
  137. //////////////////////////////////////////////////////////////////////////////////////////
  138. //
  139. // Constructors
  140. //
  141. //////////////////////////////////////////////////////////////////////////////////////////
  142. // Default constructor
  143. //
  144. inline auto_var::auto_var() throw()
  145. {
  146. ::VariantInit(this);
  147. }
  148. // Construct a auto_var from a const VARIANT&
  149. //
  150. inline auto_var::auto_var(const VARIANT& varSrc) throw(_com_error)
  151. {
  152. ::VariantInit(this);
  153. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
  154. }
  155. // Construct a auto_var from a const VARIANT*
  156. //
  157. inline auto_var::auto_var(const VARIANT* pSrc) throw(_com_error)
  158. {
  159. ::VariantInit(this);
  160. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
  161. }
  162. // Construct a auto_var from a const auto_var&
  163. //
  164. inline auto_var::auto_var(const auto_var& varSrc) throw(_com_error)
  165. {
  166. ::VariantInit(this);
  167. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
  168. }
  169. // Construct a auto_var from a VARIANT&. If fCopy is FALSE, give control of
  170. // data to the auto_var without doing a VariantCopy.
  171. //
  172. inline auto_var::auto_var(VARIANT& varSrc, bool fCopy) throw(_com_error)
  173. {
  174. if (fCopy) {
  175. ::VariantInit(this);
  176. _com_util::CheckError(::VariantCopy(this, &varSrc));
  177. } else {
  178. memcpy(this, &varSrc, sizeof(varSrc));
  179. V_VT(&varSrc) = VT_EMPTY;
  180. }
  181. }
  182. // Construct either a VT_I2 VARIANT or a VT_BOOL VARIANT from
  183. // a short (the default is VT_I2)
  184. //
  185. inline auto_var::auto_var(short sSrc, VARTYPE vtSrc) throw(_com_error)
  186. {
  187. if ((vtSrc != VT_I2) && (vtSrc != VT_BOOL)) {
  188. _com_issue_error(E_INVALIDARG);
  189. }
  190. if (vtSrc == VT_BOOL) {
  191. V_VT(this) = VT_BOOL;
  192. V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
  193. }
  194. else {
  195. V_VT(this) = VT_I2;
  196. V_I2(this) = sSrc;
  197. }
  198. }
  199. // Construct either a VT_I4 VARIANT, a VT_BOOL VARIANT, or a
  200. // VT_ERROR VARIANT from a long (the default is VT_I4)
  201. //
  202. inline auto_var::auto_var(long lSrc, VARTYPE vtSrc) throw(_com_error)
  203. {
  204. if ((vtSrc != VT_I4) && (vtSrc != VT_ERROR) && (vtSrc != VT_BOOL)) {
  205. _com_issue_error(E_INVALIDARG);
  206. }
  207. if (vtSrc == VT_ERROR) {
  208. V_VT(this) = VT_ERROR;
  209. V_ERROR(this) = lSrc;
  210. }
  211. else if (vtSrc == VT_BOOL) {
  212. V_VT(this) = VT_BOOL;
  213. V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
  214. }
  215. else {
  216. V_VT(this) = VT_I4;
  217. V_I4(this) = lSrc;
  218. }
  219. }
  220. // Construct a VT_R4 VARIANT from a float
  221. //
  222. inline auto_var::auto_var(float fltSrc) throw()
  223. {
  224. V_VT(this) = VT_R4;
  225. V_R4(this) = fltSrc;
  226. }
  227. // Construct either a VT_R8 VARIANT, or a VT_DATE VARIANT from
  228. // a double (the default is VT_R8)
  229. //
  230. inline auto_var::auto_var(double dblSrc, VARTYPE vtSrc) throw(_com_error)
  231. {
  232. if ((vtSrc != VT_R8) && (vtSrc != VT_DATE)) {
  233. _com_issue_error(E_INVALIDARG);
  234. }
  235. if (vtSrc == VT_DATE) {
  236. V_VT(this) = VT_DATE;
  237. V_DATE(this) = dblSrc;
  238. }
  239. else {
  240. V_VT(this) = VT_R8;
  241. V_R8(this) = dblSrc;
  242. }
  243. }
  244. // Construct a VT_CY from a CY
  245. //
  246. inline auto_var::auto_var(const CY& cySrc) throw()
  247. {
  248. V_VT(this) = VT_CY;
  249. V_CY(this) = cySrc;
  250. }
  251. // Construct a VT_BSTR VARIANT from a const _bstr_t&
  252. //
  253. inline auto_var::auto_var(const _bstr_t& bstrSrc) throw(_com_error)
  254. {
  255. V_VT(this) = VT_BSTR;
  256. BSTR bstr = static_cast<wchar_t*>(bstrSrc);
  257. V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
  258. ::SysStringByteLen(bstr));
  259. if (V_BSTR(this) == NULL) {
  260. _com_issue_error(E_OUTOFMEMORY);
  261. }
  262. }
  263. // Construct a VT_BSTR VARIANT from a const wchar_t*
  264. //
  265. inline auto_var::auto_var(const wchar_t* pSrc) throw(_com_error)
  266. {
  267. V_VT(this) = VT_BSTR;
  268. V_BSTR(this) = ::SysAllocString(pSrc);
  269. if (V_BSTR(this) == NULL && pSrc != NULL) {
  270. _com_issue_error(E_OUTOFMEMORY);
  271. }
  272. }
  273. // Construct a VT_BSTR VARIANT from a const char*
  274. //
  275. inline auto_var::auto_var(const char* pSrc) throw(_com_error)
  276. {
  277. V_VT(this) = VT_BSTR;
  278. V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
  279. if (V_BSTR(this) == NULL && pSrc != NULL) {
  280. _com_issue_error(E_OUTOFMEMORY);
  281. }
  282. }
  283. // Construct a VT_DISPATCH VARIANT from an IDispatch*
  284. //
  285. inline auto_var::auto_var(IDispatch* pSrc, bool fAddRef) throw()
  286. {
  287. V_VT(this) = VT_DISPATCH;
  288. V_DISPATCH(this) = pSrc;
  289. // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
  290. // false indicates we're taking ownership
  291. //
  292. if (fAddRef) {
  293. V_DISPATCH(this)->AddRef();
  294. }
  295. }
  296. // Construct a VT_BOOL VARIANT from a bool
  297. //
  298. inline auto_var::auto_var(bool bSrc) throw()
  299. {
  300. V_VT(this) = VT_BOOL;
  301. V_BOOL(this) = (bSrc ? VARIANT_TRUE : VARIANT_FALSE);
  302. }
  303. // Construct a VT_UNKNOWN VARIANT from an IUnknown*
  304. //
  305. inline auto_var::auto_var(IUnknown* pSrc, bool fAddRef) throw()
  306. {
  307. V_VT(this) = VT_UNKNOWN;
  308. V_UNKNOWN(this) = pSrc;
  309. // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
  310. // false indicates we're taking ownership
  311. //
  312. if (fAddRef) {
  313. V_UNKNOWN(this)->AddRef();
  314. }
  315. }
  316. // Construct a VT_DECIMAL VARIANT from a DECIMAL
  317. //
  318. inline auto_var::auto_var(const DECIMAL& decSrc) throw()
  319. {
  320. // Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
  321. //
  322. V_DECIMAL(this) = decSrc;
  323. V_VT(this) = VT_DECIMAL;
  324. }
  325. // Construct a VT_UI1 VARIANT from a BYTE (unsigned char)
  326. //
  327. inline auto_var::auto_var(BYTE bSrc) throw()
  328. {
  329. V_VT(this) = VT_UI1;
  330. V_UI1(this) = bSrc;
  331. }
  332. //////////////////////////////////////////////////////////////////////////////////////////
  333. //
  334. // Extractors
  335. //
  336. //////////////////////////////////////////////////////////////////////////////////////////
  337. // Extracts a VT_I2 into a short
  338. //
  339. inline auto_var::operator short() const throw(_com_error)
  340. {
  341. if (V_VT(this) == VT_I2) {
  342. return V_I2(this);
  343. }
  344. auto_var varDest;
  345. varDest.ChangeType(VT_I2, this);
  346. return V_I2(&varDest);
  347. }
  348. // Extracts a VT_I4 into a long
  349. //
  350. inline auto_var::operator long() const throw(_com_error)
  351. {
  352. if (V_VT(this) == VT_I4) {
  353. return V_I4(this);
  354. }
  355. auto_var varDest;
  356. varDest.ChangeType(VT_I4, this);
  357. return V_I4(&varDest);
  358. }
  359. // Extracts a VT_R4 into a float
  360. //
  361. inline auto_var::operator float() const throw(_com_error)
  362. {
  363. if (V_VT(this) == VT_R4) {
  364. return V_R4(this);
  365. }
  366. auto_var varDest;
  367. varDest.ChangeType(VT_R4, this);
  368. return V_R4(&varDest);
  369. }
  370. // Extracts a VT_R8 into a double
  371. //
  372. inline auto_var::operator double() const throw(_com_error)
  373. {
  374. if (V_VT(this) == VT_R8) {
  375. return V_R8(this);
  376. }
  377. auto_var varDest;
  378. varDest.ChangeType(VT_R8, this);
  379. return V_R8(&varDest);
  380. }
  381. // Extracts a VT_CY into a CY
  382. //
  383. inline auto_var::operator CY() const throw(_com_error)
  384. {
  385. if (V_VT(this) == VT_CY) {
  386. return V_CY(this);
  387. }
  388. auto_var varDest;
  389. varDest.ChangeType(VT_CY, this);
  390. return V_CY(&varDest);
  391. }
  392. // Extracts a VT_BSTR into a _bstr_t
  393. //
  394. inline auto_var::operator _bstr_t() const throw(_com_error)
  395. {
  396. if (V_VT(this) == VT_BSTR) {
  397. return V_BSTR(this);
  398. }
  399. auto_var varDest;
  400. varDest.ChangeType(VT_BSTR, this);
  401. return V_BSTR(&varDest);
  402. }
  403. // Extracts a VT_DISPATCH into an IDispatch*
  404. //
  405. inline auto_var::operator IDispatch*() const throw(_com_error)
  406. {
  407. if (V_VT(this) == VT_DISPATCH) {
  408. V_DISPATCH(this)->AddRef();
  409. return V_DISPATCH(this);
  410. }
  411. auto_var varDest;
  412. varDest.ChangeType(VT_DISPATCH, this);
  413. V_DISPATCH(&varDest)->AddRef();
  414. return V_DISPATCH(&varDest);
  415. }
  416. // Extract a VT_BOOL into a bool
  417. //
  418. inline auto_var::operator bool() const throw(_com_error)
  419. {
  420. if (V_VT(this) == VT_BOOL) {
  421. return V_BOOL(this) ? true : false;
  422. }
  423. auto_var varDest;
  424. varDest.ChangeType(VT_BOOL, this);
  425. return V_BOOL(&varDest) ? true : false;
  426. }
  427. // Extracts a VT_UNKNOWN into an IUnknown*
  428. //
  429. inline auto_var::operator IUnknown*() const throw(_com_error)
  430. {
  431. if (V_VT(this) == VT_UNKNOWN) {
  432. return V_UNKNOWN(this);
  433. }
  434. auto_var varDest;
  435. varDest.ChangeType(VT_UNKNOWN, this);
  436. return V_UNKNOWN(&varDest);
  437. }
  438. // Extracts a VT_DECIMAL into a DECIMAL
  439. //
  440. inline auto_var::operator DECIMAL() const throw(_com_error)
  441. {
  442. if (V_VT(this) == VT_DECIMAL) {
  443. return V_DECIMAL(this);
  444. }
  445. auto_var varDest;
  446. varDest.ChangeType(VT_DECIMAL, this);
  447. return V_DECIMAL(&varDest);
  448. }
  449. // Extracts a VT_UI1 into a BYTE (unsigned char)
  450. //
  451. inline auto_var::operator BYTE() const throw(_com_error)
  452. {
  453. if (V_VT(this) == VT_UI1) {
  454. return V_UI1(this);
  455. }
  456. auto_var varDest;
  457. varDest.ChangeType(VT_UI1, this);
  458. return V_UI1(&varDest);
  459. }
  460. //////////////////////////////////////////////////////////////////////////////////////////
  461. //
  462. // Assignment operations
  463. //
  464. //////////////////////////////////////////////////////////////////////////////////////////
  465. // Assign a const VARIANT& (::VariantCopy handles everything)
  466. //
  467. inline auto_var& auto_var::operator=(const VARIANT& varSrc) throw(_com_error)
  468. {
  469. Clear();
  470. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
  471. return *this;
  472. }
  473. // Assign a const VARIANT* (::VariantCopy handles everything)
  474. //
  475. inline auto_var& auto_var::operator=(const VARIANT* pSrc) throw(_com_error)
  476. {
  477. Clear();
  478. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
  479. return *this;
  480. }
  481. // Assign a const auto_var& (::VariantCopy handles everything)
  482. //
  483. inline auto_var& auto_var::operator=(const auto_var& varSrc) throw(_com_error)
  484. {
  485. Clear();
  486. _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
  487. return *this;
  488. }
  489. // Assign a short creating either VT_I2 VARIANT or a
  490. // VT_BOOL VARIANT (VT_I2 is the default)
  491. //
  492. inline auto_var& auto_var::operator=(short sSrc) throw(_com_error)
  493. {
  494. if (V_VT(this) == VT_I2) {
  495. V_I2(this) = sSrc;
  496. }
  497. else if (V_VT(this) == VT_BOOL) {
  498. V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
  499. }
  500. else {
  501. // Clear the VARIANT and create a VT_I2
  502. //
  503. Clear();
  504. V_VT(this) = VT_I2;
  505. V_I2(this) = sSrc;
  506. }
  507. return *this;
  508. }
  509. // Assign a long creating either VT_I4 VARIANT, a VT_ERROR VARIANT
  510. // or a VT_BOOL VARIANT (VT_I4 is the default)
  511. //
  512. inline auto_var& auto_var::operator=(long lSrc) throw(_com_error)
  513. {
  514. if (V_VT(this) == VT_I4) {
  515. V_I4(this) = lSrc;
  516. }
  517. else if (V_VT(this) == VT_ERROR) {
  518. V_ERROR(this) = lSrc;
  519. }
  520. else if (V_VT(this) == VT_BOOL) {
  521. V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
  522. }
  523. else {
  524. // Clear the VARIANT and create a VT_I4
  525. //
  526. Clear();
  527. V_VT(this) = VT_I4;
  528. V_I4(this) = lSrc;
  529. }
  530. return *this;
  531. }
  532. // Assign a float creating a VT_R4 VARIANT
  533. //
  534. inline auto_var& auto_var::operator=(float fltSrc) throw(_com_error)
  535. {
  536. if (V_VT(this) != VT_R4) {
  537. // Clear the VARIANT and create a VT_R4
  538. //
  539. Clear();
  540. V_VT(this) = VT_R4;
  541. }
  542. V_R4(this) = fltSrc;
  543. return *this;
  544. }
  545. // Assign a double creating either a VT_R8 VARIANT, or a VT_DATE
  546. // VARIANT (VT_R8 is the default)
  547. //
  548. inline auto_var& auto_var::operator=(double dblSrc) throw(_com_error)
  549. {
  550. if (V_VT(this) == VT_R8) {
  551. V_R8(this) = dblSrc;
  552. }
  553. else if(V_VT(this) == VT_DATE) {
  554. V_DATE(this) == dblSrc;
  555. }
  556. else {
  557. // Clear the VARIANT and create a VT_R8
  558. //
  559. Clear();
  560. V_VT(this) = VT_R8;
  561. V_R8(this) = dblSrc;
  562. }
  563. return *this;
  564. }
  565. // Assign a CY creating a VT_CY VARIANT
  566. //
  567. inline auto_var& auto_var::operator=(const CY& cySrc) throw(_com_error)
  568. {
  569. if (V_VT(this) != VT_CY) {
  570. // Clear the VARIANT and create a VT_CY
  571. //
  572. Clear();
  573. V_VT(this) = VT_CY;
  574. }
  575. V_CY(this) = cySrc;
  576. return *this;
  577. }
  578. // Assign a const _bstr_t& creating a VT_BSTR VARIANT
  579. //
  580. inline auto_var& auto_var::operator=(const _bstr_t& bstrSrc) throw(_com_error)
  581. {
  582. // Clear the VARIANT (This will SysFreeString() any previous occupant)
  583. //
  584. Clear();
  585. V_VT(this) = VT_BSTR;
  586. if (!bstrSrc) {
  587. V_BSTR(this) = NULL;
  588. }
  589. else {
  590. BSTR bstr = static_cast<wchar_t*>(bstrSrc);
  591. V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
  592. ::SysStringByteLen(bstr));
  593. if (V_BSTR(this) == NULL) {
  594. _com_issue_error(E_OUTOFMEMORY);
  595. }
  596. }
  597. return *this;
  598. }
  599. // Assign a const wchar_t* creating a VT_BSTR VARIANT
  600. //
  601. inline auto_var& auto_var::operator=(const wchar_t* pSrc) throw(_com_error)
  602. {
  603. // Clear the VARIANT (This will SysFreeString() any previous occupant)
  604. //
  605. Clear();
  606. V_VT(this) = VT_BSTR;
  607. if (pSrc == NULL) {
  608. V_BSTR(this) = NULL;
  609. }
  610. else {
  611. V_BSTR(this) = ::SysAllocString(pSrc);
  612. if (V_BSTR(this) == NULL) {
  613. _com_issue_error(E_OUTOFMEMORY);
  614. }
  615. }
  616. return *this;
  617. }
  618. // Assign a const char* creating a VT_BSTR VARIANT
  619. //
  620. inline auto_var& auto_var::operator=(const char* pSrc) throw(_com_error)
  621. {
  622. // Clear the VARIANT (This will SysFreeString() any previous occupant)
  623. //
  624. Clear();
  625. V_VT(this) = VT_BSTR;
  626. V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
  627. if (V_BSTR(this) == NULL && pSrc != NULL) {
  628. _com_issue_error(E_OUTOFMEMORY);
  629. }
  630. return *this;
  631. }
  632. // Assign an IDispatch* creating a VT_DISPATCH VARIANT
  633. //
  634. inline auto_var& auto_var::operator=(IDispatch* pSrc) throw(_com_error)
  635. {
  636. // Clear the VARIANT (This will Release() any previous occupant)
  637. //
  638. Clear();
  639. V_VT(this) = VT_DISPATCH;
  640. V_DISPATCH(this) = pSrc;
  641. // Need the AddRef() as VariantClear() calls Release()
  642. //
  643. V_DISPATCH(this)->AddRef();
  644. return *this;
  645. }
  646. // Assign a bool creating a VT_BOOL VARIANT
  647. //
  648. inline auto_var& auto_var::operator=(bool bSrc) throw(_com_error)
  649. {
  650. if (V_VT(this) != VT_BOOL) {
  651. // Clear the VARIANT and create a VT_BOOL
  652. //
  653. Clear();
  654. V_VT(this) = VT_BOOL;
  655. }
  656. V_BOOL(this) = (bSrc ? VARIANT_TRUE : VARIANT_FALSE);
  657. return *this;
  658. }
  659. // Assign an IUnknown* creating a VT_UNKNOWN VARIANT
  660. //
  661. inline auto_var& auto_var::operator=(IUnknown* pSrc) throw(_com_error)
  662. {
  663. // Clear VARIANT (This will Release() any previous occupant)
  664. //
  665. Clear();
  666. V_VT(this) = VT_UNKNOWN;
  667. V_UNKNOWN(this) = pSrc;
  668. // Need the AddRef() as VariantClear() calls Release()
  669. //
  670. V_UNKNOWN(this)->AddRef();
  671. return *this;
  672. }
  673. // Assign a DECIMAL creating a VT_DECIMAL VARIANT
  674. //
  675. inline auto_var& auto_var::operator=(const DECIMAL& decSrc) throw(_com_error)
  676. {
  677. if (V_VT(this) != VT_DECIMAL) {
  678. // Clear the VARIANT
  679. //
  680. Clear();
  681. }
  682. // Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
  683. V_DECIMAL(this) = decSrc;
  684. V_VT(this) = VT_DECIMAL;
  685. return *this;
  686. }
  687. // Assign a BTYE (unsigned char) creating a VT_UI1 VARIANT
  688. //
  689. inline auto_var& auto_var::operator=(BYTE bSrc) throw(_com_error)
  690. {
  691. if (V_VT(this) != VT_UI1) {
  692. // Clear the VARIANT and create a VT_UI1
  693. //
  694. Clear();
  695. V_VT(this) = VT_UI1;
  696. }
  697. V_UI1(this) = bSrc;
  698. return *this;
  699. }
  700. //////////////////////////////////////////////////////////////////////////////////////////
  701. //
  702. // Comparison operations
  703. //
  704. //////////////////////////////////////////////////////////////////////////////////////////
  705. // Compare a auto_var against a const VARIANT& for equality
  706. //
  707. inline bool auto_var::operator==(const VARIANT& varSrc) const throw()
  708. {
  709. return *this == &varSrc;
  710. }
  711. // Compare a auto_var against a const VARIANT* for equality
  712. //
  713. inline bool auto_var::operator==(const VARIANT* pSrc) const throw()
  714. {
  715. if (this == pSrc) {
  716. return true;
  717. }
  718. //
  719. // Variants not equal if types don't match
  720. //
  721. if (V_VT(this) != V_VT(pSrc)) {
  722. return false;
  723. }
  724. //
  725. // Check type specific values
  726. //
  727. switch (V_VT(this)) {
  728. case VT_EMPTY:
  729. case VT_NULL:
  730. return true;
  731. case VT_I2:
  732. return V_I2(this) == V_I2(pSrc);
  733. case VT_I4:
  734. return V_I4(this) == V_I4(pSrc);
  735. case VT_R4:
  736. return V_R4(this) == V_R4(pSrc);
  737. case VT_R8:
  738. return V_R8(this) == V_R8(pSrc);
  739. case VT_CY:
  740. return memcmp(&(V_CY(this)), &(V_CY(pSrc)), sizeof(CY)) == 0;
  741. case VT_DATE:
  742. return V_DATE(this) == V_DATE(pSrc);
  743. case VT_BSTR:
  744. return (::SysStringByteLen(V_BSTR(this)) == ::SysStringByteLen(V_BSTR(pSrc))) &&
  745. (memcmp(V_BSTR(this), V_BSTR(pSrc), ::SysStringByteLen(V_BSTR(this))) == 0);
  746. case VT_DISPATCH:
  747. return V_DISPATCH(this) == V_DISPATCH(pSrc);
  748. case VT_ERROR:
  749. return V_ERROR(this) == V_ERROR(pSrc);
  750. case VT_BOOL:
  751. return V_BOOL(this) == V_BOOL(pSrc);
  752. case VT_UNKNOWN:
  753. return V_UNKNOWN(this) == V_UNKNOWN(pSrc);
  754. case VT_DECIMAL:
  755. return memcmp(&(V_DECIMAL(this)), &(V_DECIMAL(pSrc)), sizeof(DECIMAL)) == 0;
  756. case VT_UI1:
  757. return V_UI1(this) == V_UI1(pSrc);
  758. default:
  759. _com_issue_error(E_INVALIDARG);
  760. // fall through
  761. }
  762. return false;
  763. }
  764. // Compare a auto_var against a const VARIANT& for in-equality
  765. //
  766. inline bool auto_var::operator!=(const VARIANT& varSrc) const throw()
  767. {
  768. return !(*this == &varSrc);
  769. }
  770. // Compare a auto_var against a const VARIANT* for in-equality
  771. //
  772. inline bool auto_var::operator!=(const VARIANT* pSrc) const throw()
  773. {
  774. return !(*this == pSrc);
  775. }
  776. //////////////////////////////////////////////////////////////////////////////////////////
  777. //
  778. // Low-level operations
  779. //
  780. //////////////////////////////////////////////////////////////////////////////////////////
  781. // Clear the auto_var
  782. //
  783. inline void auto_var::Clear() throw(_com_error)
  784. {
  785. _com_util::CheckError(::VariantClear(this));
  786. }
  787. inline void auto_var::Attach(VARIANT& varSrc) throw(_com_error)
  788. {
  789. //
  790. // Free up previous VARIANT
  791. //
  792. Clear();
  793. //
  794. // Give control of data to auto_var
  795. //
  796. memcpy(this, &varSrc, sizeof(varSrc));
  797. V_VT(&varSrc) = VT_EMPTY;
  798. }
  799. inline VARIANT auto_var::Detach() throw(_com_error)
  800. {
  801. VARIANT varResult = *this;
  802. V_VT(this) = VT_EMPTY;
  803. return varResult;
  804. }
  805. // Change the type and contents of this auto_var to the type vartype and
  806. // contents of pSrc
  807. //
  808. inline void auto_var::ChangeType(VARTYPE vartype, const auto_var* pSrc) throw(_com_error)
  809. {
  810. //
  811. // If pDest is NULL, convert type in place
  812. //
  813. if (pSrc == NULL) {
  814. pSrc = this;
  815. }
  816. if ((this != pSrc) || (vartype != V_VT(this))) {
  817. _com_util::CheckError(::VariantChangeType(static_cast<VARIANT*>(this),
  818. const_cast<VARIANT*>(static_cast<const VARIANT*>(pSrc)),
  819. 0, vartype));
  820. }
  821. }
  822. inline void auto_var::SetString(const char* pSrc) throw(_com_error)
  823. {
  824. //
  825. // Free up previous VARIANT
  826. //
  827. Clear();
  828. V_VT(this) = VT_BSTR;
  829. V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
  830. if (V_BSTR(this) == NULL && pSrc != NULL) {
  831. _com_issue_error(E_OUTOFMEMORY);
  832. }
  833. }
  834. //////////////////////////////////////////////////////////////////////////////////////////
  835. //
  836. // Destructor
  837. //
  838. //////////////////////////////////////////////////////////////////////////////////////////
  839. inline auto_var::~auto_var() throw(_com_error)
  840. {
  841. _com_util::CheckError(::VariantClear(this));
  842. }