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.

154 lines
2.5 KiB

  1. /*++
  2. // Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved
  3. Module Name:
  4. helpers.h
  5. Abstract:
  6. Generic helper code
  7. History:
  8. --*/
  9. #ifndef _HELPERS_H_
  10. #include <genutils.h>
  11. inline wchar_t *Macro_CloneLPWSTR(LPCWSTR src)
  12. {
  13. if (!src)
  14. return 0;
  15. wchar_t *dest = new wchar_t[wcslen(src) + 1];
  16. if (!dest)
  17. return 0;
  18. return wcscpy(dest, src);
  19. }
  20. //#define Macro_CloneLPWSTR(x) \
  21. // (x ? wcscpy(new wchar_t[wcslen(x) + 1], x) : 0)
  22. template<class T>
  23. class CDeleteMe
  24. {
  25. protected:
  26. T* m_p;
  27. public:
  28. CDeleteMe(T* p) : m_p(p){}
  29. ~CDeleteMe() {delete m_p;}
  30. };
  31. class CSysFreeMe
  32. {
  33. protected:
  34. BSTR m_str;
  35. public:
  36. CSysFreeMe(BSTR str) : m_str(str){}
  37. ~CSysFreeMe() {SysFreeString(m_str);}
  38. };
  39. typedef LPVOID * PPVOID;
  40. template<class TObj>
  41. class CGenFactory : public IClassFactory
  42. {
  43. protected:
  44. long m_cRef;
  45. public:
  46. CGenFactory(void)
  47. {
  48. m_cRef=0L;
  49. return;
  50. };
  51. ~CGenFactory(void)
  52. {
  53. return;
  54. }
  55. //IUnknown members
  56. STDMETHODIMP QueryInterface(REFIID riid, PPVOID ppv)
  57. {
  58. *ppv=NULL;
  59. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  60. *ppv=this;
  61. if (NULL!=*ppv)
  62. {
  63. ((LPUNKNOWN)*ppv)->AddRef();
  64. return NOERROR;
  65. }
  66. return ResultFromScode(E_NOINTERFACE);
  67. };
  68. STDMETHODIMP_(ULONG) AddRef(void)
  69. {
  70. return InterlockedIncrement(&m_cRef);
  71. };
  72. STDMETHODIMP_(ULONG) Release(void)
  73. {
  74. long lRet = InterlockedDecrement(&m_cRef);
  75. if (0 ==lRet)
  76. delete this;
  77. return lRet;
  78. };
  79. //IClassFactory members
  80. STDMETHODIMP CreateInstance(IN LPUNKNOWN pUnkOuter, IN REFIID riid, OUT PPVOID ppvObj)
  81. {
  82. HRESULT hr;
  83. *ppvObj=NULL;
  84. hr=E_OUTOFMEMORY;
  85. // This object doesnt support aggregation.
  86. if (NULL!=pUnkOuter)
  87. return ResultFromScode(CLASS_E_NOAGGREGATION);
  88. //Create the object passing function to notify on destruction.
  89. TObj * pObj = new TObj();
  90. if (NULL==pObj)
  91. return hr;
  92. // Setup the class all empty, etc.
  93. pObj->InitEmpty();
  94. hr=pObj->QueryInterface(riid, ppvObj);
  95. pObj->Release();
  96. return hr;
  97. };
  98. STDMETHODIMP LockServer(BOOL fLock)
  99. {
  100. if (fLock)
  101. InterlockedIncrement((long *)&g_cLock);
  102. else
  103. InterlockedDecrement((long *)&g_cLock);
  104. return NOERROR;
  105. };
  106. };
  107. class CReleaseMe
  108. {
  109. protected:
  110. IUnknown* m_pUnk;
  111. public:
  112. CReleaseMe(IUnknown* pUnk) : m_pUnk(pUnk){}
  113. ~CReleaseMe() { release();}
  114. void release() { if(m_pUnk) m_pUnk->Release(); m_pUnk=0;}
  115. };
  116. #endif