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.

229 lines
6.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iascore.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Implementation of DLL exports for an ATL in proc server.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 07/09/1997 Original version.
  16. // 11/12/1997 Cleaned up the startup/shutdown code.
  17. // 04/08/1998 Add code for ProductDir registry entry.
  18. // 04/14/1998 Remove SystemMonitor coclass.
  19. // 05/04/1998 Change OBJECT_ENTRY to IASComponentObject.
  20. // 02/18/1999 Move registry values; remove registration code.
  21. // 04/17/2000 Remove Dictionary and DataSource.
  22. //
  23. ///////////////////////////////////////////////////////////////////////////////
  24. #include <iascore.h>
  25. #include <loadperf.h>
  26. #include <AuditChannel.h>
  27. #include <InfoBase.h>
  28. #include <NTEventLog.h>
  29. #include <newop.cpp>
  30. CComModule _Module;
  31. BEGIN_OBJECT_MAP(ObjectMap)
  32. OBJECT_ENTRY(__uuidof(AuditChannel), AuditChannel )
  33. OBJECT_ENTRY(__uuidof(InfoBase),
  34. IASTL::IASComponentObject< InfoBase > )
  35. OBJECT_ENTRY(__uuidof(NTEventLog),
  36. IASTL::IASComponentObject< NTEventLog > )
  37. END_OBJECT_MAP()
  38. ///////////////////////////////////////////////////////////////////////////////
  39. //
  40. // FUNCTION
  41. //
  42. // registerCore
  43. //
  44. // DESCRIPTION
  45. //
  46. // Add non-COM registry entries for the IAS core.
  47. //
  48. ///////////////////////////////////////////////////////////////////////////////
  49. HRESULT registerCore() throw ()
  50. {
  51. /////////
  52. // Get the filename for the service DLL.
  53. /////////
  54. DWORD error;
  55. WCHAR filename[MAX_PATH];
  56. DWORD numberChars = GetModuleFileNameW(
  57. _Module.GetModuleInstance(),
  58. filename,
  59. MAX_PATH
  60. );
  61. if ((numberChars == 0) || (numberChars == MAX_PATH))
  62. {
  63. error = GetLastError();
  64. return HRESULT_FROM_WIN32(error);
  65. }
  66. /////////
  67. // Compute the product dir.
  68. /////////
  69. WCHAR prodDir[MAX_PATH];
  70. wcscpy(wcsrchr(wcscpy(prodDir, filename), L'\\'), L"\\IAS");
  71. //////////
  72. // Create the ProductDir entry in the registry.
  73. //////////
  74. CRegKey policyKey;
  75. error = policyKey.Create(
  76. HKEY_LOCAL_MACHINE,
  77. IAS_POLICY_KEY,
  78. NULL,
  79. REG_OPTION_NON_VOLATILE,
  80. KEY_SET_VALUE
  81. );
  82. if (error) { return HRESULT_FROM_WIN32(error); }
  83. error = IASPublishLicenseType(policyKey);
  84. if (error == NO_ERROR)
  85. {
  86. error = policyKey.SetValue(prodDir, L"ProductDir");
  87. }
  88. return HRESULT_FROM_WIN32(error);
  89. }
  90. extern CRITICAL_SECTION theGlobalLock;
  91. /////////////////////////////////////////////////////////////////////////////
  92. // DLL Entry Point
  93. extern "C"
  94. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  95. {
  96. if (dwReason == DLL_PROCESS_ATTACH)
  97. {
  98. if (!InitializeCriticalSectionAndSpinCount(&theGlobalLock, 0))
  99. {
  100. return FALSE;
  101. }
  102. _Module.Init(ObjectMap, hInstance);
  103. DisableThreadLibraryCalls(hInstance);
  104. }
  105. else if (dwReason == DLL_PROCESS_DETACH)
  106. {
  107. _Module.Term();
  108. DeleteCriticalSection(&theGlobalLock);
  109. }
  110. return TRUE; // ok
  111. }
  112. /////////////////////////////////////////////////////////////////////////////
  113. // Used to determine whether the DLL can be unloaded by OLE
  114. STDAPI DllCanUnloadNow(void)
  115. {
  116. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  117. }
  118. /////////////////////////////////////////////////////////////////////////////
  119. // Returns a class factory to create an object of the requested type
  120. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  121. {
  122. return _Module.GetClassObject(rclsid, riid, ppv);
  123. }
  124. // return true is IAS is installed or some other errors occured
  125. // false when the SCM returns ERROR_SERVICE_DOES_NOT_EXIST
  126. bool IsIASInstalled() throw ()
  127. {
  128. SC_HANDLE manager = OpenSCManager(
  129. NULL,
  130. SERVICES_ACTIVE_DATABASE,
  131. GENERIC_READ
  132. );
  133. if (manager == 0)
  134. {
  135. // Should not happen
  136. _ASSERT(FALSE);
  137. return true;
  138. }
  139. SC_HANDLE service = OpenService(
  140. manager,
  141. L"IAS",
  142. SERVICE_QUERY_STATUS
  143. );
  144. bool iasInstalled;
  145. if (service != 0)
  146. {
  147. // IAS is installed
  148. CloseServiceHandle(service);
  149. iasInstalled = true;
  150. }
  151. else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
  152. {
  153. // This is the only case where it is 100% sure IAS is NOT installed
  154. iasInstalled = false;
  155. }
  156. CloseServiceHandle(manager);
  157. return iasInstalled;
  158. }
  159. /////////////////////////////////////////////////////////////////////////////
  160. // DllRegisterServer - Adds entries to the system registry
  161. STDAPI DllRegisterServer(void)
  162. {
  163. // There was a bug in Win2K that caused the PerfMon counters to be
  164. // registered even if IAS wasn't installed.
  165. BOOL isWow64;
  166. if (!IsIASInstalled() &&
  167. ((IsWow64Process(GetCurrentProcess(),&isWow64)) && (!isWow64)))
  168. {
  169. // Remove the perf counters only when:
  170. // - IAS is not installed
  171. // - AND the process is native x86 or native ia64, but not wow64
  172. UnloadPerfCounterTextStringsW(L"LODCTR " IASServiceName, TRUE);
  173. }
  174. HRESULT hr = registerCore();
  175. if (FAILED(hr)) return hr;
  176. // registers object, typelib and all interfaces in typelib
  177. return _Module.RegisterServer(TRUE);
  178. }
  179. /////////////////////////////////////////////////////////////////////////////
  180. // DllUnregisterServer - Removes entries from the system registry
  181. STDAPI DllUnregisterServer(void)
  182. {
  183. HRESULT hr = _Module.UnregisterServer();
  184. if (FAILED(hr)) return hr;
  185. hr = UnRegisterTypeLib(__uuidof(IASCoreLib),
  186. 1,
  187. 0,
  188. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  189. SYS_WIN32);
  190. return hr;
  191. }
  192. #include <atlimpl.cpp>