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.

196 lines
4.4 KiB

  1. //==============================================================;
  2. //
  3. // This source code is only intended as a supplement to existing Microsoft documentation.
  4. //
  5. //
  6. //
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  9. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11. // PURPOSE.
  12. //
  13. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  14. //
  15. //
  16. //
  17. //==============================================================;
  18. #include <objbase.h>
  19. #include <initguid.h>
  20. #include "guids.h"
  21. #include "basesnap.h"
  22. #include "Comp.h"
  23. #include "CompData.h"
  24. #include "Registry.h"
  25. // our globals
  26. HINSTANCE g_hinst;
  27. BOOL WINAPI DllMain(HINSTANCE hinstDLL,
  28. DWORD fdwReason,
  29. void* lpvReserved)
  30. {
  31. if (fdwReason == DLL_PROCESS_ATTACH) {
  32. g_hinst = hinstDLL;
  33. }
  34. return TRUE;
  35. }
  36. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvObj)
  37. {
  38. if (rclsid != CLSID_CComponentData)
  39. return CLASS_E_CLASSNOTAVAILABLE;
  40. if (!ppvObj)
  41. return E_FAIL;
  42. *ppvObj = NULL;
  43. // We can only hand out IUnknown and IClassFactory pointers. Fail
  44. // if they ask for anything else.
  45. if (!IsEqualIID(riid, IID_IUnknown) && !IsEqualIID(riid, IID_IClassFactory))
  46. return E_NOINTERFACE;
  47. CClassFactory *pFactory = new CClassFactory();
  48. if (NULL == pFactory)
  49. return E_OUTOFMEMORY;
  50. HRESULT hr = pFactory->QueryInterface(riid, ppvObj);
  51. return hr;
  52. }
  53. STDAPI DllCanUnloadNow(void)
  54. {
  55. if (g_uObjects == 0 && g_uSrvLock == 0)
  56. return S_OK;
  57. else
  58. return S_FALSE;
  59. }
  60. CClassFactory::CClassFactory()
  61. : m_cref(0)
  62. {
  63. OBJECT_CREATED
  64. }
  65. CClassFactory::~CClassFactory()
  66. {
  67. OBJECT_DESTROYED
  68. }
  69. STDMETHODIMP CClassFactory::QueryInterface(REFIID riid, LPVOID *ppv)
  70. {
  71. if (!ppv)
  72. return E_FAIL;
  73. *ppv = NULL;
  74. if (IsEqualIID(riid, IID_IUnknown))
  75. *ppv = static_cast<IClassFactory *>(this);
  76. else
  77. if (IsEqualIID(riid, IID_IClassFactory))
  78. *ppv = static_cast<IClassFactory *>(this);
  79. if (*ppv)
  80. {
  81. reinterpret_cast<IUnknown *>(*ppv)->AddRef();
  82. return S_OK;
  83. }
  84. return E_NOINTERFACE;
  85. }
  86. STDMETHODIMP_(ULONG) CClassFactory::AddRef()
  87. {
  88. return InterlockedIncrement((LONG *)&m_cref);
  89. }
  90. STDMETHODIMP_(ULONG) CClassFactory::Release()
  91. {
  92. if (InterlockedDecrement((LONG *)&m_cref) == 0)
  93. {
  94. delete this;
  95. return 0;
  96. }
  97. return m_cref;
  98. }
  99. STDMETHODIMP CClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID * ppvObj)
  100. {
  101. HRESULT hr;
  102. void* pObj;
  103. if (!ppvObj)
  104. return E_FAIL;
  105. *ppvObj = NULL;
  106. // Our object does does not support aggregation, so we need to
  107. // fail if they ask us to do aggregation.
  108. if (pUnkOuter)
  109. return CLASS_E_NOAGGREGATION;
  110. pObj = new CComponentData();
  111. if (!pObj)
  112. return E_OUTOFMEMORY;
  113. // QueryInterface will do the AddRef() for us, so we do not
  114. // do it in this function
  115. hr = ((LPUNKNOWN)pObj)->QueryInterface(riid, ppvObj);
  116. if (FAILED(hr))
  117. delete pObj;
  118. return hr;
  119. }
  120. STDMETHODIMP CClassFactory::LockServer(BOOL fLock)
  121. {
  122. if (fLock)
  123. InterlockedIncrement((LONG *)&g_uSrvLock);
  124. else
  125. InterlockedDecrement((LONG *)&g_uSrvLock);
  126. return S_OK;
  127. }
  128. //////////////////////////////////////////////////////////
  129. //
  130. // Exported functions
  131. //
  132. //
  133. // Server registration
  134. //
  135. STDAPI DllRegisterServer()
  136. {
  137. _TCHAR szName[256];
  138. _TCHAR szSnapInName[256];
  139. LoadString(g_hinst, IDS_NAME, szName, sizeof(szName));
  140. LoadString(g_hinst, IDS_SNAPINNAME, szSnapInName, sizeof(szSnapInName));
  141. if (RegisterServer(g_hinst,
  142. CLSID_CComponentData,
  143. szName) == S_OK)
  144. return RegisterSnapin(CLSID_CComponentData, szSnapInName, IID_NULL, FALSE);
  145. else
  146. return E_FAIL;
  147. }
  148. STDAPI DllUnregisterServer()
  149. {
  150. if (UnregisterServer(CLSID_CComponentData) == S_OK)
  151. return UnregisterSnapin(CLSID_CComponentData);
  152. else
  153. return E_FAIL;
  154. }