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.

225 lines
5.3 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 <olectl.h>
  20. #include <initguid.h>
  21. #include "guids.h"
  22. #include "basesnap.h"
  23. #include "Comp.h"
  24. #include "CompData.h"
  25. #include "About.h"
  26. #include "Registry.h"
  27. // our globals
  28. HINSTANCE g_hinst;
  29. BOOL WINAPI DllMain(HINSTANCE hinstDLL,
  30. DWORD fdwReason,
  31. void* lpvReserved)
  32. {
  33. if (fdwReason == DLL_PROCESS_ATTACH) {
  34. g_hinst = hinstDLL;
  35. }
  36. return TRUE;
  37. }
  38. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvObj)
  39. {
  40. if ((rclsid != CLSID_CComponentData) && (rclsid != CLSID_CSnapinAbout))
  41. return CLASS_E_CLASSNOTAVAILABLE;
  42. if (!ppvObj)
  43. return E_FAIL;
  44. *ppvObj = NULL;
  45. // We can only hand out IUnknown and IClassFactory pointers. Fail
  46. // if they ask for anything else.
  47. if (!IsEqualIID(riid, IID_IUnknown) && !IsEqualIID(riid, IID_IClassFactory))
  48. return E_NOINTERFACE;
  49. CClassFactory *pFactory = NULL;
  50. // make the factory passing in the creation function for the type of object they want
  51. if (rclsid == CLSID_CComponentData)
  52. pFactory = new CClassFactory(CClassFactory::COMPONENT);
  53. else if (rclsid == CLSID_CSnapinAbout)
  54. pFactory = new CClassFactory(CClassFactory::ABOUT);
  55. if (NULL == pFactory)
  56. return E_OUTOFMEMORY;
  57. HRESULT hr = pFactory->QueryInterface(riid, ppvObj);
  58. return hr;
  59. }
  60. STDAPI DllCanUnloadNow(void)
  61. {
  62. if (g_uObjects == 0 && g_uSrvLock == 0)
  63. return S_OK;
  64. else
  65. return S_FALSE;
  66. }
  67. CClassFactory::CClassFactory(FACTORY_TYPE factoryType)
  68. : m_cref(0), m_factoryType(factoryType)
  69. {
  70. OBJECT_CREATED
  71. }
  72. CClassFactory::~CClassFactory()
  73. {
  74. OBJECT_DESTROYED
  75. }
  76. STDMETHODIMP CClassFactory::QueryInterface(REFIID riid, LPVOID *ppv)
  77. {
  78. if (!ppv)
  79. return E_FAIL;
  80. *ppv = NULL;
  81. if (IsEqualIID(riid, IID_IUnknown))
  82. *ppv = static_cast<IClassFactory *>(this);
  83. else
  84. if (IsEqualIID(riid, IID_IClassFactory))
  85. *ppv = static_cast<IClassFactory *>(this);
  86. if (*ppv)
  87. {
  88. reinterpret_cast<IUnknown *>(*ppv)->AddRef();
  89. return S_OK;
  90. }
  91. return E_NOINTERFACE;
  92. }
  93. STDMETHODIMP_(ULONG) CClassFactory::AddRef()
  94. {
  95. return InterlockedIncrement((LONG *)&m_cref);
  96. }
  97. STDMETHODIMP_(ULONG) CClassFactory::Release()
  98. {
  99. if (InterlockedDecrement((LONG *)&m_cref) == 0)
  100. {
  101. delete this;
  102. return 0;
  103. }
  104. return m_cref;
  105. }
  106. STDMETHODIMP CClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID * ppvObj)
  107. {
  108. HRESULT hr;
  109. void* pObj;
  110. if (!ppvObj)
  111. return E_FAIL;
  112. *ppvObj = NULL;
  113. // Our object does does not support aggregation, so we need to
  114. // fail if they ask us to do aggregation.
  115. if (pUnkOuter)
  116. return CLASS_E_NOAGGREGATION;
  117. if (COMPONENT == m_factoryType) {
  118. pObj = new CComponentData();
  119. } else {
  120. pObj = new CSnapinAbout();
  121. }
  122. if (!pObj)
  123. return E_OUTOFMEMORY;
  124. // QueryInterface will do the AddRef() for us, so we do not
  125. // do it in this function
  126. hr = ((LPUNKNOWN)pObj)->QueryInterface(riid, ppvObj);
  127. if (FAILED(hr))
  128. delete pObj;
  129. return hr;
  130. }
  131. STDMETHODIMP CClassFactory::LockServer(BOOL fLock)
  132. {
  133. if (fLock)
  134. InterlockedIncrement((LONG *)&g_uSrvLock);
  135. else
  136. InterlockedDecrement((LONG *)&g_uSrvLock);
  137. return S_OK;
  138. }
  139. //////////////////////////////////////////////////////////
  140. //
  141. // Exported functions
  142. //
  143. //
  144. // Server registration
  145. //
  146. STDAPI DllRegisterServer()
  147. {
  148. HRESULT hr = SELFREG_E_CLASS;
  149. _TCHAR szName[256];
  150. _TCHAR szSnapInName[256];
  151. LoadString(g_hinst, IDS_NAME, szName, sizeof(szName));
  152. LoadString(g_hinst, IDS_SNAPINNAME, szSnapInName, sizeof(szSnapInName));
  153. _TCHAR szAboutName[256];
  154. LoadString(g_hinst, IDS_ABOUTNAME, szAboutName, sizeof(szAboutName));
  155. // register our CoClasses
  156. hr = RegisterServer(g_hinst,
  157. CLSID_CComponentData,
  158. szName);
  159. if SUCCEEDED(hr)
  160. hr = RegisterServer(g_hinst,
  161. CLSID_CSnapinAbout,
  162. szAboutName);
  163. // place the registry information for SnapIns
  164. if SUCCEEDED(hr)
  165. hr = RegisterSnapin(CLSID_CComponentData, szSnapInName, CLSID_CSnapinAbout, FALSE);
  166. return hr;
  167. }
  168. STDAPI DllUnregisterServer()
  169. {
  170. if (UnregisterServer(CLSID_CComponentData) == S_OK)
  171. return UnregisterSnapin(CLSID_CComponentData);
  172. else
  173. return E_FAIL;
  174. }