Source code of Windows XP (NT5)
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.

465 lines
12 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c)1999 Microsoft Corporation, All Rights Reserved
  4. //
  5. // gpdas.h
  6. //
  7. // Module: Rsop Planning mode Provider
  8. //
  9. // History: 11-Jul-99 MickH Created
  10. //
  11. //*************************************************************
  12. // Note: Proxy/Stub Information
  13. // To build a separate proxy/stub DLL,
  14. // run nmake -f RSOPPROVps.mk in the project directory.
  15. #include "stdafx.h"
  16. #include "resource.h"
  17. #include <initguid.h>
  18. #include "planprov.h"
  19. #include <stdio.h>
  20. #include "GPDAS.h"
  21. #include "events.h"
  22. #include "rsopdbg.h"
  23. CServiceModule _Module;
  24. BEGIN_OBJECT_MAP(ObjectMap)
  25. OBJECT_ENTRY(CLSID_RsopPlanningModeProvider, RsopPlanningModeProvider)
  26. END_OBJECT_MAP()
  27. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  28. {
  29. while (p1 != NULL && *p1 != NULL)
  30. {
  31. LPCTSTR p = p2;
  32. while (p != NULL && *p != NULL)
  33. {
  34. if (*p1 == *p)
  35. return CharNext(p1);
  36. p = CharNext(p);
  37. }
  38. p1 = CharNext(p1);
  39. }
  40. return NULL;
  41. }
  42. // Although some of these functions are big they are declared inline since they are only used once
  43. inline HRESULT CServiceModule::RegisterServer(BOOL bRegTypeLib, BOOL bService)
  44. {
  45. HRESULT hr = CoInitialize(NULL);
  46. if (FAILED(hr))
  47. return hr;
  48. // Remove any previous service since it may point to
  49. // the incorrect file
  50. Uninstall();
  51. // Add service entries
  52. UpdateRegistryFromResource(IDR_RSOPPROV, TRUE);
  53. // Adjust the AppID for Local Server or Service
  54. CRegKey keyAppID;
  55. LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_WRITE);
  56. if (lRes != ERROR_SUCCESS)
  57. return lRes;
  58. CRegKey key;
  59. lRes = key.Open(keyAppID, _T("{6EBBFC6C-B721-4D10-9371-5D8E8C76D315}"), KEY_WRITE);
  60. if (lRes != ERROR_SUCCESS)
  61. return lRes;
  62. key.DeleteValue(_T("LocalService"));
  63. if (bService)
  64. {
  65. key.SetValue(_T("RSOPPROV"), _T("LocalService"));
  66. key.SetValue(_T("-Service"), _T("ServiceParameters"));
  67. // Create service
  68. Install();
  69. }
  70. // Add object entries
  71. hr = CComModule::RegisterServer(bRegTypeLib);
  72. CoUninitialize();
  73. return hr;
  74. }
  75. inline HRESULT CServiceModule::UnregisterServer()
  76. {
  77. HRESULT hr = CoInitialize(NULL);
  78. if (FAILED(hr))
  79. return hr;
  80. // Remove service entries
  81. UpdateRegistryFromResource(IDR_RSOPPROV, FALSE);
  82. // Remove service
  83. Uninstall();
  84. // Remove object entries
  85. CComModule::UnregisterServer(TRUE);
  86. CoUninitialize();
  87. return S_OK;
  88. }
  89. inline void CServiceModule::Init(_ATL_OBJMAP_ENTRY* p, HINSTANCE h, UINT nServiceNameID, const GUID* plibid)
  90. {
  91. CComModule::Init(p, h, plibid);
  92. m_bService = TRUE;
  93. LoadString(h, nServiceNameID, m_szServiceName, sizeof(m_szServiceName) / sizeof(TCHAR));
  94. // set up the initial service status
  95. m_hServiceStatus = NULL;
  96. m_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  97. m_status.dwCurrentState = SERVICE_STOPPED;
  98. m_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  99. m_status.dwWin32ExitCode = 0;
  100. m_status.dwServiceSpecificExitCode = 0;
  101. m_status.dwCheckPoint = 0;
  102. m_status.dwWaitHint = 0;
  103. }
  104. LONG CServiceModule::Lock()
  105. {
  106. LONG l = CComModule::Lock();
  107. IncrementServiceCount();
  108. return l;
  109. }
  110. LONG CServiceModule::Unlock()
  111. {
  112. LONG l = CComModule::Unlock();
  113. DecrementServiceCount();
  114. return l;
  115. }
  116. LONG CServiceModule::IncrementServiceCount()
  117. {
  118. LONG l;
  119. l = CoAddRefServerProcess();
  120. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::IncrementServiceCount. Ref count = %d."), l);
  121. return l;
  122. }
  123. LONG CServiceModule::DecrementServiceCount()
  124. {
  125. LONG srvRefCount = CoReleaseServerProcess();
  126. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::DecrementServiceCount. Ref count = %d. "), srvRefCount);
  127. if (srvRefCount == 0) {
  128. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  129. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::Unlock Ref count came down to zero. Exitting."));
  130. }
  131. return srvRefCount;
  132. }
  133. BOOL CServiceModule::IsInstalled()
  134. {
  135. BOOL bResult = FALSE;
  136. SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  137. if (hSCM != NULL)
  138. {
  139. SC_HANDLE hService = ::OpenService(hSCM, m_szServiceName, SERVICE_QUERY_CONFIG);
  140. if (hService != NULL)
  141. {
  142. bResult = TRUE;
  143. ::CloseServiceHandle(hService);
  144. }
  145. ::CloseServiceHandle(hSCM);
  146. }
  147. return bResult;
  148. }
  149. inline BOOL CServiceModule::Install()
  150. {
  151. if (IsInstalled())
  152. return TRUE;
  153. SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  154. if (hSCM == NULL)
  155. {
  156. MessageBox(NULL, _T("Couldn't open service manager"), m_szServiceName, MB_OK);
  157. return FALSE;
  158. }
  159. // Get the executable file path
  160. TCHAR szFilePath[_MAX_PATH];
  161. ::GetModuleFileName(NULL, szFilePath, _MAX_PATH);
  162. SC_HANDLE hService = ::CreateService(
  163. hSCM, m_szServiceName, m_szServiceName,
  164. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  165. SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
  166. szFilePath, NULL, NULL, _T("RPCSS\0"), NULL, NULL);
  167. if (hService == NULL)
  168. {
  169. ::CloseServiceHandle(hSCM);
  170. MessageBox(NULL, _T("Couldn't create service"), m_szServiceName, MB_OK);
  171. return FALSE;
  172. }
  173. ::CloseServiceHandle(hService);
  174. ::CloseServiceHandle(hSCM);
  175. //
  176. // as part of the initialisation, install the eventlog as well.
  177. //
  178. HKEY hKey;
  179. DWORD dwTypes=7, dwDisp;
  180. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\gpdas"),
  181. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp) == ERROR_SUCCESS) {
  182. RegSetValueEx(hKey, TEXT("EventMessageFile"), 0, REG_EXPAND_SZ, (BYTE *)MessageResourceFile, (1+lstrlen(MessageResourceFile))*sizeof(TCHAR));
  183. RegSetValueEx(hKey, TEXT("TypesSupported"), 0, REG_DWORD, (BYTE *)&dwTypes, sizeof(dwTypes));
  184. RegCloseKey(hKey);
  185. }
  186. return TRUE;
  187. }
  188. inline BOOL CServiceModule::Uninstall()
  189. {
  190. if (!IsInstalled())
  191. return TRUE;
  192. SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  193. if (hSCM == NULL)
  194. {
  195. MessageBox(NULL, _T("Couldn't open service manager"), m_szServiceName, MB_OK);
  196. return FALSE;
  197. }
  198. SC_HANDLE hService = ::OpenService(hSCM, m_szServiceName, SERVICE_STOP | DELETE);
  199. if (hService == NULL)
  200. {
  201. ::CloseServiceHandle(hSCM);
  202. MessageBox(NULL, _T("Couldn't open service"), m_szServiceName, MB_OK);
  203. return FALSE;
  204. }
  205. SERVICE_STATUS status;
  206. ::ControlService(hService, SERVICE_CONTROL_STOP, &status);
  207. BOOL bDelete = ::DeleteService(hService);
  208. ::CloseServiceHandle(hService);
  209. ::CloseServiceHandle(hSCM);
  210. if (bDelete)
  211. return TRUE;
  212. MessageBox(NULL, _T("Service could not be deleted"), m_szServiceName, MB_OK);
  213. return FALSE;
  214. }
  215. ///////////////////////////////////////////////////////////////////////////////////////
  216. // Logging functions.
  217. //
  218. // This has static buffers and other restrictions. This is being used only in the service startup scenario
  219. ///////////////////////////////////////////////////////////////////////////////////////
  220. void CServiceModule::LogEvent(LPCTSTR pFormat, ...)
  221. {
  222. /* TCHAR chMsg[256];
  223. HANDLE hEventSource;
  224. LPTSTR lpszStrings[1];
  225. va_list pArg;
  226. va_start(pArg, pFormat);
  227. _vstprintf(chMsg, pFormat, pArg);
  228. va_end(pArg);
  229. lpszStrings[0] = chMsg;
  230. if (m_bService)
  231. {
  232. CEvents ev(TRUE, EVENT_GPDAS_STARTUP);
  233. ev.AddArg(chMsg); ev.Report();
  234. }
  235. else
  236. {
  237. // As we are not running as a service, just write the error to the console.
  238. _putts(chMsg);
  239. }
  240. */
  241. }
  242. //////////////////////////////////////////////////////////////////////////////////////////////
  243. // Service startup and registration
  244. inline void CServiceModule::Start()
  245. {
  246. SERVICE_TABLE_ENTRY st[] =
  247. {
  248. { m_szServiceName, _ServiceMain },
  249. { NULL, NULL }
  250. };
  251. if (m_bService && !::StartServiceCtrlDispatcher(st))
  252. {
  253. m_bService = FALSE;
  254. }
  255. if (m_bService == FALSE)
  256. Run();
  257. }
  258. inline void CServiceModule::ServiceMain(DWORD /* dwArgc */, LPTSTR* /* lpszArgv */)
  259. {
  260. // Register the control request handler
  261. m_status.dwCurrentState = SERVICE_START_PENDING;
  262. m_hServiceStatus = RegisterServiceCtrlHandler(m_szServiceName, _Handler);
  263. if (m_hServiceStatus == NULL)
  264. {
  265. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CServiceModule::ServiceMain failed to Register ServiceCtrlHandler with error %d."), GetLastError() );
  266. return;
  267. }
  268. SetServiceStatus(SERVICE_START_PENDING);
  269. m_status.dwWin32ExitCode = S_OK;
  270. m_status.dwCheckPoint = 0;
  271. m_status.dwWaitHint = 0;
  272. // When the Run function returns, the service has stopped.
  273. Run();
  274. SetServiceStatus(SERVICE_STOPPED);
  275. }
  276. inline void CServiceModule::Handler(DWORD dwOpcode)
  277. {
  278. switch (dwOpcode)
  279. {
  280. case SERVICE_CONTROL_STOP:
  281. SetServiceStatus(SERVICE_STOP_PENDING);
  282. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  283. break;
  284. case SERVICE_CONTROL_PAUSE:
  285. break;
  286. case SERVICE_CONTROL_CONTINUE:
  287. break;
  288. case SERVICE_CONTROL_INTERROGATE:
  289. break;
  290. case SERVICE_CONTROL_SHUTDOWN:
  291. break;
  292. default:
  293. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::Handler Wrong opcode passed to handler %d."), dwOpcode );
  294. }
  295. }
  296. void WINAPI CServiceModule::_ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
  297. {
  298. _Module.ServiceMain(dwArgc, lpszArgv);
  299. }
  300. void WINAPI CServiceModule::_Handler(DWORD dwOpcode)
  301. {
  302. _Module.Handler(dwOpcode);
  303. }
  304. void CServiceModule::SetServiceStatus(DWORD dwState)
  305. {
  306. m_status.dwCurrentState = dwState;
  307. ::SetServiceStatus(m_hServiceStatus, &m_status);
  308. }
  309. void CServiceModule::Run()
  310. {
  311. _Module.dwThreadID = GetCurrentThreadId();
  312. // HRESULT hr = CoInitialize(NULL);
  313. // If you are running on NT 4.0 or higher you can use the following call
  314. // instead to make the EXE free threaded.
  315. // This means that calls come in on a random RPC thread
  316. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  317. _ASSERTE(SUCCEEDED(hr));
  318. // This provides a NULL DACL which will allow access to everyone.
  319. CSecurityDescriptor sd;
  320. sd.InitializeFromThreadToken();
  321. hr = CoInitializeSecurity(sd, -1, NULL, NULL,
  322. RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
  323. _ASSERTE(SUCCEEDED(hr));
  324. hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE);
  325. _ASSERTE(SUCCEEDED(hr));
  326. if (m_bService)
  327. SetServiceStatus(SERVICE_RUNNING);
  328. MSG msg;
  329. while (GetMessage(&msg, 0, 0, 0))
  330. DispatchMessage(&msg);
  331. _Module.RevokeClassObjects();
  332. CoUninitialize();
  333. }
  334. /////////////////////////////////////////////////////////////////////////////
  335. //
  336. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
  337. HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
  338. {
  339. lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
  340. _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_RSOPPROVLib);
  341. _Module.m_bService = TRUE;
  342. TCHAR szTokens[] = _T("-/");
  343. LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
  344. while (lpszToken != NULL)
  345. {
  346. if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
  347. return _Module.UnregisterServer();
  348. // Register as Local Server
  349. if (lstrcmpi(lpszToken, _T("RegServer"))==0)
  350. return _Module.RegisterServer(TRUE, FALSE);
  351. // Register as Service
  352. if (lstrcmpi(lpszToken, _T("Service"))==0)
  353. return _Module.RegisterServer(TRUE, TRUE);
  354. lpszToken = FindOneOf(lpszToken, szTokens);
  355. }
  356. // Are we Service or Local Server
  357. CRegKey keyAppID;
  358. LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_READ);
  359. if (lRes != ERROR_SUCCESS)
  360. return lRes;
  361. CRegKey key;
  362. lRes = key.Open(keyAppID, _T("{6EBBFC6C-B721-4D10-9371-5D8E8C76D315}"), KEY_READ);
  363. if (lRes != ERROR_SUCCESS)
  364. return lRes;
  365. TCHAR szValue[_MAX_PATH];
  366. DWORD dwLen = _MAX_PATH;
  367. lRes = key.QueryValue(szValue, _T("LocalService"), &dwLen);
  368. _Module.m_bService = FALSE;
  369. if (lRes == ERROR_SUCCESS)
  370. _Module.m_bService = TRUE;
  371. _Module.Start();
  372. ShutdownEvents();
  373. // When we get here, the service has been stopped
  374. return _Module.m_status.dwWin32ExitCode;
  375. }