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.

130 lines
4.0 KiB

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