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.

149 lines
3.0 KiB

  1. /*****************************************************************************
  2. *
  3. * powertoycf.cpp - IClassFactory interface
  4. *
  5. *****************************************************************************/
  6. #include "priv.h"
  7. #define INITSCID
  8. #include <initguid.h>
  9. #include "guids.h"
  10. #include "sccls.h"
  11. #include "power.h"
  12. #include "power_i.c"
  13. // Need this so the GUIDs are defined
  14. #include <mmsystem.h>
  15. #include <amstream.h>
  16. #include "dsound.h"
  17. typedef HRESULT (*PFNCREATE)(IUnknown*, REFIID, LPVOID*);
  18. typedef struct
  19. {
  20. const GUID* pclsid;
  21. PFNCREATE pfnCreate;
  22. } CLASSFACTORYENTRY;
  23. static const CLASSFACTORYENTRY
  24. g_InProcCreateList[] =
  25. {
  26. {&CLSID_Zaxxon, CZaxxon_CreateInstance},
  27. {&CLSID_ZaxxonPlayer, CZaxxonPlayer_CreateInstance},
  28. {&CLSID_MegaMan, CMegaMan_CreateInstance},
  29. {0,0},
  30. };
  31. /*****************************************************************************
  32. * IClassFactory::CreateInstance
  33. *****************************************************************************/
  34. HRESULT CFactory::CreateInstance(IUnknown * punkOuter, REFIID riid, void ** ppv)
  35. {
  36. HRESULT hres = E_INVALIDARG;
  37. if (!punkOuter)
  38. {
  39. const CLASSFACTORYENTRY* pcf = g_InProcCreateList;
  40. int i = 0;
  41. while (pcf[i].pfnCreate)
  42. {
  43. if (IsEqualCLSID(_rclsid, *pcf[i].pclsid))
  44. {
  45. hres = pcf[i].pfnCreate(punkOuter, riid, ppv);
  46. break;
  47. }
  48. i++;
  49. }
  50. }
  51. else
  52. {
  53. hres = CLASS_E_NOAGGREGATION;
  54. }
  55. return hres;
  56. }
  57. /*****************************************************************************
  58. *
  59. * IClassFactory::LockServer
  60. *
  61. * What a stupid function. Locking the server is identical to
  62. * creating an object and not releasing it until you want to unlock
  63. * the server.
  64. *
  65. *****************************************************************************/
  66. HRESULT CFactory::LockServer(BOOL fLock)
  67. {
  68. if (fLock)
  69. DllAddRef();
  70. else
  71. DllRelease();
  72. return S_OK;
  73. }
  74. HRESULT CFactory_Create(REFCLSID rclsid, REFIID riid, void ** ppv)
  75. {
  76. HRESULT hres;
  77. if (IsEqualCLSID(riid, IID_IClassFactory))
  78. {
  79. *ppv = (LPVOID) new CFactory(rclsid);
  80. hres = (*ppv) ? S_OK : E_OUTOFMEMORY;
  81. }
  82. else
  83. hres = E_NOINTERFACE;
  84. return hres;
  85. }
  86. CFactory::CFactory(REFCLSID rclsid) : _cRef(1)
  87. {
  88. _rclsid = rclsid;
  89. DllAddRef();
  90. }
  91. CFactory::~CFactory()
  92. {
  93. DllRelease();
  94. }
  95. //===========================
  96. // *** IUnknown Interface ***
  97. //===========================
  98. ULONG CFactory::AddRef()
  99. {
  100. _cRef++;
  101. return _cRef;
  102. }
  103. ULONG CFactory::Release()
  104. {
  105. ASSERT(_cRef > 0);
  106. _cRef--;
  107. if (_cRef > 0)
  108. return _cRef;
  109. delete this;
  110. return 0;
  111. }
  112. HRESULT CFactory::QueryInterface(REFIID riid, void **ppv)
  113. {
  114. if (IsEqualCLSID(riid, IID_IUnknown) || IsEqualCLSID(riid, IID_IClassFactory))
  115. {
  116. *ppv = SAFECAST(this, IClassFactory *);
  117. }
  118. else
  119. {
  120. *ppv = NULL;
  121. return E_NOINTERFACE;
  122. }
  123. AddRef();
  124. return S_OK;
  125. }