1
0

AppRefCount.cpp 863 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "AppRefCount.h"
  2. AppRefCount appRefCount;
  3. AppRefCount::AppRefCount()
  4. {
  5. refCount = 1;
  6. m_dwThread = 0;
  7. }
  8. STDMETHODIMP AppRefCount::QueryInterface(REFIID riid, PVOID *ppvObject)
  9. {
  10. if (!ppvObject)
  11. return E_POINTER;
  12. else if (IsEqualIID(riid, IID_IUnknown))
  13. *ppvObject = this;
  14. else
  15. {
  16. *ppvObject = NULL;
  17. return E_NOINTERFACE;
  18. }
  19. AddRef();
  20. return S_OK;
  21. }
  22. ULONG AppRefCount::AddRef(void)
  23. {
  24. return InterlockedIncrement(&refCount);
  25. }
  26. ULONG AppRefCount::Release(void)
  27. {
  28. LONG lRef = InterlockedDecrement(&refCount);
  29. if (lRef == 0) PostThreadMessage(m_dwThread, WM_NULL, 0, 0);
  30. return lRef;
  31. }
  32. int AppRefCount_CanQuit()
  33. {
  34. return appRefCount.refCount == 0;
  35. }
  36. void *InitAppRefCounterObject(DWORD threadId)
  37. {
  38. appRefCount.m_dwThread = threadId;
  39. return (IUnknown *)&appRefCount;
  40. }
  41. void AppRefCount_Release()
  42. {
  43. appRefCount.Release();
  44. }