Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287/* * PROJECT: ReactOS W32Time Service * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) * PURPOSE: Service Registration * COPYRIGHT: Copyright 2021 Eric Kohl <ekohl@reactos.org> */
#include "w32time.h"#include <debug.h>#include <strsafe.h>
#include "resource.h"
staticPWSTRReadString( _In_ UINT uID){ HINSTANCE hInstance; int nLength; PWSTR pszString, pszPtr;
hInstance = GetModuleHandleW(L"w32time"); if (hInstance == NULL) return NULL;
nLength = LoadStringW(hInstance, uID, (LPWSTR)&pszPtr, 0); if (nLength == 0) return NULL;
pszString = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR)); if (pszString == NULL) return NULL;
wcsncpy(pszString, pszPtr, nLength);
return pszString;}
staticHRESULTRegisterService(VOID){ SC_HANDLE hServiceManager = NULL; SC_HANDLE hService = NULL; HKEY hKey = NULL; PWSTR pszValue; PWSTR pszDisplayName = NULL, pszDescription = NULL; DWORD dwDisposition, dwError; SERVICE_DESCRIPTIONW ServiceDescription; HRESULT hresult = S_OK;
DPRINT("RegisterService()\n");
hServiceManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CREATE_SERVICE); if (hServiceManager == NULL) { DPRINT1("OpenSCManager() failed!\n"); hresult = E_FAIL; goto done; }
pszDisplayName = ReadString(IDS_DISPLAYNAME); if (pszDisplayName == NULL) { DPRINT1("ReadString(IDS_DISPLAYNAME) failed!\n"); hresult = E_FAIL; goto done; }
pszDescription = ReadString(IDS_DESCRIPTION); if (pszDescription == NULL) { DPRINT1("ReadString(IDS_DESCRIPTION) failed!\n"); hresult = E_FAIL; goto done; }
hService = CreateServiceW(hServiceManager, L"W32Time", pszDisplayName, GENERIC_WRITE, SERVICE_WIN32_SHARE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, L"%SystemRoot%\\system32\\svchost.exe -k netsvcs", L"Time", NULL, NULL, L"LocalSystem", NULL); if (hService == NULL) { DPRINT1("CreateService() failed!\n"); hresult = E_FAIL; goto done; }
ServiceDescription.lpDescription = pszDescription; if (ChangeServiceConfig2W(hService, SERVICE_CONFIG_DESCRIPTION, &ServiceDescription) == FALSE) { DPRINT1("ChangeServiceConfig2() failed!\n"); hresult = E_FAIL; goto done; }
dwError = RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\W32Time\\Parameters", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisposition); if (dwError != ERROR_SUCCESS) { DPRINT1("RegCreateKeyEx() failed!\n"); hresult = E_FAIL; goto done; }
pszValue = L"%SystemRoot%\\system32\\w32time.dll"; dwError = RegSetValueExW(hKey, L"ServiceDll", 0, REG_EXPAND_SZ, (LPBYTE)pszValue, (wcslen(pszValue) + 1) * sizeof(WCHAR)); if (dwError != ERROR_SUCCESS) { DPRINT1("RegSetValueEx() failed!\n"); hresult = E_FAIL; goto done; }
pszValue = L"SvchostEntry_W32Time"; dwError = RegSetValueExW(hKey, L"ServiceMain", 0, REG_SZ, (LPBYTE)pszValue, (wcslen(pszValue) + 1) * sizeof(WCHAR)); if (dwError != ERROR_SUCCESS) { DPRINT1("RegSetValueEx() failed!\n"); hresult = E_FAIL; goto done; }
done: if (hKey) RegCloseKey(hKey);
if (hService) CloseServiceHandle(hService);
if (hServiceManager) CloseServiceHandle(hServiceManager);
if (pszDescription) HeapFree(GetProcessHeap(), 0, pszDescription);
if (pszDisplayName) HeapFree(GetProcessHeap(), 0, pszDisplayName);
return hresult;}
staticHRESULTSetParametersValues(VOID){ HKEY hKey = NULL; PWSTR pszValue; DWORD dwDisposition, dwError; HRESULT hresult = S_OK;
dwError = RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\W32Time\\Parameters", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisposition); if (dwError != ERROR_SUCCESS) { DPRINT1("RegCreateKeyEx() failed!\n"); goto done; }
pszValue = L"NTP"; dwError = RegSetValueExW(hKey, L"Type", 0, REG_SZ, (LPBYTE)pszValue, (wcslen(pszValue) + 1) * sizeof(WCHAR)); if (dwError != ERROR_SUCCESS) { DPRINT1("RegSetValueEx() failed!\n"); goto done; }
done: if (hKey) RegCloseKey(hKey);
return hresult;}
staticHRESULTSetNtpClientValues(VOID){ HKEY hKey = NULL; DWORD dwValue; DWORD dwDisposition, dwError; HRESULT hresult = S_OK;
dwError = RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\W32Time\\TimeProviders\\NtpClient", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisposition); if (dwError != ERROR_SUCCESS) { DPRINT1("RegCreateKeyEx() failed!\n"); goto done; }
dwValue = W32TIME_POLL_INTERVAL; dwError = RegSetValueExW(hKey, L"SpecialPollInterval", 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue)); if (dwError != ERROR_SUCCESS) { DPRINT1("RegSetValueEx() failed!\n"); goto done; }
done: if (hKey) RegCloseKey(hKey);
return hresult;}
HRESULTWINAPIDllRegisterServer(VOID){ HRESULT hresult;
hresult = RegisterService(); if (FAILED(hresult)) return hresult;
hresult = SetParametersValues(); if (FAILED(hresult)) return hresult;
hresult = SetNtpClientValues();
return hresult;}
HRESULTWINAPIDllUnregisterServer(VOID){ DPRINT1("DllUnregisterServer()\n"); return S_OK;}