Leaked source code of windows server 2003
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.

190 lines
3.0 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. wsbvar.cpp
  5. Abstract:
  6. This class is a wrapper for the VARIANT structure, providing
  7. conversion and cleanup automatically. Current supported types
  8. for conversion are OLECHAR * (BSTR), IUnknown / IDispatch, and
  9. GUID. GUIDs are represented internally as strings.
  10. Author:
  11. Rohde Wakefield [rohde] 21-Jan-1997
  12. Revision History:
  13. --*/
  14. #include "stdafx.h"
  15. #include "wsb.h"
  16. //
  17. // OLECHAR (wide-character) methods
  18. //
  19. CWsbVariant::CWsbVariant ( const OLECHAR * string )
  20. {
  21. Init ( );
  22. if ( 0 != ( bstrVal = WsbAllocString ( string ) ) )
  23. vt = VT_BSTR;
  24. }
  25. CWsbVariant & CWsbVariant::operator = ( const OLECHAR * string )
  26. {
  27. Clear ( );
  28. if ( 0 != ( bstrVal = WsbAllocString ( string ) ) )
  29. vt = VT_BSTR;
  30. return ( *this );
  31. }
  32. CWsbVariant::operator OLECHAR * ( )
  33. {
  34. if ( VT_BSTR != vt )
  35. VariantChangeType ( this, this, 0, VT_BSTR );
  36. return ( VT_BSTR == vt ) ? bstrVal : 0;
  37. }
  38. //
  39. // COM Interface methods
  40. //
  41. CWsbVariant::CWsbVariant ( IUnknown * pUnk )
  42. {
  43. Init ( );
  44. if ( 0 != pUnk ) {
  45. punkVal = pUnk;
  46. punkVal->AddRef ( );
  47. vt = VT_UNKNOWN;
  48. }
  49. }
  50. CWsbVariant::CWsbVariant ( IDispatch * pDisp )
  51. {
  52. Init ( );
  53. if ( 0 != pDisp ) {
  54. pdispVal = pDisp;
  55. pdispVal->AddRef ( );
  56. vt = VT_DISPATCH;
  57. }
  58. }
  59. CWsbVariant::operator IUnknown * ( )
  60. {
  61. //
  62. // Ok to return IDispatch as IUnknown since it
  63. // derives from IUnknown
  64. //
  65. if ( IsInterface ( ) )
  66. return punkVal;
  67. return 0;
  68. }
  69. CWsbVariant::operator IDispatch * ( )
  70. {
  71. if ( IsDispatch ( ) ) {
  72. return pdispVal;
  73. }
  74. if ( IsInterface ( ) ) {
  75. IDispatch * pDisp;
  76. if ( SUCCEEDED ( punkVal->QueryInterface ( IID_IDispatch, (void**)&pDisp ) ) ) {
  77. punkVal->Release ( );
  78. pdispVal = pDisp;
  79. vt = VT_DISPATCH;
  80. return ( pdispVal );
  81. }
  82. }
  83. return 0;
  84. }
  85. CWsbVariant & CWsbVariant::operator = ( IUnknown * pUnk )
  86. {
  87. Clear ( );
  88. vt = VT_UNKNOWN;
  89. punkVal = pUnk;
  90. punkVal->AddRef ( );
  91. return ( *this );
  92. }
  93. CWsbVariant & CWsbVariant::operator = ( IDispatch * pDisp )
  94. {
  95. Clear ( );
  96. vt = VT_DISPATCH;
  97. pdispVal = pDisp;
  98. pdispVal->AddRef ( );
  99. return ( *this );
  100. }
  101. //
  102. // Methods to work with GUIDs
  103. //
  104. CWsbVariant::CWsbVariant ( REFGUID rguid )
  105. {
  106. Init ( );
  107. *this = rguid;
  108. }
  109. CWsbVariant & CWsbVariant::operator = ( REFGUID rguid )
  110. {
  111. Clear ( );
  112. if ( 0 != ( bstrVal = WsbAllocStringLen( 0, WSB_GUID_STRING_SIZE ) ) ) {
  113. if ( SUCCEEDED ( WsbStringFromGuid ( rguid, bstrVal ) ) ) {
  114. vt = VT_BSTR;
  115. }
  116. }
  117. return ( *this );
  118. }
  119. CWsbVariant::operator GUID ()
  120. {
  121. GUID guid;
  122. WsbGuidFromString ( (const OLECHAR *)*this, &guid );
  123. return guid;
  124. }