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.

285 lines
9.0 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: ThemeService.cpp
  3. //
  4. // Copyright (c) 2001, Microsoft Corporation
  5. //
  6. // This file contains functions that are called from the shell services DLL
  7. // to interact with the theme service.
  8. //
  9. // History: 2001-01-02 vtan created
  10. // --------------------------------------------------------------------------
  11. #include "StandardHeader.h"
  12. #include "ThemeService.h"
  13. #include <shlwapi.h>
  14. #include <shlwapip.h>
  15. #include "Resource.h"
  16. #include "ThemeManagerAPIRequest.h"
  17. #include "ThemeManagerService.h"
  18. #include "ThemeServerClient.h"
  19. extern HINSTANCE g_hInstance;
  20. CRITICAL_SECTION g_csThemeService = {0};
  21. // --------------------------------------------------------------------------
  22. // CThemeService::Main
  23. //
  24. // Arguments: See the platform SDK under DllMain.
  25. //
  26. // Returns: See the platform SDK under DllMain.
  27. //
  28. // Purpose: Performs initialization and clean up on process attach and
  29. // detach. Not interested in anything else.
  30. //
  31. // History: 2000-10-12 vtan created
  32. // 2001-01-02 vtan scoped to a C++ class
  33. // --------------------------------------------------------------------------
  34. NTSTATUS CThemeService::_ProcessAttach()
  35. {
  36. NTSTATUS status;
  37. status = CThemeManagerAPIRequest::StaticInitialize();
  38. if (!NT_SUCCESS(status))
  39. goto cleanup;
  40. status = CThemeServerClient::StaticInitialize();
  41. if (!NT_SUCCESS(status))
  42. goto cleanup1;
  43. if (!InitializeCriticalSectionAndSpinCount(&g_csThemeService, 0))
  44. {
  45. status = STATUS_NO_MEMORY;
  46. goto cleanup2;
  47. }
  48. status = STATUS_SUCCESS;
  49. goto cleanup;
  50. cleanup2:
  51. CThemeServerClient::StaticTerminate();
  52. cleanup1:
  53. CThemeManagerAPIRequest::StaticTerminate();
  54. cleanup:
  55. return status;
  56. }
  57. // --------------------------------------------------------------------------
  58. // CThemeService::Main
  59. //
  60. // Arguments: See the platform SDK under DllMain.
  61. //
  62. // Returns: See the platform SDK under DllMain.
  63. //
  64. // Purpose: Performs initialization and clean up on process attach and
  65. // detach. Not interested in anything else.
  66. //
  67. // History: 2000-10-12 vtan created
  68. // 2001-01-02 vtan scoped to a C++ class
  69. // --------------------------------------------------------------------------
  70. BOOL CThemeService::Main (DWORD dwReason)
  71. {
  72. NTSTATUS status;
  73. switch (dwReason)
  74. {
  75. case DLL_PROCESS_ATTACH:
  76. status = _ProcessAttach();
  77. break;
  78. case DLL_PROCESS_DETACH:
  79. DeleteCriticalSection(&g_csThemeService);
  80. TSTATUS(CThemeServerClient::StaticTerminate());
  81. TSTATUS(CThemeManagerAPIRequest::StaticTerminate());
  82. status = STATUS_SUCCESS;
  83. break;
  84. default:
  85. status = STATUS_SUCCESS;
  86. break;
  87. }
  88. return NT_SUCCESS(status);
  89. }
  90. // --------------------------------------------------------------------------
  91. // CThemeService::DllRegisterServer
  92. //
  93. // Arguments: <none>
  94. //
  95. // Returns: NTSTATUS
  96. //
  97. // Purpose: Register entry point to allow the theme server to install
  98. // itself into the registry.
  99. //
  100. // History: 2000-11-28 vtan created
  101. // 2001-01-02 vtan scoped to a C++ class
  102. // --------------------------------------------------------------------------
  103. NTSTATUS CThemeService::RegisterServer (void)
  104. {
  105. NTSTATUS status;
  106. status = STATUS_SUCCESS;
  107. // In upgrade cases, remove our old name service from both 32 & 64 bit systems
  108. (NTSTATUS)CService::Remove(TEXT("ThemeService"));
  109. #ifdef _WIN64
  110. // In upgrade cases for 64-bit, remove our current name service
  111. (NTSTATUS)CService::Remove(CThemeManagerService::GetName());
  112. #else
  113. // This is 32-bit only. Check if this is REALLY 32-bit and not 32-bit on 64-bit.
  114. if (!IsOS(OS_WOW6432))
  115. {
  116. // Prepare the failure actions, in order to get the service to restart automatically
  117. SC_ACTION ac[3];
  118. ac[0].Type = SC_ACTION_RESTART;
  119. ac[0].Delay = 60000;
  120. ac[1].Type = SC_ACTION_RESTART;
  121. ac[1].Delay = 60000;
  122. ac[2].Type = SC_ACTION_NONE;
  123. ac[2].Delay = 0;
  124. SERVICE_FAILURE_ACTIONS sf;
  125. sf.dwResetPeriod = 86400;
  126. sf.lpRebootMsg = NULL;
  127. sf.lpCommand = NULL;
  128. sf.cActions = 3;
  129. sf.lpsaActions = ac;
  130. // Now install the new service by name.
  131. status = CService::Install(CThemeManagerService::GetName(),
  132. TEXT("%SystemRoot%\\System32\\svchost.exe -k netsvcs"),
  133. TEXT("UIGroup"),
  134. NULL,
  135. TEXT("shsvcs.dll"),
  136. NULL,
  137. TEXT("netsvcs"),
  138. TEXT("ThemeServiceMain"),
  139. // Disabled by default on Server SKUs
  140. IsOS(OS_ANYSERVER) ? SERVICE_DISABLED : SERVICE_AUTO_START,
  141. g_hInstance,
  142. IDS_THEMESERVER_DISPLAYNAME,
  143. IDS_THEMESERVER_DESCRIPTION,
  144. &sf);
  145. }
  146. #endif
  147. return(status);
  148. }
  149. // --------------------------------------------------------------------------
  150. // CThemeService::DllUnregisterServer
  151. //
  152. // Arguments: <none>
  153. //
  154. // Returns: NTSTATUS
  155. //
  156. // Purpose: Unregister entry point to allow the theme server to uninstall
  157. // itself from the registry.
  158. //
  159. // History: 2000-11-28 vtan created
  160. // 2001-01-02 vtan scoped to a C++ class
  161. // --------------------------------------------------------------------------
  162. NTSTATUS CThemeService::UnregisterServer (void)
  163. {
  164. // Ignore any "not found", etc errors.
  165. (NTSTATUS)CService::Remove(CThemeManagerService::GetName());
  166. return(STATUS_SUCCESS);
  167. }
  168. // --------------------------------------------------------------------------
  169. // ::ThemeServiceMain
  170. //
  171. // Arguments: dwArgc = Number of arguments.
  172. // lpszArgv = Argument array.
  173. //
  174. // Returns: <none>
  175. //
  176. // Purpose: ServiceMain entry point for theme server.
  177. //
  178. // History: 2000-11-28 vtan created
  179. // 2001-01-02 vtan scoped to the theme service
  180. // 2002-03-22 scotthan add robustness, debug exception handling
  181. // (otherwise, SCM quietly handles service exceptions).
  182. // --------------------------------------------------------------------------
  183. void WINAPI ThemeServiceMain (DWORD dwArgc, LPWSTR *lpszArgv)
  184. {
  185. UNREFERENCED_PARAMETER(dwArgc);
  186. UNREFERENCED_PARAMETER(lpszArgv);
  187. NTSTATUS status;
  188. DEBUG_TRY();
  189. EnterCriticalSection(&g_csThemeService);
  190. status = CThemeManagerAPIRequest::InitializeServerChangeNumber();
  191. if (NT_SUCCESS(status))
  192. {
  193. CThemeManagerAPIServer *pThemeManagerAPIServer;
  194. // Bring in shell32.dll NOW so that when CheckThemeSignature is called
  195. // and it tries to use SHGetFolderPath it won't cause shell32.dll to be
  196. // brought in while impersonating a user. This will cause advapi32.dll
  197. // to leak a key to the user's hive that won't get cleaned up at logoff.
  198. CModule* pModule = new CModule(TEXT("shell32.dll"));
  199. if( pModule != NULL )
  200. {
  201. pThemeManagerAPIServer = new CThemeManagerAPIServer;
  202. if (pThemeManagerAPIServer != NULL)
  203. {
  204. CAPIConnection *pAPIConnection;
  205. pAPIConnection = new CAPIConnection(pThemeManagerAPIServer);
  206. if (pAPIConnection != NULL)
  207. {
  208. CThemeManagerService *pThemeManagerService;
  209. pThemeManagerService = new CThemeManagerService(pAPIConnection, pThemeManagerAPIServer);
  210. if (pThemeManagerService != NULL)
  211. {
  212. CThemeManagerSessionData::SetAPIConnection(pAPIConnection);
  213. TSTATUS(CThemeManagerAPIRequest::ArrayInitialize());
  214. pThemeManagerService->Start();
  215. pThemeManagerService->Release();
  216. TSTATUS(CThemeManagerAPIRequest::ArrayTerminate());
  217. CThemeManagerSessionData::ReleaseAPIConnection();
  218. }
  219. pAPIConnection->Release();
  220. }
  221. pThemeManagerAPIServer->Release();
  222. }
  223. delete pModule;
  224. }
  225. }
  226. LeaveCriticalSection(&g_csThemeService);
  227. DEBUG_EXCEPT("Breaking in ThemeServiceMain exception handler.");
  228. }