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.

83 lines
1.2 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. UNK.CPP
  5. Abstract:
  6. IUnknown Helpers
  7. History:
  8. --*/
  9. #include "precomp.h"
  10. #include "unk.h"
  11. CUnk::CUnk(CLifeControl* pControl, IUnknown* pOuter)
  12. : m_lRef(0), m_pControl(pControl), m_pOuter(pOuter)
  13. {
  14. if( m_pControl )
  15. {
  16. m_pControl->ObjectCreated((IUnknown*)this);
  17. }
  18. }
  19. CUnk::~CUnk()
  20. {
  21. if( m_pControl )
  22. {
  23. m_pControl->ObjectDestroyed((IUnknown*)this);
  24. }
  25. }
  26. BOOL CUnk::Initialize()
  27. {
  28. m_lRef++;
  29. GetUnknown()->AddRef();
  30. BOOL bRes = OnInitialize();
  31. GetUnknown()->Release();
  32. m_lRef--;
  33. return bRes;
  34. }
  35. // Non-delegating implementation
  36. STDMETHODIMP CUnk::QueryInterface(REFIID riid, void** ppv)
  37. {
  38. if(riid == IID_IUnknown)
  39. *ppv = (IUnknown*)this;
  40. else
  41. *ppv = GetInterface(riid);
  42. if(*ppv)
  43. {
  44. ((IUnknown*)*ppv)->AddRef();
  45. return S_OK;
  46. }
  47. else return E_NOINTERFACE;
  48. }
  49. ULONG CUnk::AddRef()
  50. {
  51. return InterlockedIncrement(&m_lRef);
  52. }
  53. ULONG CUnk::Release()
  54. {
  55. long lRef = InterlockedDecrement(&m_lRef);
  56. if(lRef == 0)
  57. {
  58. m_lRef++;
  59. delete this;
  60. }
  61. return lRef;
  62. }