STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
comutil.h
Go to the documentation of this file.
1 /***
2 * comutil.h - Native C++ compiler COM support - BSTR, VARIANT wrappers header
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 ****/
7 
8 #if _MSC_VER > 1000
9 #pragma once
10 #endif
11 
12 #ifdef _M_CEE_PURE
13 #error comutil.h header cannot be included under /clr:safe or /clr:pure
14 #endif
15 
16 #if !defined(_INC_COMUTIL)
17 #define _INC_COMUTIL
18 
19 #include <ole2.h>
20 
21 #if !defined(_COM_ASSERT)
22 # if defined(_DEBUG)
23 # include <assert.h>
24 # define _COM_ASSERT(x) assert(x)
25 # else
26 # define _COM_ASSERT(x) ((void)0)
27 # endif
28 #endif
29 
30 #if !defined(_SECURE_COMPILER_COM)
31 /* use secure versions by default if not specified otherwise */
32 #define _SECURE_COMPILER_COM 1
33 #endif
34 
35 #if _SECURE_COMPILER_COM && defined(__GOT_SECURE_LIB__) && __GOT_SECURE_LIB__ >= 200402L
36 
37 #include <stdio.h>
38 
39 # define _COM_MEMCPY_S(dest, destsize, src, count) memcpy_s(dest, destsize, src, count)
40 # if defined(UNICODE)
41 # define _COM_PRINTF_S_1(dest, destsize, format, arg1) swprintf_s(dest, destsize, format, arg1)
42 # else
43 # define _COM_PRINTF_S_1(dest, destsize, format, arg1) sprintf_s(dest, destsize, format, arg1)
44 # endif
45 
46 #else
47 
48 # define _COM_MEMCPY_S(dest, destsize, src, count) memcpy(dest, src, count)
49 # define _COM_PRINTF_S_1(dest, destsize, format, arg1) wsprintf(dest, format, arg1)
50 
51 #endif
52 
53 #pragma warning(push)
54 #pragma warning(disable: 4290)
55 #pragma warning(disable: 4310)
56 
57 #pragma push_macro("new")
58 #undef new
59 
60 /* Add macros if the macros were not defined. */
61 #ifndef S_OK
62 #define S_OK ((HRESULT)0L)
63 #endif
64 #ifndef INTSAFE_E_ARITHMETIC_OVERFLOW
65 #define INTSAFE_E_ARITHMETIC_OVERFLOW ((HRESULT)0x80070216L) // 0x216 = 534 = ERROR_ARITHMETIC_OVERFLOW
66 #endif
67 #ifndef INTSAFE_UINT_MAX
68 #define INTSAFE_UINT_MAX 0xffffffff
69 #endif
70 #ifndef FAILED
71 #define FAILED(hr) (((HRESULT)(hr)) < 0)
72 #endif
73 
74 class _com_error;
75 
76 void __declspec(noreturn) __stdcall _com_issue_error(HRESULT);
77 
79 //
80 // Forward class declarations
81 //
83 
84 class _bstr_t;
85 class _variant_t;
86 
88 //
89 // Error checking routines
90 //
92 
93 namespace _com_util {
94  inline void CheckError(HRESULT hr)
95  {
96  if (FAILED(hr)) {
97  _com_issue_error(hr);
98  }
99  }
100  static HRESULT UIntAdd(UINT uAugend, UINT uAddend, UINT *puResult)
101  {
102  if((uAugend + uAddend) < uAddend)
103  {
105  }
106  *puResult = uAugend + uAddend;
107  return S_OK;
108  }
109 
110  static HRESULT UIntMult(UINT uMultiplicand, UINT uMultiplier, UINT *puResult)
111  {
112  ULONGLONG ull64Result = UInt32x32To64(uMultiplicand, uMultiplier);
113  if(ull64Result <= INTSAFE_UINT_MAX)
114  {
115  *puResult = (UINT)ull64Result;
116  return S_OK;
117  }
119  }
120 }
121 
123 //
124 // Routines for handling conversions between BSTR and char*
125 //
127 
128 namespace _com_util {
129  // Convert char * to BSTR
130  //
131  BSTR __stdcall ConvertStringToBSTR(const char* pSrc) ;
132 
133  // Convert BSTR to char *
134  //
135  char* __stdcall ConvertBSTRToString(BSTR pSrc) ;
136 }
137 
139 //
140 // Wrapper class for BSTR
141 //
143 
144 class _bstr_t {
145 public:
146  // Constructors
147  //
148  _bstr_t() throw();
149  _bstr_t(const _bstr_t& s) throw();
150  _bstr_t(const char* s) ;
151  _bstr_t(const wchar_t* s) ;
152  _bstr_t(const _variant_t& var) ;
153  _bstr_t(BSTR bstr, bool fCopy) ;
154 
155  // Destructor
156  //
157  ~_bstr_t() throw();
158 
159  // Assignment operators
160  //
161  _bstr_t& operator=(const _bstr_t& s) throw();
162  _bstr_t& operator=(const char* s) ;
163  _bstr_t& operator=(const wchar_t* s) ;
164  _bstr_t& operator=(const _variant_t& var) ;
165 
166  // Operators
167  //
168  _bstr_t& operator+=(const _bstr_t& s) ;
169  _bstr_t operator+(const _bstr_t& s) const ;
170 
171  // Friend operators
172  //
173  friend _bstr_t operator+(const char* s1, const _bstr_t& s2) ;
174  friend _bstr_t operator+(const wchar_t* s1, const _bstr_t& s2) ;
175 
176  // Extractors
177  //
178  operator const wchar_t*() const throw();
179  operator wchar_t*() const throw();
180  operator const char*() const ;
181  operator char*() const ;
182 
183  // Comparison operators
184  //
185  bool operator!() const throw();
186  bool operator==(const _bstr_t& str) const throw();
187  bool operator!=(const _bstr_t& str) const throw();
188  bool operator<(const _bstr_t& str) const throw();
189  bool operator>(const _bstr_t& str) const throw();
190  bool operator<=(const _bstr_t& str) const throw();
191  bool operator>=(const _bstr_t& str) const throw();
192 
193  // Low-level helper functions
194  //
195  BSTR copy(bool fCopy = true) const ;
196  unsigned int length() const throw();
197 
198  // Binary string assign
199  //
200  void Assign(BSTR s) ;
201 
202  // Get the physical BSTR
203  //
204  BSTR& GetBSTR() ;
205  BSTR* GetAddress() ;
206 
207  // Attach to the internal BSTR w/o copying
208  //
209  void Attach(BSTR s) ;
210 
211  // Detach the internal BSTR
212  //
213  BSTR Detach();
214 
215 private:
216  // Referenced counted wrapper
217  //
218  class Data_t {
219  public:
220  // Constructors
221  //
222  Data_t(const char* s) ;
223  Data_t(const wchar_t* s) ;
224  Data_t(BSTR bstr, bool fCopy) ;
225  Data_t(const _bstr_t& s1, const _bstr_t& s2) ;
226 
227  // Reference counting routines
228  //
229  unsigned long AddRef() throw();
230  unsigned long Release() throw();
231  unsigned long RefCount() const throw();
232 
233  // Extractors
234  //
235  operator const wchar_t*() const throw();
236  operator const char*() const ;
237 
238  // Low-level helper functions
239  //
240  const wchar_t* GetWString() const throw();
241  wchar_t*& GetWString() throw();
242  const char* GetString() const ;
243 
244  BSTR Copy() const ;
245  void Assign(BSTR s) ;
246  void Attach(BSTR s) throw();
247  unsigned int Length() const throw();
248  int Compare(const Data_t& str) const throw();
249 
250  // Exception agnostic wrapper for new
251  //
252  void* operator new(size_t sz);
253 
254  private:
255  BSTR m_wstr;
256  mutable char* m_str;
257  unsigned long m_RefCount;
258 
259  // Never allow default construction
260  //
261  Data_t() throw();
262 
263  // Never allow copy
264  //
265  Data_t(const Data_t& s) throw();
266 
267  // Prevent deletes from outside. Release() must be used.
268  //
269  ~Data_t() throw();
270 
271  void _Free() throw();
272  };
273 
274 private:
275  // Reference counted representation
276  //
278 
279 private:
280  // Low-level utilities
281  //
282  void _AddRef() throw();
283  void _Free() throw();
284  int _Compare(const _bstr_t& str) const throw();
285 };
286 
288 //
289 // Constructors
290 //
292 
293 // Default constructor
294 //
295 inline _bstr_t::_bstr_t() throw()
296  : m_Data(NULL)
297 {
298 }
299 
300 // Copy constructor
301 //
302 inline _bstr_t::_bstr_t(const _bstr_t& s) throw()
303  : m_Data(s.m_Data)
304 {
305  _AddRef();
306 }
307 
308 // Construct a _bstr_t from a const char*
309 //
310 inline _bstr_t::_bstr_t(const char* s)
311  : m_Data(new Data_t(s))
312 {
313  if (m_Data == NULL) {
314  _com_issue_error(E_OUTOFMEMORY);
315  }
316 }
317 
318 // Construct a _bstr_t from a const whar_t*
319 //
320 inline _bstr_t::_bstr_t(const wchar_t* s)
321  : m_Data(new Data_t(s))
322 {
323  if (m_Data == NULL) {
324  _com_issue_error(E_OUTOFMEMORY);
325  }
326 }
327 
328 // Construct a _bstr_t from a BSTR. If fCopy is FALSE, give control of
329 // data to the _bstr_t without making a new copy.
330 //
331 inline _bstr_t::_bstr_t(BSTR bstr, bool fCopy)
332  : m_Data(new Data_t(bstr, fCopy))
333 {
334  if (m_Data == NULL) {
335  _com_issue_error(E_OUTOFMEMORY);
336  }
337 }
338 
339 // Destructor
340 //
341 inline _bstr_t::~_bstr_t() throw()
342 {
343  _Free();
344 }
345 
347 //
348 // Assignment operators
349 //
351 
352 // Default assign operator
353 //
354 inline _bstr_t& _bstr_t::operator=(const _bstr_t& s) throw()
355 {
356  if (this != &s) {
357  _Free();
358 
359  m_Data = s.m_Data;
360  _AddRef();
361  }
362 
363  return *this;
364 }
365 
366 // Assign a const char* to a _bstr_t
367 //
368 inline _bstr_t& _bstr_t::operator=(const char* s)
369 {
370  _COM_ASSERT(s == NULL || static_cast<const char*>(*this) != s);
371 
372  if (s == NULL || static_cast<const char*>(*this) != s)
373  {
374  _Free();
375 
376  m_Data = new Data_t(s);
377  if (m_Data == NULL) {
378  _com_issue_error(E_OUTOFMEMORY);
379  }
380  }
381 
382  return *this;
383 }
384 
385 // Assign a const wchar_t* to a _bstr_t
386 //
387 inline _bstr_t& _bstr_t::operator=(const wchar_t* s)
388 {
389  _COM_ASSERT(s == NULL || static_cast<const wchar_t*>(*this) != s);
390 
391  if (s == NULL || static_cast<const wchar_t*>(*this) != s)
392  {
393  _Free();
394 
395  m_Data = new Data_t(s);
396  if (m_Data == NULL) {
397  _com_issue_error(E_OUTOFMEMORY);
398  }
399  }
400 
401  return *this;
402 }
403 
405 //
406 // Operators
407 //
409 
410 // Concatenate a _bstr_t onto this _bstr_t
411 //
413 {
414  Data_t* newData = new Data_t(*this, s);
415  if (newData == NULL) {
416  _com_issue_error(E_OUTOFMEMORY);
417  }
418  else {
419  _Free();
420  m_Data = newData;
421  }
422 
423  return *this;
424 }
425 
426 // Return the concatenation of this _bstr_t with another _bstr_t
427 //
428 inline _bstr_t _bstr_t::operator+(const _bstr_t& s) const
429 {
430  _bstr_t b = *this;
431  b += s;
432 
433  return b;
434 }
435 
437 //
438 // Friend Operators
439 //
441 
442 // Return the concatenation of a const char* with a _bstr_t
443 //
444 inline _bstr_t operator+(const char* s1, const _bstr_t& s2)
445 {
446  _bstr_t b = s1;
447  b += s2;
448 
449  return b;
450 }
451 
452 // Return the concatenation of a const char* with a _bstr_t
453 //
454 inline _bstr_t operator+(const wchar_t* s1, const _bstr_t& s2)
455 {
456  _bstr_t b = s1;
457  b += s2;
458 
459  return b;
460 }
461 
463 //
464 // Extractors
465 //
467 
468 // Extract a const wchar_t*
469 //
470 inline _bstr_t::operator const wchar_t*() const throw()
471 {
472  return (m_Data != NULL) ? m_Data->GetWString() : NULL;
473 }
474 
475 // Extract a wchar_t*
476 //
477 inline _bstr_t::operator wchar_t*() const throw()
478 {
479  return const_cast<wchar_t*>((m_Data != NULL) ? m_Data->GetWString() : NULL);
480 }
481 
482 // Extract a const char_t*
483 //
484 inline _bstr_t::operator const char*() const
485 {
486  return (m_Data != NULL) ? m_Data->GetString() : NULL;
487 }
488 
489 // Extract a char_t*
490 //
491 inline _bstr_t::operator char*() const
492 {
493  return const_cast<char*>((m_Data != NULL) ? m_Data->GetString() : NULL);
494 }
495 
497 //
498 // Comparison operators
499 //
501 
502 inline bool _bstr_t::operator!() const throw()
503 {
504  return (m_Data != NULL) ? !m_Data->GetWString() : true;
505 }
506 
507 inline bool _bstr_t::operator==(const _bstr_t& str) const throw()
508 {
509  return _Compare(str) == 0;
510 }
511 
512 inline bool _bstr_t::operator!=(const _bstr_t& str) const throw()
513 {
514  return _Compare(str) != 0;
515 }
516 
517 inline bool _bstr_t::operator<(const _bstr_t& str) const throw()
518 {
519  return _Compare(str) < 0;
520 }
521 
522 inline bool _bstr_t::operator>(const _bstr_t& str) const throw()
523 {
524  return _Compare(str) > 0;
525 }
526 
527 inline bool _bstr_t::operator<=(const _bstr_t& str) const throw()
528 {
529  return _Compare(str) <= 0;
530 }
531 
532 inline bool _bstr_t::operator>=(const _bstr_t& str) const throw()
533 {
534  return _Compare(str) >= 0;
535 }
536 
538 //
539 // Low-level help functions
540 //
542 
543 // Extract a copy of the wrapped BSTR
544 //
545 inline BSTR _bstr_t::copy(bool fCopy) const
546 {
547  return (m_Data != NULL) ? (fCopy ? m_Data->Copy() : m_Data->GetWString()) : NULL;
548 }
549 
550 // Return the length of the wrapped BSTR
551 //
552 inline unsigned int _bstr_t::length() const throw()
553 {
554  return (m_Data != NULL) ? m_Data->Length() : 0;
555 }
556 
557 // Binary string assign
558 //
559 inline void _bstr_t::Assign(BSTR s)
560 {
561  _COM_ASSERT(s == NULL || m_Data == NULL || m_Data->GetWString() != s);
562 
563  if (s == NULL || m_Data == NULL || m_Data->GetWString() != s)
564  {
565  _Free();
566 
567  m_Data = new Data_t(s, TRUE);
568  if (m_Data == NULL) {
569  _com_issue_error(E_OUTOFMEMORY);
570  }
571  }
572 }
573 
574 // Get the physical BSTR
575 //
576 inline BSTR& _bstr_t::GetBSTR()
577 {
578  if (m_Data == NULL) {
579  m_Data = new Data_t(0, FALSE);
580  if (m_Data == NULL) {
581  _com_issue_error(E_OUTOFMEMORY);
582  }
583  }
584  return m_Data->GetWString();
585 }
586 
587 // Get the address of the physical BSTR to pass as an 'out'-parameter
588 //
589 inline BSTR* _bstr_t::GetAddress()
590 {
591  Attach(0);
592  return &m_Data->GetWString();
593 }
594 
595 // Attach to the internal BSTR w/o copying
596 //
597 inline void _bstr_t::Attach(BSTR s)
598 {
599  _Free();
600 
601  m_Data = new Data_t(s, FALSE);
602  if (m_Data == NULL) {
603  _com_issue_error(E_OUTOFMEMORY);
604  }
605 }
606 
607 // Detach the internal BSTR
608 //
609 inline BSTR _bstr_t::Detach()
610 {
611  _COM_ASSERT(m_Data != NULL && m_Data->RefCount() == 1);
612 
613  if (m_Data != NULL && m_Data->RefCount() == 1) {
614  BSTR b = m_Data->GetWString();
615  m_Data->GetWString() = NULL;
616  _Free();
617  return b;
618  }
619  else {
620  _com_issue_error(E_POINTER);
621  }
622 }
623 
624 // AddRef the BSTR
625 //
626 inline void _bstr_t::_AddRef() throw()
627 {
628  if (m_Data != NULL) {
629  m_Data->AddRef();
630  }
631 }
632 
633 // Free the BSTR
634 //
635 inline void _bstr_t::_Free() throw()
636 {
637  if (m_Data != NULL) {
638  m_Data->Release();
639  m_Data = NULL;
640  }
641 }
642 
643 // Compare two _bstr_t objects
644 //
645 inline int _bstr_t::_Compare(const _bstr_t& str) const throw()
646 {
647  if (m_Data == str.m_Data) {
648  return 0;
649  }
650 
651  if (m_Data == NULL) {
652  if (str.length() == 0) {
653  return 0;
654  }
655  else {
656  return -1;
657  }
658  }
659 
660  if (str.m_Data == NULL){
661  if (this->length() == 0) {
662  return 0;
663  }
664  else {
665  return 1;
666  }
667  }
668 
669  return m_Data->Compare(*str.m_Data);
670 }
671 
673 //
674 // Reference counted wrapper - Constructors
675 //
677 
678 // Construct a Data_t from a const char*
679 //
680 inline _bstr_t::Data_t::Data_t(const char* s)
681  : m_str(NULL), m_RefCount(1)
682 {
683  m_wstr = _com_util::ConvertStringToBSTR(s);
684 }
685 
686 // Construct a Data_t from a const wchar_t*
687 //
688 inline _bstr_t::Data_t::Data_t(const wchar_t* s)
689  : m_str(NULL), m_RefCount(1)
690 {
691  m_wstr = ::SysAllocString(s);
692 
693  if (m_wstr == NULL && s != NULL) {
694  _com_issue_error(E_OUTOFMEMORY);
695  }
696 }
697 
698 // Construct a Data_t from a BSTR. If fCopy is FALSE, give control of
699 // data to the Data_t without doing a SysAllocStringByteLen.
700 //
701 inline _bstr_t::Data_t::Data_t(BSTR bstr, bool fCopy)
702  : m_str(NULL), m_RefCount(1)
703 {
704  if (fCopy && bstr != NULL) {
705  m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
706  ::SysStringByteLen(bstr));
707 
708  if (m_wstr == NULL) {
709  _com_issue_error(E_OUTOFMEMORY);
710  }
711  }
712  else {
713  m_wstr = bstr;
714  }
715 }
716 
717 // Construct a Data_t from the concatenation of two _bstr_t objects
718 //
719 inline _bstr_t::Data_t::Data_t(const _bstr_t& s1, const _bstr_t& s2)
720  : m_str(NULL), m_RefCount(1)
721 {
722  const unsigned int l1 = s1.length();
723  const unsigned int l2 = s2.length();
724  unsigned int l3;
725 
726  if (FAILED(_com_util::UIntAdd(l1, l2, &l3)) ||
727  FAILED(_com_util::UIntMult(l3, sizeof(wchar_t), &l3)))
728  {
729  _com_issue_error(E_OUTOFMEMORY);
730  return;
731  }
732 
733  m_wstr = ::SysAllocStringByteLen(NULL, (l1 + l2) * sizeof(wchar_t));
734  if (m_wstr == NULL)
735  {
736  if (l1 + l2 == 0)
737  {
738  return;
739  }
740  _com_issue_error(E_OUTOFMEMORY);
741  return;
742  }
743 
744  const wchar_t* wstr1 = static_cast<const wchar_t*>(s1);
745 
746  if (wstr1 != NULL)
747  {
748  _COM_MEMCPY_S(m_wstr, (l1 + l2 + 1) * sizeof(wchar_t), wstr1, (l1 + 1) * sizeof(wchar_t));
749  }
750 
751  const wchar_t* wstr2 = static_cast<const wchar_t*>(s2);
752 
753  if (wstr2 != NULL)
754  {
755  _COM_MEMCPY_S(m_wstr + l1, (l2 + 1) * sizeof(wchar_t), wstr2, (l2 + 1) * sizeof(wchar_t));
756  }
757 }
758 
760 //
761 // Reference counted wrapper - reference counting routines
762 //
764 
765 inline unsigned long _bstr_t::Data_t::AddRef() throw()
766 {
767  InterlockedIncrement(reinterpret_cast<long*>(&m_RefCount));
768  return m_RefCount;
769 }
770 
771 inline unsigned long _bstr_t::Data_t::Release() throw()
772 {
773  unsigned long cRef = InterlockedDecrement(reinterpret_cast<long*>(&m_RefCount));
774  if (cRef == 0) {
775  delete this;
776  }
777 
778  return cRef;
779 }
780 
781 inline unsigned long _bstr_t::Data_t::RefCount() const throw()
782 {
783  return m_RefCount;
784 }
785 
787 //
788 // Reference counted wrapper - extractors
789 //
791 
792 // Extract a const wchar_t*
793 //
794 inline _bstr_t::Data_t::operator const wchar_t*() const throw()
795 {
796  return m_wstr;
797 }
798 
799 // Extract a const char_t*
800 //
801 inline _bstr_t::Data_t::operator const char*() const
802 {
803  return GetString();
804 }
805 
807 //
808 // Reference counted wrapper - helper functions
809 //
811 
812 inline const wchar_t* _bstr_t::Data_t::GetWString() const throw()
813 {
814  return m_wstr;
815 }
816 
817 inline wchar_t*& _bstr_t::Data_t::GetWString() throw()
818 {
819  return m_wstr;
820 }
821 
822 inline const char* _bstr_t::Data_t::GetString() const
823 {
824  if (m_str == NULL) {
825  m_str = _com_util::ConvertBSTRToString(m_wstr);
826  }
827 
828  return m_str;
829 }
830 
831 // Return a copy of the wrapped BSTR
832 //
833 inline BSTR _bstr_t::Data_t::Copy() const
834 {
835  if (m_wstr != NULL) {
836  BSTR bstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(m_wstr), ::SysStringByteLen(m_wstr));
837 
838  if (bstr == NULL) {
839  _com_issue_error(E_OUTOFMEMORY);
840  }
841 
842  return bstr;
843  }
844 
845  return NULL;
846 }
847 
848 inline void _bstr_t::Data_t::Assign(BSTR s)
849 {
850  _Free();
851 
852  if (s != NULL) {
853  m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(s), ::SysStringByteLen(s));
854  m_str = 0;
855  }
856 }
857 
858 inline void _bstr_t::Data_t::Attach(BSTR s) throw()
859 {
860  _Free();
861 
862  m_wstr = s;
863  m_str = 0;
864  m_RefCount = 1;
865 }
866 
867 // Return the length of the wrapper BSTR
868 //
869 inline unsigned int _bstr_t::Data_t::Length() const throw()
870 {
871  return m_wstr ? ::SysStringLen(m_wstr) : 0;
872 }
873 
874 // Compare two wrapped BSTRs
875 //
876 inline int _bstr_t::Data_t::Compare(const _bstr_t::Data_t& str) const throw()
877 {
878  // Dont need to check for NULL here, because
879  // SysStringLen will return 0 if you pass in NULL
880  const unsigned int l1 = ::SysStringLen(m_wstr);
881  const unsigned int l2 = ::SysStringLen(str.m_wstr);
882 
883  unsigned int len = l1;
884  if (len > l2) {
885  len = l2;
886  }
887 
888  BSTR bstr1 = m_wstr;
889  BSTR bstr2 = str.m_wstr;
890 
891  while (len-- > 0) {
892  if (*bstr1++ != *bstr2++) {
893  return bstr1[-1] - bstr2[-1];
894  }
895  }
896 
897  return (l1 < l2) ? -1 : (l1 == l2) ? 0 : 1;
898 }
899 
900 // Exception agnostic wrapper for new
901 //
902 #ifdef _COM_OPERATOR_NEW_THROWS
903 inline void* _bstr_t::Data_t::operator new(size_t sz)
904 {
905  try {
906  return ::operator new(sz);
907  }
908  catch (...) {
909  return NULL;
910  }
911 }
912 #else // _COM_OPERATOR_NEW_THROWS
913 inline void* _bstr_t::Data_t::operator new(size_t sz)
914 {
915  return ::operator new(sz);
916 }
917 #endif // _COM_OPERATOR_NEW_THROWS
918 
919 // Destruct this object
920 //
921 inline _bstr_t::Data_t::~Data_t() throw()
922 {
923  _Free();
924 }
925 
926 // Free up this object
927 //
928 inline void _bstr_t::Data_t::_Free() throw()
929 {
930  if (m_wstr != NULL) {
931  ::SysFreeString(m_wstr);
932  m_wstr = NULL;
933  }
934 
935  if (m_str != NULL) {
936  delete [] m_str;
937  m_str = NULL;
938  }
939 }
940 
942 //
943 // Wrapper class for VARIANT
944 //
946 
947 /*
948  * VARENUM usage key,
949  *
950  * * [V] - may appear in a VARIANT
951  * * [T] - may appear in a TYPEDESC
952  * * [P] - may appear in an OLE property set
953  * * [S] - may appear in a Safe Array
954  * * [C] - supported by class _variant_t
955  *
956  *
957  * VT_EMPTY [V] [P] nothing
958  * VT_NULL [V] [P] SQL style Null
959  * VT_I2 [V][T][P][S][C] 2 byte signed int
960  * VT_I4 [V][T][P][S][C] 4 byte signed int
961  * VT_R4 [V][T][P][S][C] 4 byte real
962  * VT_R8 [V][T][P][S][C] 8 byte real
963  * VT_CY [V][T][P][S][C] currency
964  * VT_DATE [V][T][P][S][C] date
965  * VT_BSTR [V][T][P][S][C] OLE Automation string
966  * VT_DISPATCH [V][T][P][S][C] IDispatch *
967  * VT_ERROR [V][T] [S][C] SCODE
968  * VT_BOOL [V][T][P][S][C] True=-1, False=0
969  * VT_VARIANT [V][T][P][S] VARIANT *
970  * VT_UNKNOWN [V][T] [S][C] IUnknown *
971  * VT_DECIMAL [V][T] [S][C] 16 byte fixed point
972  * VT_I1 [T] signed char
973  * VT_UI1 [V][T][P][S][C] unsigned char
974  * VT_UI2 [T][P] unsigned short
975  * VT_UI4 [T][P] unsigned short
976  * VT_I8 [T][P] signed 64-bit int
977  * VT_UI8 [T][P] unsigned 64-bit int
978  * VT_INT [T] signed machine int
979  * VT_UINT [T] unsigned machine int
980  * VT_VOID [T] C style void
981  * VT_HRESULT [T] Standard return type
982  * VT_PTR [T] pointer type
983  * VT_SAFEARRAY [T] (use VT_ARRAY in VARIANT)
984  * VT_CARRAY [T] C style array
985  * VT_USERDEFINED [T] user defined type
986  * VT_LPSTR [T][P] null terminated string
987  * VT_LPWSTR [T][P] wide null terminated string
988  * VT_FILETIME [P] FILETIME
989  * VT_BLOB [P] Length prefixed bytes
990  * VT_STREAM [P] Name of the stream follows
991  * VT_STORAGE [P] Name of the storage follows
992  * VT_STREAMED_OBJECT [P] Stream contains an object
993  * VT_STORED_OBJECT [P] Storage contains an object
994  * VT_BLOB_OBJECT [P] Blob contains an object
995  * VT_CF [P] Clipboard format
996  * VT_CLSID [P] A Class ID
997  * VT_VECTOR [P] simple counted array
998  * VT_ARRAY [V] SAFEARRAY*
999  * VT_BYREF [V] void* for local use
1000  */
1001 
1002 class _variant_t : public ::tagVARIANT {
1003 public:
1004  // Constructors
1005  //
1006  _variant_t() throw();
1007 
1008  _variant_t(const VARIANT& varSrc) ;
1009  _variant_t(const VARIANT* pSrc) ;
1010  _variant_t(const _variant_t& varSrc) ;
1011 
1012  _variant_t(VARIANT& varSrc, bool fCopy) ; // Attach VARIANT if !fCopy
1013 
1014  _variant_t(short sSrc, VARTYPE vtSrc = VT_I2) ; // Creates a VT_I2, or a VT_BOOL
1015  _variant_t(long lSrc, VARTYPE vtSrc = VT_I4) ; // Creates a VT_I4, a VT_ERROR, or a VT_BOOL
1016  _variant_t(float fltSrc) throw(); // Creates a VT_R4
1017  _variant_t(double dblSrc, VARTYPE vtSrc = VT_R8) ; // Creates a VT_R8, or a VT_DATE
1018  _variant_t(const CY& cySrc) throw(); // Creates a VT_CY
1019  _variant_t(const _bstr_t& bstrSrc) ; // Creates a VT_BSTR
1020  _variant_t(const wchar_t *pSrc) ; // Creates a VT_BSTR
1021  _variant_t(const char* pSrc) ; // Creates a VT_BSTR
1022  _variant_t(IDispatch* pSrc, bool fAddRef = true) throw(); // Creates a VT_DISPATCH
1023  _variant_t(bool boolSrc) throw(); // Creates a VT_BOOL
1024  _variant_t(IUnknown* pSrc, bool fAddRef = true) throw(); // Creates a VT_UNKNOWN
1025  _variant_t(const DECIMAL& decSrc) throw(); // Creates a VT_DECIMAL
1026  _variant_t(BYTE bSrc) throw(); // Creates a VT_UI1
1027 
1028  _variant_t(char cSrc) throw(); // Creates a VT_I1
1029  _variant_t(unsigned short usSrc) throw(); // Creates a VT_UI2
1030  _variant_t(unsigned long ulSrc) throw(); // Creates a VT_UI4
1031  _variant_t(int iSrc) throw(); // Creates a VT_INT
1032  _variant_t(unsigned int uiSrc) throw(); // Creates a VT_UINT
1033 #if (_WIN32_WINNT >= 0x0501)
1034  _variant_t(__int64 i8Src) throw(); // Creates a VT_I8
1035  _variant_t(unsigned __int64 ui8Src) throw(); // Creates a VT_UI8
1036 #endif
1037 
1038  // Destructor
1039  //
1040  ~_variant_t() throw() ;
1041 
1042  // Extractors
1043  //
1044  operator short() const ; // Extracts a short from a VT_I2
1045  operator long() const ; // Extracts a long from a VT_I4
1046  operator float() const ; // Extracts a float from a VT_R4
1047  operator double() const ; // Extracts a double from a VT_R8
1048  operator CY() const ; // Extracts a CY from a VT_CY
1049  operator _bstr_t() const ; // Extracts a _bstr_t from a VT_BSTR
1050  operator IDispatch*() const ; // Extracts a IDispatch* from a VT_DISPATCH
1051  operator bool() const ; // Extracts a bool from a VT_BOOL
1052  operator IUnknown*() const ; // Extracts a IUnknown* from a VT_UNKNOWN
1053  operator DECIMAL() const ; // Extracts a DECIMAL from a VT_DECIMAL
1054  operator BYTE() const ; // Extracts a BTYE (unsigned char) from a VT_UI1
1055  operator VARIANT() const throw();
1056 
1057  operator char() const ; // Extracts a char from a VT_I1
1058  operator unsigned short() const ; // Extracts a unsigned short from a VT_UI2
1059  operator unsigned long() const ; // Extracts a unsigned long from a VT_UI4
1060  operator int() const ; // Extracts a int from a VT_INT
1061  operator unsigned int() const ; // Extracts a unsigned int from a VT_UINT
1062 #if (_WIN32_WINNT >= 0x0501)
1063  operator __int64() const ; // Extracts a __int64 from a VT_I8
1064  operator unsigned __int64() const ; // Extracts a unsigned __int64 from a VT_UI8
1065 #endif
1066 
1067  // Assignment operations
1068  //
1069  _variant_t& operator=(const VARIANT& varSrc) ;
1070  _variant_t& operator=(const VARIANT* pSrc) ;
1071  _variant_t& operator=(const _variant_t& varSrc) ;
1072 
1073  _variant_t& operator=(short sSrc) ; // Assign a VT_I2, or a VT_BOOL
1074  _variant_t& operator=(long lSrc) ; // Assign a VT_I4, a VT_ERROR or a VT_BOOL
1075  _variant_t& operator=(float fltSrc) ; // Assign a VT_R4
1076  _variant_t& operator=(double dblSrc) ; // Assign a VT_R8, or a VT_DATE
1077  _variant_t& operator=(const CY& cySrc) ; // Assign a VT_CY
1078  _variant_t& operator=(const _bstr_t& bstrSrc) ; // Assign a VT_BSTR
1079  _variant_t& operator=(const wchar_t* pSrc) ; // Assign a VT_BSTR
1080  _variant_t& operator=(const char* pSrc) ; // Assign a VT_BSTR
1081  _variant_t& operator=(IDispatch* pSrc) ; // Assign a VT_DISPATCH
1082  _variant_t& operator=(bool boolSrc) ; // Assign a VT_BOOL
1083  _variant_t& operator=(IUnknown* pSrc) ; // Assign a VT_UNKNOWN
1084  _variant_t& operator=(const DECIMAL& decSrc) ; // Assign a VT_DECIMAL
1085  _variant_t& operator=(BYTE bSrc) ; // Assign a VT_UI1
1086 
1087  _variant_t& operator=(char cSrc) ; // Assign a VT_I1
1088  _variant_t& operator=(unsigned short usSrc) ; // Assign a VT_UI2
1089  _variant_t& operator=(unsigned long ulSrc) ; // Assign a VT_UI4
1090  _variant_t& operator=(int iSrc) ; // Assign a VT_INT
1091  _variant_t& operator=(unsigned int uiSrc) ; // Assign a VT_UINT
1092 #if (_WIN32_WINNT >= 0x0501)
1093  _variant_t& operator=(__int64 i8Src) ; // Assign a VT_I8
1094  _variant_t& operator=(unsigned __int64 ui8Src) ; // Assign a VT_UI8
1095 #endif
1096 
1097  // Comparison operations
1098  //
1099  bool operator==(const VARIANT& varSrc) const throw();
1100  bool operator==(const VARIANT* pSrc) const throw();
1101 
1102  bool operator!=(const VARIANT& varSrc) const throw();
1103  bool operator!=(const VARIANT* pSrc) const throw();
1104 
1105  // Low-level operations
1106  //
1107  void Clear() ;
1108 
1109  void Attach(VARIANT& varSrc) ;
1110  VARIANT Detach() throw();
1111 
1112  VARIANT& GetVARIANT() throw();
1113  VARIANT* GetAddress() ;
1114 
1115  void ChangeType(VARTYPE vartype, const _variant_t* pSrc = NULL) ;
1116 
1117  void SetString(const char* pSrc) ; // used to set ANSI string
1118 };
1119 
1121 //
1122 // Constructors
1123 //
1125 
1126 // Default constructor
1127 //
1128 inline _variant_t::_variant_t() throw()
1129 {
1130  ::VariantInit(this);
1131 }
1132 
1133 // Construct a _variant_t from a const VARIANT&
1134 //
1135 inline _variant_t::_variant_t(const VARIANT& varSrc)
1136 {
1137  ::VariantInit(this);
1138  _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
1139 }
1140 
1141 // Construct a _variant_t from a const VARIANT*
1142 //
1143 inline _variant_t::_variant_t(const VARIANT* pSrc)
1144 {
1145  if (pSrc == NULL) {
1146  _com_issue_error(E_POINTER);
1147  }
1148  else {
1149  ::VariantInit(this);
1150  _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
1151  }
1152 }
1153 
1154 // Construct a _variant_t from a const _variant_t&
1155 //
1156 inline _variant_t::_variant_t(const _variant_t& varSrc)
1157 {
1158  ::VariantInit(this);
1159  _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
1160 }
1161 
1162 // Construct a _variant_t from a VARIANT&. If fCopy is FALSE, give control of
1163 // data to the _variant_t without doing a VariantCopy.
1164 //
1165 inline _variant_t::_variant_t(VARIANT& varSrc, bool fCopy)
1166 {
1167  if (fCopy) {
1168  ::VariantInit(this);
1169  _com_util::CheckError(::VariantCopy(this, &varSrc));
1170  }
1171  else {
1172  _COM_MEMCPY_S(this, sizeof(varSrc), &varSrc, sizeof(varSrc));
1173  V_VT(&varSrc) = VT_EMPTY;
1174  }
1175 }
1176 
1177 // Construct either a VT_I2 VARIANT or a VT_BOOL VARIANT from
1178 // a short (the default is VT_I2)
1179 //
1180 inline _variant_t::_variant_t(short sSrc, VARTYPE vtSrc)
1181 {
1182  if ((vtSrc != VT_I2) && (vtSrc != VT_BOOL)) {
1183  _com_issue_error(E_INVALIDARG);
1184  return;
1185  }
1186 
1187  if (vtSrc == VT_BOOL) {
1188  V_VT(this) = VT_BOOL;
1189  V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
1190  }
1191  else {
1192  V_VT(this) = VT_I2;
1193  V_I2(this) = sSrc;
1194  }
1195 }
1196 
1197 // Construct either a VT_I4 VARIANT, a VT_BOOL VARIANT, or a
1198 // VT_ERROR VARIANT from a long (the default is VT_I4)
1199 //
1200 inline _variant_t::_variant_t(long lSrc, VARTYPE vtSrc)
1201 {
1202  if ((vtSrc != VT_I4) && (vtSrc != VT_ERROR) && (vtSrc != VT_BOOL)) {
1203  _com_issue_error(E_INVALIDARG);
1204  return;
1205  }
1206 
1207  if (vtSrc == VT_ERROR) {
1208  V_VT(this) = VT_ERROR;
1209  V_ERROR(this) = lSrc;
1210  }
1211  else if (vtSrc == VT_BOOL) {
1212  V_VT(this) = VT_BOOL;
1213  V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
1214  }
1215  else {
1216  V_VT(this) = VT_I4;
1217  V_I4(this) = lSrc;
1218  }
1219 }
1220 
1221 // Construct a VT_R4 VARIANT from a float
1222 //
1223 inline _variant_t::_variant_t(float fltSrc) throw()
1224 {
1225  V_VT(this) = VT_R4;
1226  V_R4(this) = fltSrc;
1227 }
1228 
1229 // Construct either a VT_R8 VARIANT, or a VT_DATE VARIANT from
1230 // a double (the default is VT_R8)
1231 //
1232 inline _variant_t::_variant_t(double dblSrc, VARTYPE vtSrc)
1233 {
1234  if ((vtSrc != VT_R8) && (vtSrc != VT_DATE)) {
1235  _com_issue_error(E_INVALIDARG);
1236  return;
1237  }
1238 
1239  if (vtSrc == VT_DATE) {
1240  V_VT(this) = VT_DATE;
1241  V_DATE(this) = dblSrc;
1242  }
1243  else {
1244  V_VT(this) = VT_R8;
1245  V_R8(this) = dblSrc;
1246  }
1247 }
1248 
1249 // Construct a VT_CY from a CY
1250 //
1251 inline _variant_t::_variant_t(const CY& cySrc) throw()
1252 {
1253  V_VT(this) = VT_CY;
1254  V_CY(this) = cySrc;
1255 }
1256 
1257 // Construct a VT_BSTR VARIANT from a const _bstr_t&
1258 //
1259 inline _variant_t::_variant_t(const _bstr_t& bstrSrc)
1260 {
1261  V_VT(this) = VT_BSTR;
1262 
1263  BSTR bstr = static_cast<wchar_t*>(bstrSrc);
1264  if (bstr == NULL) {
1265  V_BSTR(this) = NULL;
1266  }
1267  else {
1268  V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
1269  ::SysStringByteLen(bstr));
1270  if (V_BSTR(this) == NULL) {
1271  _com_issue_error(E_OUTOFMEMORY);
1272  }
1273  }
1274 }
1275 
1276 // Construct a VT_BSTR VARIANT from a const wchar_t*
1277 //
1278 inline _variant_t::_variant_t(const wchar_t* pSrc)
1279 {
1280  V_VT(this) = VT_BSTR;
1281 
1282  V_BSTR(this) = ::SysAllocString(pSrc);
1283 
1284  if (V_BSTR(this) == NULL && pSrc != NULL) {
1285  _com_issue_error(E_OUTOFMEMORY);
1286  }
1287 }
1288 
1289 // Construct a VT_BSTR VARIANT from a const char*
1290 //
1291 inline _variant_t::_variant_t(const char* pSrc)
1292 {
1293  V_VT(this) = VT_BSTR;
1294  V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
1295 }
1296 
1297 // Construct a VT_DISPATCH VARIANT from an IDispatch*
1298 //
1299 inline _variant_t::_variant_t(IDispatch* pSrc, bool fAddRef) throw()
1300 {
1301  V_VT(this) = VT_DISPATCH;
1302  V_DISPATCH(this) = pSrc;
1303 
1304  // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
1305  // false indicates we're taking ownership
1306  //
1307  if (fAddRef) {
1308  if (V_DISPATCH(this) != NULL) {
1309  V_DISPATCH(this)->AddRef();
1310  }
1311  }
1312 }
1313 
1314 // Construct a VT_BOOL VARIANT from a bool
1315 //
1316 inline _variant_t::_variant_t(bool boolSrc) throw()
1317 {
1318  V_VT(this) = VT_BOOL;
1319  V_BOOL(this) = (boolSrc ? VARIANT_TRUE : VARIANT_FALSE);
1320 }
1321 
1322 // Construct a VT_UNKNOWN VARIANT from an IUnknown*
1323 //
1324 inline _variant_t::_variant_t(IUnknown* pSrc, bool fAddRef) throw()
1325 {
1326  V_VT(this) = VT_UNKNOWN;
1327  V_UNKNOWN(this) = pSrc;
1328 
1329  // Need the AddRef() as VariantClear() calls Release(), unless fAddRef
1330  // false indicates we're taking ownership
1331  //
1332  if (fAddRef) {
1333  if (V_UNKNOWN(this) != NULL) {
1334  V_UNKNOWN(this)->AddRef();
1335  }
1336  }
1337 }
1338 
1339 // Construct a VT_DECIMAL VARIANT from a DECIMAL
1340 //
1341 inline _variant_t::_variant_t(const DECIMAL& decSrc) throw()
1342 {
1343  // Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
1344  //
1345  V_DECIMAL(this) = decSrc;
1346  V_VT(this) = VT_DECIMAL;
1347 }
1348 
1349 // Construct a VT_UI1 VARIANT from a BYTE (unsigned char)
1350 //
1351 inline _variant_t::_variant_t(BYTE bSrc) throw()
1352 {
1353  V_VT(this) = VT_UI1;
1354  V_UI1(this) = bSrc;
1355 }
1356 
1357 // Construct a VT_I1 VARIANT from a char
1358 //
1359 inline _variant_t::_variant_t(char cSrc) throw()
1360 {
1361  V_VT(this) = VT_I1;
1362  V_I1(this) = cSrc;
1363 }
1364 
1365 // Construct a VT_UI2 VARIANT from a unsigned short
1366 //
1367 inline _variant_t::_variant_t(unsigned short usSrc) throw()
1368 {
1369  V_VT(this) = VT_UI2;
1370  V_UI2(this) = usSrc;
1371 }
1372 
1373 // Construct a VT_UI4 VARIANT from a unsigned long
1374 //
1375 inline _variant_t::_variant_t(unsigned long ulSrc) throw()
1376 {
1377  V_VT(this) = VT_UI4;
1378  V_UI4(this) = ulSrc;
1379 }
1380 
1381 // Construct a VT_INT VARIANT from a int
1382 //
1383 inline _variant_t::_variant_t(int iSrc) throw()
1384 {
1385  V_VT(this) = VT_INT;
1386  V_INT(this) = iSrc;
1387 }
1388 
1389 // Construct a VT_UINT VARIANT from a unsigned int
1390 //
1391 inline _variant_t::_variant_t(unsigned int uiSrc) throw()
1392 {
1393  V_VT(this) = VT_UINT;
1394  V_UINT(this) = uiSrc;
1395 }
1396 
1397 #if (_WIN32_WINNT >= 0x0501)
1398 // Construct a VT_I8 VARIANT from a __int64
1399 //
1400 inline _variant_t::_variant_t(__int64 i8Src) throw()
1401 {
1402  V_VT(this) = VT_I8;
1403  V_I8(this) = i8Src;
1404 }
1405 
1406 // Construct a VT_UI8 VARIANT from a unsigned __int64
1407 //
1408 inline _variant_t::_variant_t(unsigned __int64 ui8Src) throw()
1409 {
1410  V_VT(this) = VT_UI8;
1411  V_UI8(this) = ui8Src;
1412 }
1413 #endif
1414 
1416 //
1417 // Extractors
1418 //
1420 
1421 // Extracts a VT_I2 into a short
1422 //
1423 inline _variant_t::operator short() const
1424 {
1425  if (V_VT(this) == VT_I2) {
1426  return V_I2(this);
1427  }
1428 
1429  _variant_t varDest;
1430  varDest.ChangeType(VT_I2, this);
1431 
1432  return V_I2(&varDest);
1433 }
1434 
1435 // Extracts a VT_I4 into a long
1436 //
1437 inline _variant_t::operator long() const
1438 {
1439  if (V_VT(this) == VT_I4) {
1440  return V_I4(this);
1441  }
1442 
1443  _variant_t varDest;
1444  varDest.ChangeType(VT_I4, this);
1445 
1446  return V_I4(&varDest);
1447 }
1448 
1449 // Extracts a VT_R4 into a float
1450 //
1451 inline _variant_t::operator float() const
1452 {
1453  if (V_VT(this) == VT_R4) {
1454  return V_R4(this);
1455  }
1456 
1457  _variant_t varDest;
1458  varDest.ChangeType(VT_R4, this);
1459 
1460  return V_R4(&varDest);
1461 }
1462 
1463 // Extracts a VT_R8 into a double
1464 //
1465 inline _variant_t::operator double() const
1466 {
1467  if (V_VT(this) == VT_R8) {
1468  return V_R8(this);
1469  }
1470 
1471  _variant_t varDest;
1472  varDest.ChangeType(VT_R8, this);
1473 
1474  return V_R8(&varDest);
1475 }
1476 
1477 // Extracts a VT_CY into a CY
1478 //
1479 inline _variant_t::operator CY() const
1480 {
1481  if (V_VT(this) == VT_CY) {
1482  return V_CY(this);
1483  }
1484 
1485  _variant_t varDest;
1486  varDest.ChangeType(VT_CY, this);
1487 
1488  return V_CY(&varDest);
1489 }
1490 
1491 // Extracts a VT_BSTR into a _bstr_t
1492 //
1493 inline _variant_t::operator _bstr_t() const
1494 {
1495  if (V_VT(this) == VT_BSTR) {
1496  return V_BSTR(this);
1497  }
1498 
1499  _variant_t varDest;
1500  varDest.ChangeType(VT_BSTR, this);
1501 
1502  return V_BSTR(&varDest);
1503 }
1504 
1505 // Extracts a VT_DISPATCH into an IDispatch*
1506 //
1507 inline _variant_t::operator IDispatch*() const
1508 {
1509  if (V_VT(this) == VT_DISPATCH) {
1510  if (V_DISPATCH(this) != NULL) {
1511  V_DISPATCH(this)->AddRef();
1512  }
1513  return V_DISPATCH(this);
1514  }
1515 
1516  _variant_t varDest;
1517  varDest.ChangeType(VT_DISPATCH, this);
1518 
1519  if (V_DISPATCH(&varDest) != NULL) {
1520  V_DISPATCH(&varDest)->AddRef();
1521  }
1522 
1523  return V_DISPATCH(&varDest);
1524 }
1525 
1526 // Extract a VT_BOOL into a bool
1527 //
1528 inline _variant_t::operator bool() const
1529 {
1530  if (V_VT(this) == VT_BOOL) {
1531  return V_BOOL(this) ? true : false;
1532  }
1533 
1534  _variant_t varDest;
1535  varDest.ChangeType(VT_BOOL, this);
1536 
1537  return (V_BOOL(&varDest) == VARIANT_TRUE) ? true : false;
1538 }
1539 
1540 // Extracts a VT_UNKNOWN into an IUnknown*
1541 //
1542 inline _variant_t::operator IUnknown*() const
1543 {
1544  if (V_VT(this) == VT_UNKNOWN) {
1545  if (V_UNKNOWN(this) != NULL) {
1546  V_UNKNOWN(this)->AddRef();
1547  }
1548  return V_UNKNOWN(this);
1549  }
1550 
1551  _variant_t varDest;
1552  varDest.ChangeType(VT_UNKNOWN, this);
1553 
1554  if (V_UNKNOWN(&varDest) != NULL) {
1555  V_UNKNOWN(&varDest)->AddRef();
1556  }
1557 
1558  return V_UNKNOWN(&varDest);
1559 }
1560 
1561 // Extracts a VT_DECIMAL into a DECIMAL
1562 //
1563 inline _variant_t::operator DECIMAL() const
1564 {
1565  if (V_VT(this) == VT_DECIMAL) {
1566  return V_DECIMAL(this);
1567  }
1568 
1569  _variant_t varDest;
1570  varDest.ChangeType(VT_DECIMAL, this);
1571 
1572  return V_DECIMAL(&varDest);
1573 }
1574 
1575 // Extracts a VT_UI1 into a BYTE (unsigned char)
1576 //
1577 inline _variant_t::operator BYTE() const
1578 {
1579  if (V_VT(this) == VT_UI1) {
1580  return V_UI1(this);
1581  }
1582 
1583  _variant_t varDest;
1584  varDest.ChangeType(VT_UI1, this);
1585 
1586  return V_UI1(&varDest);
1587 }
1588 
1589 // Extract the physical VARIANT
1590 //
1591 inline _variant_t::operator VARIANT() const throw()
1592 {
1593  return *(VARIANT*) this;
1594 }
1595 
1596 // Extracts a VT_I1 into a char
1597 //
1598 inline _variant_t::operator char() const
1599 {
1600  if (V_VT(this) == VT_I1) {
1601  return V_I1(this);
1602  }
1603 
1604  _variant_t varDest;
1605  varDest.ChangeType(VT_I1, this);
1606 
1607  return V_I1(&varDest);
1608 }
1609 
1610 // Extracts a VT_UI2 into a unsigned short
1611 //
1612 inline _variant_t::operator unsigned short() const
1613 {
1614  if (V_VT(this) == VT_UI2) {
1615  return V_UI2(this);
1616  }
1617 
1618  _variant_t varDest;
1619  varDest.ChangeType(VT_UI2, this);
1620 
1621  return V_UI2(&varDest);
1622 }
1623 
1624 // Extracts a VT_UI4 into a unsigned long
1625 //
1626 inline _variant_t::operator unsigned long() const
1627 {
1628  if (V_VT(this) == VT_UI4) {
1629  return V_UI4(this);
1630  }
1631 
1632  _variant_t varDest;
1633  varDest.ChangeType(VT_UI4, this);
1634 
1635  return V_UI4(&varDest);
1636 }
1637 
1638 // Extracts a VT_INT into a int
1639 //
1640 inline _variant_t::operator int() const
1641 {
1642  if (V_VT(this) == VT_INT) {
1643  return V_INT(this);
1644  }
1645 
1646  _variant_t varDest;
1647  varDest.ChangeType(VT_INT, this);
1648 
1649  return V_INT(&varDest);
1650 }
1651 
1652 // Extracts a VT_UINT into a unsigned int
1653 //
1654 inline _variant_t::operator unsigned int() const
1655 {
1656  if (V_VT(this) == VT_UINT) {
1657  return V_UINT(this);
1658  }
1659 
1660  _variant_t varDest;
1661  varDest.ChangeType(VT_UINT, this);
1662 
1663  return V_UINT(&varDest);
1664 }
1665 
1666 #if (_WIN32_WINNT >= 0x0501)
1667 // Extracts a VT_I8 into a __int64
1668 //
1669 inline _variant_t::operator __int64() const
1670 {
1671  if (V_VT(this) == VT_I8) {
1672  return V_I8(this);
1673  }
1674 
1675  _variant_t varDest;
1676  varDest.ChangeType(VT_I8, this);
1677 
1678  return V_I8(&varDest);
1679 }
1680 
1681 // Extracts a VT_UI8 into a unsigned __int64
1682 //
1683 inline _variant_t::operator unsigned __int64() const
1684 {
1685  if (V_VT(this) == VT_UI8) {
1686  return V_UI8(this);
1687  }
1688 
1689  _variant_t varDest;
1690  varDest.ChangeType(VT_UI8, this);
1691 
1692  return V_UI8(&varDest);
1693 }
1694 #endif
1695 
1697 //
1698 // Assignment operations
1699 //
1701 
1702 // Assign a const VARIANT& (::VariantCopy handles everything)
1703 //
1704 inline _variant_t& _variant_t::operator=(const VARIANT& varSrc)
1705 {
1706  _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(&varSrc)));
1707 
1708  return *this;
1709 }
1710 
1711 // Assign a const VARIANT* (::VariantCopy handles everything)
1712 //
1713 inline _variant_t& _variant_t::operator=(const VARIANT* pSrc)
1714 {
1715  if (pSrc == NULL) {
1716  _com_issue_error(E_POINTER);
1717  }
1718  else {
1719  _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(pSrc)));
1720  }
1721 
1722  return *this;
1723 }
1724 
1725 // Assign a const _variant_t& (::VariantCopy handles everything)
1726 //
1728 {
1729  _com_util::CheckError(::VariantCopy(this, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc))));
1730 
1731  return *this;
1732 }
1733 
1734 // Assign a short creating either VT_I2 VARIANT or a
1735 // VT_BOOL VARIANT (VT_I2 is the default)
1736 //
1737 inline _variant_t& _variant_t::operator=(short sSrc)
1738 {
1739  if (V_VT(this) == VT_I2) {
1740  V_I2(this) = sSrc;
1741  }
1742  else if (V_VT(this) == VT_BOOL) {
1743  V_BOOL(this) = (sSrc ? VARIANT_TRUE : VARIANT_FALSE);
1744  }
1745  else {
1746  // Clear the VARIANT and create a VT_I2
1747  //
1748  Clear();
1749 
1750  V_VT(this) = VT_I2;
1751  V_I2(this) = sSrc;
1752  }
1753 
1754  return *this;
1755 }
1756 
1757 // Assign a long creating either VT_I4 VARIANT, a VT_ERROR VARIANT
1758 // or a VT_BOOL VARIANT (VT_I4 is the default)
1759 //
1761 {
1762  if (V_VT(this) == VT_I4) {
1763  V_I4(this) = lSrc;
1764  }
1765  else if (V_VT(this) == VT_ERROR) {
1766  V_ERROR(this) = lSrc;
1767  }
1768  else if (V_VT(this) == VT_BOOL) {
1769  V_BOOL(this) = (lSrc ? VARIANT_TRUE : VARIANT_FALSE);
1770  }
1771  else {
1772  // Clear the VARIANT and create a VT_I4
1773  //
1774  Clear();
1775 
1776  V_VT(this) = VT_I4;
1777  V_I4(this) = lSrc;
1778  }
1779 
1780  return *this;
1781 }
1782 
1783 // Assign a float creating a VT_R4 VARIANT
1784 //
1785 inline _variant_t& _variant_t::operator=(float fltSrc)
1786 {
1787  if (V_VT(this) != VT_R4) {
1788  // Clear the VARIANT and create a VT_R4
1789  //
1790  Clear();
1791 
1792  V_VT(this) = VT_R4;
1793  }
1794 
1795  V_R4(this) = fltSrc;
1796 
1797  return *this;
1798 }
1799 
1800 // Assign a double creating either a VT_R8 VARIANT, or a VT_DATE
1801 // VARIANT (VT_R8 is the default)
1802 //
1803 inline _variant_t& _variant_t::operator=(double dblSrc)
1804 {
1805  if (V_VT(this) == VT_R8) {
1806  V_R8(this) = dblSrc;
1807  }
1808  else if(V_VT(this) == VT_DATE) {
1809  V_DATE(this) = dblSrc;
1810  }
1811  else {
1812  // Clear the VARIANT and create a VT_R8
1813  //
1814  Clear();
1815 
1816  V_VT(this) = VT_R8;
1817  V_R8(this) = dblSrc;
1818  }
1819 
1820  return *this;
1821 }
1822 
1823 // Assign a CY creating a VT_CY VARIANT
1824 //
1825 inline _variant_t& _variant_t::operator=(const CY& cySrc)
1826 {
1827  if (V_VT(this) != VT_CY) {
1828  // Clear the VARIANT and create a VT_CY
1829  //
1830  Clear();
1831 
1832  V_VT(this) = VT_CY;
1833  }
1834 
1835  V_CY(this) = cySrc;
1836 
1837  return *this;
1838 }
1839 
1840 // Assign a const _bstr_t& creating a VT_BSTR VARIANT
1841 //
1842 inline _variant_t& _variant_t::operator=(const _bstr_t& bstrSrc)
1843 {
1844  _COM_ASSERT(V_VT(this) != VT_BSTR || (BSTR) bstrSrc == NULL || V_BSTR(this) != (BSTR) bstrSrc);
1845 
1846  // Clear the VARIANT (This will SysFreeString() any previous occupant)
1847  //
1848  Clear();
1849 
1850  V_VT(this) = VT_BSTR;
1851 
1852  if (!bstrSrc) {
1853  V_BSTR(this) = NULL;
1854  }
1855  else {
1856  BSTR bstr = static_cast<wchar_t*>(bstrSrc);
1857  V_BSTR(this) = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
1858  ::SysStringByteLen(bstr));
1859 
1860  if (V_BSTR(this) == NULL) {
1861  _com_issue_error(E_OUTOFMEMORY);
1862  }
1863  }
1864 
1865  return *this;
1866 }
1867 
1868 // Assign a const wchar_t* creating a VT_BSTR VARIANT
1869 //
1870 inline _variant_t& _variant_t::operator=(const wchar_t* pSrc)
1871 {
1872  _COM_ASSERT(V_VT(this) != VT_BSTR || pSrc == NULL || V_BSTR(this) != pSrc);
1873 
1874  // Clear the VARIANT (This will SysFreeString() any previous occupant)
1875  //
1876  Clear();
1877 
1878  V_VT(this) = VT_BSTR;
1879 
1880  if (pSrc == NULL) {
1881  V_BSTR(this) = NULL;
1882  }
1883  else {
1884  V_BSTR(this) = ::SysAllocString(pSrc);
1885 
1886  if (V_BSTR(this) == NULL) {
1887  _com_issue_error(E_OUTOFMEMORY);
1888  }
1889  }
1890 
1891  return *this;
1892 }
1893 
1894 // Assign a const char* creating a VT_BSTR VARIANT
1895 //
1896 inline _variant_t& _variant_t::operator=(const char* pSrc)
1897 {
1898  _COM_ASSERT(V_VT(this) != (VT_I1 | VT_BYREF) || pSrc == NULL || V_I1REF(this) != pSrc);
1899 
1900  // Clear the VARIANT (This will SysFreeString() any previous occupant)
1901  //
1902  Clear();
1903 
1904  V_VT(this) = VT_BSTR;
1905  V_BSTR(this) = _com_util::ConvertStringToBSTR(pSrc);
1906 
1907  return *this;
1908 }
1909 
1910 // Assign an IDispatch* creating a VT_DISPATCH VARIANT
1911 //
1912 inline _variant_t& _variant_t::operator=(IDispatch* pSrc)
1913 {
1914  _COM_ASSERT(V_VT(this) != VT_DISPATCH || pSrc == 0 || V_DISPATCH(this) != pSrc);
1915 
1916  // Clear the VARIANT (This will Release() any previous occupant)
1917  //
1918  Clear();
1919 
1920  V_VT(this) = VT_DISPATCH;
1921  V_DISPATCH(this) = pSrc;
1922 
1923  if (V_DISPATCH(this) != NULL) {
1924  // Need the AddRef() as VariantClear() calls Release()
1925  //
1926  V_DISPATCH(this)->AddRef();
1927  }
1928 
1929  return *this;
1930 }
1931 
1932 // Assign a bool creating a VT_BOOL VARIANT
1933 //
1934 inline _variant_t& _variant_t::operator=(bool boolSrc)
1935 {
1936  if (V_VT(this) != VT_BOOL) {
1937  // Clear the VARIANT and create a VT_BOOL
1938  //
1939  Clear();
1940 
1941  V_VT(this) = VT_BOOL;
1942  }
1943 
1944  V_BOOL(this) = (boolSrc ? VARIANT_TRUE : VARIANT_FALSE);
1945 
1946  return *this;
1947 }
1948 
1949 // Assign an IUnknown* creating a VT_UNKNOWN VARIANT
1950 //
1951 inline _variant_t& _variant_t::operator=(IUnknown* pSrc)
1952 {
1953  _COM_ASSERT(V_VT(this) != VT_UNKNOWN || pSrc == NULL || V_UNKNOWN(this) != pSrc);
1954 
1955  // Clear VARIANT (This will Release() any previous occupant)
1956  //
1957  Clear();
1958 
1959  V_VT(this) = VT_UNKNOWN;
1960  V_UNKNOWN(this) = pSrc;
1961 
1962  if (V_UNKNOWN(this) != NULL) {
1963  // Need the AddRef() as VariantClear() calls Release()
1964  //
1965  V_UNKNOWN(this)->AddRef();
1966  }
1967 
1968  return *this;
1969 }
1970 
1971 // Assign a DECIMAL creating a VT_DECIMAL VARIANT
1972 //
1973 inline _variant_t& _variant_t::operator=(const DECIMAL& decSrc)
1974 {
1975  if (V_VT(this) != VT_DECIMAL) {
1976  // Clear the VARIANT
1977  //
1978  Clear();
1979  }
1980 
1981  // Order is important here! Setting V_DECIMAL wipes out the entire VARIANT
1982  V_DECIMAL(this) = decSrc;
1983  V_VT(this) = VT_DECIMAL;
1984 
1985  return *this;
1986 }
1987 
1988 // Assign a BTYE (unsigned char) creating a VT_UI1 VARIANT
1989 //
1991 {
1992  if (V_VT(this) != VT_UI1) {
1993  // Clear the VARIANT and create a VT_UI1
1994  //
1995  Clear();
1996 
1997  V_VT(this) = VT_UI1;
1998  }
1999 
2000  V_UI1(this) = bSrc;
2001 
2002  return *this;
2003 }
2004 
2005 // Assign a char creating a VT_I1 VARIANT
2006 //
2008 {
2009  if (V_VT(this) != VT_I1) {
2010  // Clear the VARIANT and create a VT_I1
2011  //
2012  Clear();
2013 
2014  V_VT(this) = VT_I1;
2015  }
2016 
2017  V_I1(this) = cSrc;
2018 
2019  return *this;
2020 }
2021 
2022 // Assign a char creating a VT_UI2 VARIANT
2023 //
2024 inline _variant_t& _variant_t::operator=(unsigned short usSrc)
2025 {
2026  if (V_VT(this) != VT_UI2) {
2027  // Clear the VARIANT and create a VT_UI2
2028  //
2029  Clear();
2030 
2031  V_VT(this) = VT_UI2;
2032  }
2033 
2034  V_UI2(this) = usSrc;
2035 
2036  return *this;
2037 }
2038 
2039 // Assign a char creating a VT_UI4 VARIANT
2040 //
2041 inline _variant_t& _variant_t::operator=(unsigned long ulSrc)
2042 {
2043  if (V_VT(this) != VT_UI4) {
2044  // Clear the VARIANT and create a VT_UI4
2045  //
2046  Clear();
2047 
2048  V_VT(this) = VT_UI4;
2049  }
2050 
2051  V_UI4(this) = ulSrc;
2052 
2053  return *this;
2054 }
2055 
2056 // Assign a char creating a VT_INT VARIANT
2057 //
2059 {
2060  if (V_VT(this) != VT_INT) {
2061  // Clear the VARIANT and create a VT_INT
2062  //
2063  Clear();
2064 
2065  V_VT(this) = VT_INT;
2066  }
2067 
2068  V_INT(this) = iSrc;
2069 
2070  return *this;
2071 }
2072 
2073 // Assign a char creating a VT_UINT VARIANT
2074 //
2075 inline _variant_t& _variant_t::operator=(unsigned int uiSrc)
2076 {
2077  if (V_VT(this) != VT_UINT) {
2078  // Clear the VARIANT and create a VT_UINT
2079  //
2080  Clear();
2081 
2082  V_VT(this) = VT_UINT;
2083  }
2084 
2085  V_UINT(this) = uiSrc;
2086 
2087  return *this;
2088 }
2089 
2090 #if (_WIN32_WINNT >= 0x0501)
2091 // Assign a char creating a VT_I8 VARIANT
2092 //
2093 inline _variant_t& _variant_t::operator=(__int64 i8Src)
2094 {
2095  if (V_VT(this) != VT_I8) {
2096  // Clear the VARIANT and create a VT_I8
2097  //
2098  Clear();
2099 
2100  V_VT(this) = VT_I8;
2101  }
2102 
2103  V_I8(this) = i8Src;
2104 
2105  return *this;
2106 }
2107 
2108 // Assign a char creating a VT_UI8 VARIANT
2109 //
2110 inline _variant_t& _variant_t::operator=(unsigned __int64 ui8Src)
2111 {
2112  if (V_VT(this) != VT_UI8) {
2113  // Clear the VARIANT and create a VT_UI8
2114  //
2115  Clear();
2116 
2117  V_VT(this) = VT_UI8;
2118  }
2119 
2120  V_UI8(this) = ui8Src;
2121 
2122  return *this;
2123 }
2124 #endif
2125 
2127 //
2128 // Comparison operations
2129 //
2131 
2132 // Compare a _variant_t against a const VARIANT& for equality
2133 //
2134 inline bool _variant_t::operator==(const VARIANT& varSrc) const throw()
2135 {
2136  return *this == &varSrc;
2137 }
2138 
2139 #pragma warning(push)
2140 #pragma warning(disable: 4702) // unreachable code
2141 
2142 // Compare a _variant_t against a const VARIANT* for equality
2143 //
2144 inline bool _variant_t::operator==(const VARIANT* pSrc) const throw()
2145 {
2146  if (pSrc == NULL) {
2147  return false;
2148  }
2149 
2150  if (this == pSrc) {
2151  return true;
2152  }
2153 
2154  //
2155  // Variants not equal if types don't match
2156  //
2157  if (V_VT(this) != V_VT(pSrc)) {
2158  return false;
2159  }
2160 
2161  //
2162  // Check type specific values
2163  //
2164  switch (V_VT(this)) {
2165  case VT_EMPTY:
2166  case VT_NULL:
2167  return true;
2168 
2169  case VT_I2:
2170  return V_I2(this) == V_I2(pSrc);
2171 
2172  case VT_I4:
2173  return V_I4(this) == V_I4(pSrc);
2174 
2175  case VT_R4:
2176  return V_R4(this) == V_R4(pSrc);
2177 
2178  case VT_R8:
2179  return V_R8(this) == V_R8(pSrc);
2180 
2181  case VT_CY:
2182  return memcmp(&(V_CY(this)), &(V_CY(pSrc)), sizeof(CY)) == 0;
2183 
2184  case VT_DATE:
2185  return V_DATE(this) == V_DATE(pSrc);
2186 
2187  case VT_BSTR:
2188  return (::SysStringByteLen(V_BSTR(this)) == ::SysStringByteLen(V_BSTR(pSrc))) &&
2189  (memcmp(V_BSTR(this), V_BSTR(pSrc), ::SysStringByteLen(V_BSTR(this))) == 0);
2190 
2191  case VT_DISPATCH:
2192  return V_DISPATCH(this) == V_DISPATCH(pSrc);
2193 
2194  case VT_ERROR:
2195  return V_ERROR(this) == V_ERROR(pSrc);
2196 
2197  case VT_BOOL:
2198  return V_BOOL(this) == V_BOOL(pSrc);
2199 
2200  case VT_UNKNOWN:
2201  return V_UNKNOWN(this) == V_UNKNOWN(pSrc);
2202 
2203  case VT_DECIMAL:
2204  return memcmp(&(V_DECIMAL(this)), &(V_DECIMAL(pSrc)), sizeof(DECIMAL)) == 0;
2205 
2206  case VT_UI1:
2207  return V_UI1(this) == V_UI1(pSrc);
2208 
2209  case VT_I1:
2210  return V_I1(this) == V_I1(pSrc);
2211 
2212  case VT_UI2:
2213  return V_UI2(this) == V_UI2(pSrc);
2214 
2215  case VT_UI4:
2216  return V_UI4(this) == V_UI4(pSrc);
2217 
2218  case VT_INT:
2219  return V_INT(this) == V_INT(pSrc);
2220 
2221  case VT_UINT:
2222  return V_UINT(this) == V_UINT(pSrc);
2223 
2224 #if (_WIN32_WINNT >= 0x0501)
2225  case VT_I8:
2226  return V_I8(this) == V_I8(pSrc);
2227 
2228  case VT_UI8:
2229  return V_UI8(this) == V_UI8(pSrc);
2230 #endif
2231 
2232  default:
2233  _com_issue_error(E_INVALIDARG);
2234  // fall through
2235  }
2236 
2237  return false;
2238 }
2239 
2240 #pragma warning(pop)
2241 
2242 // Compare a _variant_t against a const VARIANT& for in-equality
2243 //
2244 inline bool _variant_t::operator!=(const VARIANT& varSrc) const throw()
2245 {
2246  return !(*this == &varSrc);
2247 }
2248 
2249 // Compare a _variant_t against a const VARIANT* for in-equality
2250 //
2251 inline bool _variant_t::operator!=(const VARIANT* pSrc) const throw()
2252 {
2253  return !(*this == pSrc);
2254 }
2255 
2257 //
2258 // Low-level operations
2259 //
2261 
2262 // Clear the _variant_t
2263 //
2264 inline void _variant_t::Clear()
2265 {
2266  _com_util::CheckError(::VariantClear(this));
2267 }
2268 
2269 inline void _variant_t::Attach(VARIANT& varSrc)
2270 {
2271  //
2272  // Free up previous VARIANT
2273  //
2274  Clear();
2275 
2276  //
2277  // Give control of data to _variant_t
2278  //
2279  _COM_MEMCPY_S(this, sizeof(varSrc), &varSrc, sizeof(varSrc));
2280  V_VT(&varSrc) = VT_EMPTY;
2281 }
2282 
2283 inline VARIANT _variant_t::Detach() throw()
2284 {
2285  VARIANT varResult = *this;
2286  V_VT(this) = VT_EMPTY;
2287 
2288  return varResult;
2289 }
2290 
2291 inline VARIANT& _variant_t::GetVARIANT() throw()
2292 {
2293  return *(VARIANT*) this;
2294 }
2295 
2296 inline VARIANT* _variant_t::GetAddress()
2297 {
2298  Clear();
2299  return (VARIANT*) this;
2300 }
2301 
2302 // Change the type and contents of this _variant_t to the type vartype and
2303 // contents of pSrc
2304 //
2305 inline void _variant_t::ChangeType(VARTYPE vartype, const _variant_t* pSrc)
2306 {
2307  //
2308  // If pDest is NULL, convert type in place
2309  //
2310  if (pSrc == NULL) {
2311  pSrc = this;
2312  }
2313 
2314  if ((this != pSrc) || (vartype != V_VT(this))) {
2315  _com_util::CheckError(::VariantChangeType(static_cast<VARIANT*>(this),
2316  const_cast<VARIANT*>(static_cast<const VARIANT*>(pSrc)),
2317  0, vartype));
2318  }
2319 }
2320 
2321 inline void _variant_t::SetString(const char* pSrc)
2322 {
2323  operator=(pSrc);
2324 }
2325 
2327 //
2328 // Destructor
2329 //
2331 
2332 inline _variant_t::~_variant_t() throw()
2333 {
2334  ::VariantClear(this);
2335 }
2336 
2338 //
2339 // Mutually-dependent definitions
2340 //
2342 
2343 // Construct a _bstr_t from a const _variant_t&
2344 //
2345 inline _bstr_t::_bstr_t(const _variant_t &var)
2346  : m_Data(NULL)
2347 {
2348  if (V_VT(&var) == VT_BSTR) {
2349  *this = V_BSTR(&var);
2350  return;
2351  }
2352 
2353  _variant_t varDest;
2354  varDest.ChangeType(VT_BSTR, &var);
2355 
2356  *this = V_BSTR(&varDest);
2357 }
2358 
2359 // Assign a const _variant_t& to a _bstr_t
2360 //
2362 {
2363  if (V_VT(&var) == VT_BSTR) {
2364  *this = V_BSTR(&var);
2365  return *this;
2366  }
2367 
2368  _variant_t varDest;
2369  varDest.ChangeType(VT_BSTR, &var);
2370 
2371  *this = V_BSTR(&varDest);
2372 
2373  return *this;
2374 }
2375 
2376 extern _variant_t vtMissing;
2377 
2378 #ifndef _USE_RAW
2379 #define bstr_t _bstr_t
2380 #define variant_t _variant_t
2381 #endif
2382 
2383 #pragma pop_macro("new")
2384 
2385 #pragma warning(pop)
2386 
2387 #endif // _INC_COMUTIL
unsigned long RefCount() const
Definition: comutil.h:781
BSTR __stdcall ConvertStringToBSTR(const char *pSrc)
void _Free()
Definition: comutil.h:928
bool operator==(const VARIANT &varSrc) const
Definition: comutil.h:2134
Definition: comutil.h:144
Definition: comdef.h:247
void Clear()
Definition: comutil.h:2264
#define NULL
Definition: vcruntime.h:236
int _Compare(const _bstr_t &str) const
Definition: comutil.h:645
int Compare(const Data_t &str) const
Definition: comutil.h:876
void ChangeType(VARTYPE vartype, const _variant_t *pSrc=NULL)
Definition: comutil.h:2305
#define S_OK
Definition: comutil.h:62
void Assign(BSTR s)
Definition: comutil.h:848
unsigned long Release()
Definition: comutil.h:771
bool operator>=(const _bstr_t &str) const
Definition: comutil.h:532
bool operator!=(const _bstr_t &str) const
Definition: comutil.h:512
BSTR * GetAddress()
Definition: comutil.h:589
_Check_return_ int __cdecl memcmp(_In_reads_bytes_(_Size) void const *_Buf1, _In_reads_bytes_(_Size) void const *_Buf2, _In_ size_t _Size)
_variant_t & operator=(const VARIANT &varSrc)
Definition: comutil.h:1704
~Data_t()
Definition: comutil.h:921
BSTR Detach()
Definition: comutil.h:609
void Attach(BSTR s)
Definition: comutil.h:597
#define INTSAFE_UINT_MAX
Definition: comutil.h:68
void _Free()
Definition: comutil.h:635
void __declspec(noreturn) __stdcall _com_issue_error(HRESULT)
Cancels the currently executing task. This function can be called from within the body of a task to a...
Definition: ppltasks.h:131
Definition: comutil.h:1002
~_bstr_t()
Definition: comutil.h:341
VARIANT Detach()
Definition: comutil.h:2283
unsigned int
Definition: vccorlib.h:2468
unsigned long AddRef()
Definition: comutil.h:765
Data_t * m_Data
Definition: comutil.h:277
_variant_t vtMissing
unsigned long m_RefCount
Definition: comutil.h:257
_variant_t()
Definition: comutil.h:1128
BSTR copy(bool fCopy=true) const
Definition: comutil.h:545
Definition: comutil.h:93
unsigned int Length() const
Definition: comutil.h:869
char * m_str
Definition: comutil.h:256
_BidIt1 _Compare(_BidIt1 _Begin1, _BidIt1 _End1, _BidIt2 _Begin2, _BidIt2 _End2, const _RxTraits &_Traits, regex_constants::syntax_option_type _Sflags)
Definition: regex:4355
_bstr_t & operator+=(const _bstr_t &s)
Definition: comutil.h:412
void __stdcall _com_issue_error(HRESULT)
BSTR & GetBSTR()
Definition: comutil.h:576
#define FAILED(hr)
Definition: comutil.h:71
const char * GetString() const
Definition: comutil.h:822
_bstr_t operator+(const char *s1, const _bstr_t &s2)
Definition: comutil.h:444
#define bool
Definition: stdbool.h:15
const wchar_t * GetWString() const
Definition: comutil.h:812
_bstr_t()
Definition: comutil.h:295
Definition: comutil.h:218
bool operator!() const
Definition: comutil.h:502
static HRESULT UIntAdd(UINT uAugend, UINT uAddend, UINT *puResult)
Definition: comutil.h:100
_bstr_t & operator=(const _bstr_t &s)
Definition: comutil.h:354
bool operator>(const _bstr_t &str) const
Definition: comutil.h:522
BSTR Copy() const
Definition: comutil.h:833
void SetString(const char *pSrc)
Definition: comutil.h:2321
VARIANT * GetAddress()
Definition: comutil.h:2296
#define _COM_MEMCPY_S(dest, destsize, src, count)
Definition: comutil.h:48
bool operator<(const _bstr_t &str) const
Definition: comutil.h:517
_bstr_t operator+(const _bstr_t &s) const
Definition: comutil.h:428
void Assign(BSTR s)
Definition: comutil.h:559
VARIANT & GetVARIANT()
Definition: comutil.h:2291
void Attach(BSTR s)
Definition: comutil.h:858
void _AddRef()
Definition: comutil.h:626
bool operator!=(const VARIANT &varSrc) const
Definition: comutil.h:2244
void Attach(VARIANT &varSrc)
Definition: comutil.h:2269
~_variant_t()
Definition: comutil.h:2332
#define _COM_ASSERT(x)
Definition: comutil.h:26
static HRESULT UIntMult(UINT uMultiplicand, UINT uMultiplier, UINT *puResult)
Definition: comutil.h:110
void CheckError(HRESULT hr)
Definition: comutil.h:94
BSTR m_wstr
Definition: comutil.h:255
bool operator==(const _bstr_t &str) const
Definition: comutil.h:507
bool operator<=(const _bstr_t &str) const
Definition: comutil.h:527
char *__stdcall ConvertBSTRToString(BSTR pSrc)
unsigned int length() const
Definition: comutil.h:552
#define INTSAFE_E_ARITHMETIC_OVERFLOW
Definition: comutil.h:65