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.

135 lines
3.9 KiB

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