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.

134 lines
4.0 KiB

  1. /*++
  2. Copyright (C) 1998-2001 Microsoft Corporation
  3. Module Name:
  4. OAHELP.INL
  5. Abstract:
  6. OLE Automation Helpers for client code
  7. History:
  8. raymcc 21-Jul-97
  9. --*/
  10. #ifndef _OAHELP_INL_
  11. #define _OAHELP_INL_
  12. class CBSTR
  13. {
  14. BSTR m_pStr;
  15. public:
  16. CBSTR() { m_pStr = 0; }
  17. CBSTR(LPWSTR pSrc) { SetStr(pSrc);}
  18. ~CBSTR() { Clear(); }
  19. operator BSTR() { return m_pStr; }
  20. void Unbind() { m_pStr = 0; }
  21. void Clear() {if (m_pStr) SysFreeString(m_pStr);}
  22. void SetStr(LPWSTR pSrc) {m_pStr = SysAllocString(pSrc); }
  23. };
  24. class CVARIANT
  25. {
  26. protected:
  27. VARIANT v;
  28. public:
  29. CVARIANT() { VariantInit(&v); }
  30. ~CVARIANT() { VariantClear(&v); }
  31. void Clear() { VariantClear(&v); }
  32. operator VARIANT *() { return &v; }
  33. VARIANT *operator &() { return &v; }
  34. CVARIANT(LPWSTR pSrc) { VariantInit(&v); SetStr(pSrc); }
  35. CVARIANT(LONG lSrc) { VariantInit(&v); SetLONG(lSrc); }
  36. CVARIANT(BOOL b) { VariantInit(&v); SetBool(b); }
  37. CVARIANT(short i) { VariantInit(&v); SetShort(i); }
  38. CVARIANT(double d) { VariantInit(&v); SetDouble(d); }
  39. CVARIANT(BYTE b) { VariantInit(&v); SetByte(b); }
  40. CVARIANT(IDispatch * pDisp) { VariantInit(&v); pDisp->AddRef(); SetDispatch(pDisp); }
  41. void SetStr(LPWSTR pSrc)
  42. { Clear(); V_VT(&v) = pSrc ? VT_BSTR : VT_NULL;
  43. V_BSTR(&v) = pSrc ? SysAllocString(pSrc) : 0;
  44. }
  45. LPWSTR GetStr() { return V_VT(&v) == VT_BSTR ? V_BSTR(&v) : 0; }
  46. operator LPWSTR() { return V_VT(&v) == VT_BSTR ? V_BSTR(&v) : 0; }
  47. void SetLONG(LONG lSrc) { Clear(); V_VT(&v) = VT_I4; V_I4(&v) = lSrc; }
  48. LONG GetLONG() { return V_I4(&v); }
  49. operator LONG() { return V_I4(&v); }
  50. void SetDouble(double dSrc) { Clear(); V_VT(&v) = VT_R8; V_R8(&v) = dSrc; }
  51. double GetDouble() { return V_R8(&v); }
  52. operator double() { return V_R8(&v); }
  53. void SetByte(BYTE bySrc) { Clear(); V_VT(&v) = VT_UI1; V_UI1(&v) = bySrc; }
  54. BYTE GetByte() { return V_UI1(&v); }
  55. operator BYTE() { return V_UI1(&v); }
  56. void SetBool(BOOL b) { V_VT(&v) = VT_BOOL; V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE; }
  57. BOOL GetBool() { return V_BOOL(&v) == VARIANT_TRUE; }
  58. operator BOOL() { return V_BOOL(&v); }
  59. void SetDispatch(IDispatch* pDisp) { V_VT(&v) = VT_DISPATCH; V_DISPATCH(&v) = pDisp; }
  60. IDispatch * GetDispatch() { return V_DISPATCH(&v); }
  61. void SetUnknown(IUnknown* pUnk) { V_VT(&v) = VT_UNKNOWN; V_UNKNOWN(&v) = pUnk; }
  62. IUnknown * GetUnknown() { return V_UNKNOWN(&v); }
  63. void SetShort(short i) { V_VT(&v) = VT_I2; V_I2(&v) = i; }
  64. short GetShort() { return V_I2(&v); }
  65. operator short() { return V_I2(&v); }
  66. VARTYPE GetType() { return V_VT(&v); }
  67. void SetArray(SAFEARRAY *p, VARTYPE vt) { Clear(); V_VT(&v) = vt; V_ARRAY(&v) = p; }
  68. // This function acquires the SAFEARRAY pointer and it is no longer owned
  69. // by the caller.
  70. operator SAFEARRAY *() { return (V_VT(&v) & VT_ARRAY ? V_ARRAY(&v) : 0); }
  71. void Unbind() { VariantInit(&v); }
  72. };
  73. class CSAFEARRAY
  74. {
  75. SAFEARRAY *p;
  76. public:
  77. CSAFEARRAY(VARTYPE v, int nSize)
  78. {
  79. SAFEARRAYBOUND rgsabound[1];
  80. rgsabound[0].lLbound = 0;
  81. rgsabound[0].cElements = nSize;
  82. p = SafeArrayCreate(v, 1, rgsabound);
  83. }
  84. ~CSAFEARRAY() { if (p) SafeArrayDestroy(p); }
  85. CSAFEARRAY(SAFEARRAY *pSrc) { p = pSrc; }
  86. HRESULT Put(long nLocation, LPVOID pData) {
  87. return SafeArrayPutElement(p, &nLocation, pData);
  88. }
  89. HRESULT Get(long nLocation, LPVOID pData) {
  90. return SafeArrayGetElement(p, &nLocation, pData);
  91. }
  92. HRESULT Access(void **pData) { return SafeArrayAccessData(p, pData); }
  93. HRESULT Unaccess() { return SafeArrayUnaccessData(p); }
  94. operator SAFEARRAY *() { return p; }
  95. long GetNumElements() { long upper; SafeArrayGetUBound(p, 1, &upper); return upper + 1; }
  96. void Unbind() { p = 0; }
  97. };
  98. #endif