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.

75 lines
1.9 KiB

  1. /*
  2. *
  3. * Copyright (c) 1998,1999 Microsoft Corporation. All rights reserved.
  4. * EXEMPT: copyright change only, no build required
  5. *
  6. */
  7. #ifndef _UNKNOWN_HXX
  8. #define _UNKNOWN_HXX
  9. #pragma once
  10. #include "fusionlastwin32error.h"
  11. //===========================================================================
  12. // This template implements the IUnknown portion of a given COM interface.
  13. template <class I, const IID* I_IID> class _unknown : public I
  14. {
  15. private: long _refcount;
  16. public:
  17. _unknown() : _refcount(0)
  18. {
  19. }
  20. virtual ~_unknown()
  21. {
  22. }
  23. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void ** ppvObject)
  24. {
  25. if (ppvObject == NULL)
  26. return E_POINTER;
  27. else
  28. *ppvObject = NULL;
  29. if (riid == IID_IUnknown)
  30. {
  31. *ppvObject = static_cast<IUnknown*>(this);
  32. }
  33. else if (riid == *I_IID)
  34. {
  35. *ppvObject = static_cast<I*>(this);
  36. }
  37. if (*ppvObject)
  38. {
  39. reinterpret_cast<IUnknown*>(*ppvObject)->AddRef();
  40. return S_OK;
  41. }
  42. else
  43. {
  44. return E_NOINTERFACE;
  45. }
  46. }
  47. virtual ULONG STDMETHODCALLTYPE AddRef( void)
  48. {
  49. return InterlockedIncrement(&_refcount);
  50. }
  51. virtual ULONG STDMETHODCALLTYPE Release( void)
  52. {
  53. DWORD dwError = ::FusionpGetLastWin32Error();
  54. if (InterlockedDecrement(&_refcount) == 0)
  55. {
  56. delete this;
  57. ::FusionpSetLastWin32Error( dwError );
  58. return 0;
  59. }
  60. ::FusionpSetLastWin32Error( dwError );
  61. return _refcount;
  62. }
  63. };
  64. #endif _UNKNOWN_HXX