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.

161 lines
3.3 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. #define LENGTH_OF(a) (sizeof(a)/sizeof(a[0]))
  12. #ifndef _DBG_ASSERT
  13. #ifdef DBG
  14. #define _DBG_ASSERT(X) { if (!(X)) { DebugBreak(); } }
  15. #else
  16. #define _DBG_ASSERT(X)
  17. #endif
  18. #endif
  19. inline wchar_t *Macro_CloneLPWSTR(LPCWSTR src)
  20. {
  21. if (NULL == src) return 0;
  22. size_t cchTmp = wcslen(src) + 1;
  23. wchar_t *dest = new wchar_t[cchTmp];
  24. if (dest) memcpy(dest,src,cchTmp * sizeof(WCHAR));
  25. return dest;
  26. }
  27. template<class T>
  28. class CDeleteMe
  29. {
  30. protected:
  31. T* m_p;
  32. public:
  33. CDeleteMe(T* p) : m_p(p){}
  34. ~CDeleteMe() {delete m_p;}
  35. };
  36. class CSysFreeMe
  37. {
  38. protected:
  39. BSTR m_str;
  40. public:
  41. CSysFreeMe(BSTR str) : m_str(str){}
  42. ~CSysFreeMe() {SysFreeString(m_str);}
  43. };
  44. typedef LPVOID * PPVOID;
  45. template<class TObj>
  46. class CGenFactory : public IClassFactory
  47. {
  48. protected:
  49. long m_cRef;
  50. public:
  51. CGenFactory(void)
  52. {
  53. m_cRef=0L;
  54. return;
  55. };
  56. ~CGenFactory(void)
  57. {
  58. return;
  59. }
  60. //IUnknown members
  61. STDMETHODIMP QueryInterface(REFIID riid, PPVOID ppv)
  62. {
  63. *ppv=NULL;
  64. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  65. *ppv=this;
  66. if (NULL!=*ppv)
  67. {
  68. ((LPUNKNOWN)*ppv)->AddRef();
  69. return NOERROR;
  70. }
  71. return ResultFromScode(E_NOINTERFACE);
  72. };
  73. STDMETHODIMP_(ULONG) AddRef(void)
  74. {
  75. return InterlockedIncrement(&m_cRef);
  76. };
  77. STDMETHODIMP_(ULONG) Release(void)
  78. {
  79. long lRet = InterlockedDecrement(&m_cRef);
  80. if (0 ==lRet)
  81. delete this;
  82. return lRet;
  83. };
  84. //IClassFactory members
  85. STDMETHODIMP CreateInstance(IN LPUNKNOWN pUnkOuter, IN REFIID riid, OUT PPVOID ppvObj)
  86. {
  87. HRESULT hr;
  88. *ppvObj=NULL;
  89. hr=E_OUTOFMEMORY;
  90. // This object doesnt support aggregation.
  91. if (NULL!=pUnkOuter)
  92. return ResultFromScode(CLASS_E_NOAGGREGATION);
  93. //Create the object passing function to notify on destruction.
  94. TObj * pObj = new TObj();
  95. if (NULL==pObj)
  96. return hr;
  97. // Setup the class all empty, etc.
  98. pObj->InitEmpty();
  99. hr=pObj->QueryInterface(riid, ppvObj);
  100. pObj->Release();
  101. return hr;
  102. };
  103. STDMETHODIMP LockServer(BOOL fLock)
  104. {
  105. if (fLock)
  106. InterlockedIncrement((long *)&g_cLock);
  107. else
  108. InterlockedDecrement((long *)&g_cLock);
  109. return NOERROR;
  110. };
  111. };
  112. class CReleaseMe
  113. {
  114. protected:
  115. IUnknown* m_pUnk;
  116. public:
  117. CReleaseMe(IUnknown* pUnk) : m_pUnk(pUnk){}
  118. ~CReleaseMe() { release();}
  119. void release() { if(m_pUnk) m_pUnk->Release(); m_pUnk=0;}
  120. };
  121. #endif