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.

188 lines
5.9 KiB

  1. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  2. // CVARIANT.h
  3. #pragma once
  4. class CVARIANT;
  5. class CVARIANTError;
  6. class CVARIANTError
  7. {
  8. public:
  9. inline CVARIANTError()
  10. : m_hr(E_FAIL)
  11. {}
  12. inline CVARIANTError(HRESULT hr)
  13. : m_hr(hr)
  14. {}
  15. inline virtual ~CVARIANTError() {}
  16. inline HRESULT GetError()
  17. {
  18. return m_hr;
  19. }
  20. inline HRESULT GetWBEMError()
  21. {
  22. HRESULT hrOut = WBEM_S_NO_ERROR;
  23. switch(m_hr)
  24. {
  25. case DISP_E_ARRAYISLOCKED:
  26. {
  27. hrOut = WBEM_E_FAILED;
  28. break;
  29. }
  30. case DISP_E_BADVARTYPE:
  31. {
  32. hrOut = WBEM_E_INVALID_PARAMETER;
  33. break;
  34. }
  35. case E_OUTOFMEMORY:
  36. {
  37. hrOut = WBEM_E_OUT_OF_MEMORY;
  38. break;
  39. }
  40. case E_INVALIDARG:
  41. {
  42. hrOut = WBEM_E_INVALID_PARAMETER;
  43. break;
  44. }
  45. }
  46. return hrOut;
  47. }
  48. private:
  49. HRESULT m_hr;
  50. };
  51. class CVARIANT
  52. {
  53. VARIANT v;
  54. public:
  55. CVARIANT() { VariantInit(&v); }
  56. ~CVARIANT() { VariantClear(&v); }
  57. CVARIANT(const CVARIANT& vIn) throw(CVARIANTError)
  58. {
  59. HRESULT hr = S_OK;
  60. VariantInit(&v);
  61. hr = ::VariantCopy(&v, const_cast<VARIANT*>(&(vIn.v)));
  62. if(FAILED(hr))
  63. {
  64. throw CVARIANTError(hr);
  65. }
  66. }
  67. void Clear() { VariantClear(&v); }
  68. operator VARIANT *() { return &v; }
  69. VARIANT *operator &() { return &v; }
  70. CVARIANT& operator=(const CVARIANT& rv)
  71. {
  72. HRESULT hr = S_OK;
  73. hr = ::VariantCopy(&v, const_cast<VARIANT*>(&(rv.v)));
  74. if(FAILED(hr))
  75. {
  76. throw CVARIANTError(hr);
  77. }
  78. return *this;
  79. }
  80. CVARIANT(LPCWSTR pSrc) { ::VariantInit(&v); SetStr(pSrc); }
  81. CVARIANT(LONG lSrc) { ::VariantInit(&v); SetLONG(lSrc); }
  82. CVARIANT(DWORD dwSrc) { ::VariantInit(&v); SetDWORD(dwSrc); }
  83. CVARIANT(LONGLONG llSrc) { ::VariantInit(&v); SetLONGLONG(llSrc); }
  84. CVARIANT(ULONGLONG ullSrc) { ::VariantInit(&v); SetULONGLONG(ullSrc); }
  85. CVARIANT(BOOL b) { ::VariantInit(&v); SetBool(b); }
  86. CVARIANT(short i) { ::VariantInit(&v); SetShort(i); }
  87. CVARIANT(double d) { ::VariantInit(&v); SetDouble(d); }
  88. CVARIANT(BYTE b) { ::VariantInit(&v); SetByte(b); }
  89. CVARIANT(IDispatch * pDisp) { ::VariantInit(&v); pDisp->AddRef(); SetDispatch(pDisp); }
  90. CVARIANT(VARIANT& vIn)
  91. {
  92. HRESULT hr = S_OK;
  93. ::VariantInit(&v);
  94. hr = ::VariantCopy(&v, &vIn);
  95. if(FAILED(hr))
  96. {
  97. throw CVARIANTError(hr);
  98. }
  99. }
  100. // Can't have a DATE override, since DATA and double are defined
  101. // the same. Hence, to set a date, construct a CVARIANT using the
  102. // default constructor, then call SetDate.
  103. //CVARIANT(DATE dtDate) { ::VariantInit(&v); SetDATE(dtDate); }
  104. void SetStr(LPCWSTR pSrc)
  105. { Clear(); V_VT(&v) = pSrc ? VT_BSTR : VT_NULL;
  106. V_BSTR(&v) = pSrc ? SysAllocString(pSrc) : 0;
  107. }
  108. LPWSTR GetStr() { return V_VT(&v) == VT_BSTR ? V_BSTR(&v) : 0; }
  109. operator LPWSTR() { return V_VT(&v) == VT_BSTR ? V_BSTR(&v) : 0; }
  110. void SetLONG(LONG lSrc) { Clear(); V_VT(&v) = VT_I4; V_I4(&v) = lSrc; }
  111. LONG GetLONG() { return V_I4(&v); }
  112. operator LONG() { return V_I4(&v); }
  113. void SetDWORD(DWORD dwSrc) { Clear(); V_VT(&v) = VT_UI4; V_UI4(&v) = dwSrc; }
  114. LONG GetDWORD() { return V_UI4(&v); }
  115. operator DWORD() { return V_UI4(&v); }
  116. void SetLONGLONG(LONGLONG llSrc) { Clear(); V_VT(&v) = VT_I8; V_I8(&v) = llSrc; }
  117. LONGLONG GetLONGLONG() { return V_I8(&v); }
  118. operator LONGLONG() { return V_I8(&v); }
  119. void SetULONGLONG(ULONGLONG ullSrc) { Clear(); V_VT(&v) = VT_UI8; V_UI8(&v) = ullSrc; }
  120. ULONGLONG GetULONGLONG() { return V_UI8(&v); }
  121. operator ULONGLONG() { return V_UI8(&v); }
  122. void SetDouble(double dSrc) { Clear(); V_VT(&v) = VT_R8; V_R8(&v) = dSrc; }
  123. double GetDouble() { return V_R8(&v); }
  124. operator double() { return V_R8(&v); }
  125. void SetDate(DATE dtDate) { Clear(); V_VT(&v) = VT_DATE; V_DATE(&v) = dtDate; }
  126. double GetDate() { return V_DATE(&v); }
  127. // operator DATE won't work since we have operator double. You must call
  128. // GetDate() instead.
  129. //operator DATE() { return V_DATE(&v); }
  130. void SetByte(BYTE bySrc) { Clear(); V_VT(&v) = VT_UI1; V_UI1(&v) = bySrc; }
  131. BYTE GetByte() { return V_UI1(&v); }
  132. operator BYTE() { return V_UI1(&v); }
  133. void SetBool(BOOL b) { V_VT(&v) = VT_BOOL; V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE; }
  134. BOOL GetBool() { return V_BOOL(&v) == VARIANT_TRUE; }
  135. operator BOOL() { return V_BOOL(&v); }
  136. void SetDispatch(IDispatch* pDisp) { V_VT(&v) = VT_DISPATCH; V_DISPATCH(&v) = pDisp; if(pDisp) pDisp->AddRef(); }
  137. IDispatch * GetDispatch() { return V_DISPATCH(&v); }
  138. void SetUnknown(IUnknown* pUnk) { V_VT(&v) = VT_UNKNOWN; V_UNKNOWN(&v) = pUnk; if(pUnk) pUnk->AddRef(); }
  139. IUnknown * GetUnknown() { return V_UNKNOWN(&v); }
  140. void SetShort(short i) { V_VT(&v) = VT_I2; V_I2(&v) = i; }
  141. short GetShort() { return V_I2(&v); }
  142. operator short() { return V_I2(&v); }
  143. VARTYPE GetType() { return V_VT(&v); }
  144. // Should only be used for artificially
  145. // setting the type to something other
  146. // than what it really is!
  147. void SetType(VARTYPE vt) { V_VT(&v) = vt; }
  148. void SetArray(SAFEARRAY *p, VARTYPE vt) { Clear(); V_VT(&v) = vt; V_ARRAY(&v) = p; }
  149. // This function acquires the SAFEARRAY pointer and it is no longer owned
  150. // by the caller.
  151. operator SAFEARRAY *() { return (V_VT(&v) & VT_ARRAY ? V_ARRAY(&v) : 0); }
  152. void Unbind() { ::VariantInit(&v); }
  153. };