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.

62 lines
1.6 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()
  18. {
  19. _refcount = 0;
  20. }
  21. virtual ~_unknown()
  22. {
  23. }
  24. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void ** ppvObject)
  25. {
  26. if (riid == IID_IUnknown)
  27. {
  28. *ppvObject = static_cast<IUnknown*>(this);
  29. }
  30. else if (riid == *I_IID)
  31. {
  32. *ppvObject = static_cast<I*>(this);
  33. }
  34. reinterpret_cast<IUnknown*>(*ppvObject)->AddRef();
  35. return S_OK;
  36. }
  37. virtual ULONG STDMETHODCALLTYPE AddRef( void)
  38. {
  39. return InterlockedIncrement(&_refcount);
  40. }
  41. virtual ULONG STDMETHODCALLTYPE Release( void)
  42. {
  43. DWORD dwError = ::FusionpGetLastWin32Error();
  44. if (InterlockedDecrement(&_refcount) == 0)
  45. {
  46. delete this;
  47. ::FusionpSetLastWin32Error( dwError );
  48. return 0;
  49. }
  50. ::FusionpSetLastWin32Error( dwError );
  51. return _refcount;
  52. }
  53. };
  54. #endif _UNKNOWN_HXX