ReactOS 0.4.15-dev-7788-g1ad9096
register.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS W32Time Service
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Service Registration
5 * COPYRIGHT: Copyright 2021 Eric Kohl <ekohl@reactos.org>
6 */
7
8#include "w32time.h"
9#include <debug.h>
10#include <strsafe.h>
11
12#include "resource.h"
13
14static
17 _In_ UINT uID)
18{
20 int nLength;
21 PWSTR pszString, pszPtr;
22
23 hInstance = GetModuleHandleW(L"w32time");
24 if (hInstance == NULL)
25 return NULL;
26
27 nLength = LoadStringW(hInstance, uID, (LPWSTR)&pszPtr, 0);
28 if (nLength == 0)
29 return NULL;
30
32 if (pszString == NULL)
33 return NULL;
34
35 wcsncpy(pszString, pszPtr, nLength);
36
37 return pszString;
38}
39
40
41static
44{
45 SC_HANDLE hServiceManager = NULL;
46 SC_HANDLE hService = NULL;
47 HKEY hKey = NULL;
48 PWSTR pszValue;
49 PWSTR pszDisplayName = NULL, pszDescription = NULL;
50 DWORD dwDisposition, dwError;
51 SERVICE_DESCRIPTIONW ServiceDescription;
52 HRESULT hresult = S_OK;
53
54 DPRINT("RegisterService()\n");
55
57 if (hServiceManager == NULL)
58 {
59 DPRINT1("OpenSCManager() failed!\n");
60 hresult = E_FAIL;
61 goto done;
62 }
63
64 pszDisplayName = ReadString(IDS_DISPLAYNAME);
65 if (pszDisplayName == NULL)
66 {
67 DPRINT1("ReadString(IDS_DISPLAYNAME) failed!\n");
68 hresult = E_FAIL;
69 goto done;
70 }
71
72 pszDescription = ReadString(IDS_DESCRIPTION);
73 if (pszDescription == NULL)
74 {
75 DPRINT1("ReadString(IDS_DESCRIPTION) failed!\n");
76 hresult = E_FAIL;
77 goto done;
78 }
79
80 hService = CreateServiceW(hServiceManager,
81 L"W32Time",
82 pszDisplayName,
87 L"%SystemRoot%\\system32\\svchost.exe -k netsvcs",
88 L"Time", NULL, NULL, L"LocalSystem", NULL);
89 if (hService == NULL)
90 {
91 DPRINT1("CreateService() failed!\n");
92 hresult = E_FAIL;
93 goto done;
94 }
95
96 ServiceDescription.lpDescription = pszDescription;
97 if (ChangeServiceConfig2W(hService,
99 &ServiceDescription) == FALSE)
100 {
101 DPRINT1("ChangeServiceConfig2() failed!\n");
102 hresult = E_FAIL;
103 goto done;
104 }
105
107 L"System\\CurrentControlSet\\Services\\W32Time\\Parameters",
108 0,
109 NULL,
111 KEY_WRITE,
112 NULL,
113 &hKey,
114 &dwDisposition);
115 if (dwError != ERROR_SUCCESS)
116 {
117 DPRINT1("RegCreateKeyEx() failed!\n");
118 hresult = E_FAIL;
119 goto done;
120 }
121
122 pszValue = L"%SystemRoot%\\system32\\w32time.dll";
123 dwError = RegSetValueExW(hKey,
124 L"ServiceDll",
125 0,
127 (LPBYTE)pszValue,
128 (wcslen(pszValue) + 1) * sizeof(WCHAR));
129 if (dwError != ERROR_SUCCESS)
130 {
131 DPRINT1("RegSetValueEx() failed!\n");
132 hresult = E_FAIL;
133 goto done;
134 }
135
136 pszValue = L"SvchostEntry_W32Time";
137 dwError = RegSetValueExW(hKey,
138 L"ServiceMain",
139 0,
140 REG_SZ,
141 (LPBYTE)pszValue,
142 (wcslen(pszValue) + 1) * sizeof(WCHAR));
143 if (dwError != ERROR_SUCCESS)
144 {
145 DPRINT1("RegSetValueEx() failed!\n");
146 hresult = E_FAIL;
147 goto done;
148 }
149
150done:
151 if (hKey)
153
154 if (hService)
155 CloseServiceHandle(hService);
156
157 if (hServiceManager)
158 CloseServiceHandle(hServiceManager);
159
160 if (pszDescription)
161 HeapFree(GetProcessHeap(), 0, pszDescription);
162
163 if (pszDisplayName)
164 HeapFree(GetProcessHeap(), 0, pszDisplayName);
165
166 return hresult;
167}
168
169
170static
173{
174 HKEY hKey = NULL;
175 PWSTR pszValue;
176 DWORD dwDisposition, dwError;
177 HRESULT hresult = S_OK;
178
180 L"System\\CurrentControlSet\\Services\\W32Time\\Parameters",
181 0,
182 NULL,
184 KEY_WRITE,
185 NULL,
186 &hKey,
187 &dwDisposition);
188 if (dwError != ERROR_SUCCESS)
189 {
190 DPRINT1("RegCreateKeyEx() failed!\n");
191 goto done;
192 }
193
194 pszValue = L"NTP";
195 dwError = RegSetValueExW(hKey,
196 L"Type",
197 0,
198 REG_SZ,
199 (LPBYTE)pszValue,
200 (wcslen(pszValue) + 1) * sizeof(WCHAR));
201 if (dwError != ERROR_SUCCESS)
202 {
203 DPRINT1("RegSetValueEx() failed!\n");
204 goto done;
205 }
206
207done:
208 if (hKey)
210
211 return hresult;
212}
213
214
215static
218{
219 HKEY hKey = NULL;
220 DWORD dwValue;
221 DWORD dwDisposition, dwError;
222 HRESULT hresult = S_OK;
223
225 L"System\\CurrentControlSet\\Services\\W32Time\\TimeProviders\\NtpClient",
226 0,
227 NULL,
229 KEY_WRITE,
230 NULL,
231 &hKey,
232 &dwDisposition);
233 if (dwError != ERROR_SUCCESS)
234 {
235 DPRINT1("RegCreateKeyEx() failed!\n");
236 goto done;
237 }
238
239 dwValue = 0x00093a80;
240 dwError = RegSetValueExW(hKey,
241 L"SpecialPollInterval",
242 0,
243 REG_DWORD,
244 (LPBYTE)&dwValue,
245 sizeof(dwValue));
246 if (dwError != ERROR_SUCCESS)
247 {
248 DPRINT1("RegSetValueEx() failed!\n");
249 goto done;
250 }
251
252done:
253 if (hKey)
255
256 return hresult;
257}
258
259
261WINAPI
263{
264 HRESULT hresult;
265
266 hresult = RegisterService();
267 if (FAILED(hresult))
268 return hresult;
269
270 hresult = SetParametersValues();
271 if (FAILED(hresult))
272 return hresult;
273
274 hresult = SetNtpClientValues();
275
276 return hresult;
277}
278
279
281WINAPI
283{
284 DPRINT1("DllUnregisterServer()\n");
285 return S_OK;
286}
#define IDS_DESCRIPTION
Definition: resource.h:4
#define DPRINT1
Definition: precomp.h:8
static HRESULT SetNtpClientValues(VOID)
Definition: register.c:217
static HRESULT RegisterService(VOID)
Definition: register.c:43
static PWSTR ReadString(_In_ UINT uID)
Definition: register.c:16
static HRESULT SetParametersValues(VOID)
Definition: register.c:172
#define IDS_DISPLAYNAME
Definition: resource.h:3
STDAPI DllRegisterServer(void)
Definition: misc.cpp:180
STDAPI DllUnregisterServer(void)
Definition: misc.cpp:196
#define RegCloseKey(hKey)
Definition: registry.h:49
HINSTANCE hInstance
Definition: charmap.c:19
#define E_FAIL
Definition: ddrawi.h:102
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
LONG WINAPI RegCreateKeyExW(_In_ HKEY hKey, _In_ LPCWSTR lpSubKey, _In_ DWORD Reserved, _In_opt_ LPWSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _Out_ PHKEY phkResult, _Out_opt_ LPDWORD lpdwDisposition)
Definition: reg.c:1096
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4911
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
#define REG_SZ
Definition: layer.c:22
#define _In_
Definition: ms_sal.h:308
unsigned int UINT
Definition: ndis.h:50
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_WRITE
Definition: nt_native.h:1031
#define GENERIC_WRITE
Definition: nt_native.h:90
#define REG_EXPAND_SZ
Definition: nt_native.h:1494
#define L(x)
Definition: ntvdm.h:50
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2068
SC_HANDLE WINAPI CreateServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, LPCWSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCWSTR lpBinaryPathName, LPCWSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCWSTR lpDependencies, LPCWSTR lpServiceStartName, LPCWSTR lpPassword)
Definition: scm.c:812
BOOL WINAPI ChangeServiceConfig2W(SC_HANDLE hService, DWORD dwInfoLevel, LPVOID lpInfo)
Definition: scm.c:305
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
#define REG_DWORD
Definition: sdbapi.c:596
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
#define DPRINT
Definition: sndvol32.h:71
LPWSTR lpDescription
Definition: winsvc.h:196
uint16_t * PWSTR
Definition: typedefs.h:56
unsigned char * LPBYTE
Definition: typedefs.h:53
_In_ DWORD nLength
Definition: wincon.h:473
_In_ DWORD _In_ DWORD _Out_writes_to_opt_ pcchString LPSTR pszString
Definition: wincrypt.h:4505
#define WINAPI
Definition: msvc.h:6
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define SERVICE_CONFIG_DESCRIPTION
Definition: winsvc.h:65
#define SC_MANAGER_CREATE_SERVICE
Definition: winsvc.h:15
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
#define SERVICE_WIN32_SHARE_PROCESS
Definition: cmtypes.h:963
#define SERVICE_AUTO_START
Definition: cmtypes.h:977
#define SERVICE_ERROR_NORMAL
Definition: cmtypes.h:982
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184