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.

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