ReactOS 0.4.17-dev-736-g75e91de
register.c File Reference
#include "w32time.h"
#include <debug.h>
#include <strsafe.h>
#include "resource.h"
Include dependency graph for register.c:

Go to the source code of this file.

Functions

static PWSTR ReadString (_In_ UINT uID)
 
static HRESULT RegisterService (VOID)
 
static HRESULT SetParametersValues (VOID)
 
static HRESULT SetNtpClientValues (VOID)
 
HRESULT WINAPI DllRegisterServer (VOID)
 
static UINT UnregisterService (VOID)
 
HRESULT WINAPI DllUnregisterServer (VOID)
 

Variables

PCWSTR g_pszServiceName = L"W32Time"
 

Function Documentation

◆ DllRegisterServer()

HRESULT WINAPI DllRegisterServer ( VOID  )

Definition at line 269 of file register.c.

270{
271 HRESULT hresult;
272
273 hresult = RegisterService();
274 if (FAILED(hresult))
275 return hresult;
276
277 hresult = SetParametersValues();
278 if (FAILED(hresult))
279 return hresult;
280
281 hresult = SetNtpClientValues();
282
283 return hresult;
284}
static HRESULT SetNtpClientValues(VOID)
Definition: register.c:224
static HRESULT RegisterService(VOID)
Definition: register.c:45
static HRESULT SetParametersValues(VOID)
Definition: register.c:179
#define FAILED(hr)
Definition: intsafe.h:51

◆ DllUnregisterServer()

HRESULT WINAPI DllUnregisterServer ( VOID  )

Definition at line 327 of file register.c.

328{
331}
static UINT UnregisterService(VOID)
Definition: register.c:289
unsigned int UINT
Definition: sysinfo.c:13
#define error(str)
Definition: mkdosfs.c:1605
static HRESULT HRESULT_FROM_WIN32(unsigned int x)
Definition: winerror.h:210

◆ ReadString()

static PWSTR ReadString ( _In_ UINT  uID)
static

Definition at line 18 of file register.c.

20{
22 int nLength;
23 PWSTR pszString, pszPtr;
24
25 hInstance = GetModuleHandleW(L"w32time");
26 if (hInstance == NULL)
27 return NULL;
28
29 nLength = LoadStringW(hInstance, uID, (LPWSTR)&pszPtr, 0);
30 if (nLength == 0)
31 return NULL;
32
33 pszString = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
34 if (pszString == NULL)
35 return NULL;
36
37 wcsncpy(pszString, pszPtr, nLength);
38
39 return pszString;
40}
HINSTANCE hInstance
Definition: charmap.c:19
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
#define L(x)
Definition: resources.c:13
short WCHAR
Definition: pedump.c:58
_In_ UINT uID
Definition: shlwapi.h:156
wcsncpy
#define LoadStringW
Definition: utils.h:64
uint16_t * PWSTR
Definition: typedefs.h:56
uint16_t * LPWSTR
Definition: typedefs.h:56
WINBASEAPI _In_ DWORD nLength
Definition: wincon.h:682

Referenced by RegisterService().

◆ RegisterService()

static HRESULT RegisterService ( VOID  )
static

Definition at line 45 of file register.c.

46{
47 SC_HANDLE hServiceManager = NULL;
48 SC_HANDLE hService = NULL;
49 HKEY hKey = NULL;
51 PWSTR pszDisplayName = NULL, pszDescription = NULL;
52 DWORD dwDisposition, dwError;
53 SERVICE_DESCRIPTIONW ServiceDescription;
54 HRESULT hresult = S_OK;
55
56 DPRINT("RegisterService()\n");
57
59 if (hServiceManager == NULL)
60 {
61 DPRINT1("OpenSCManager() failed!\n");
62 hresult = E_FAIL;
63 goto done;
64 }
65
66 pszDisplayName = ReadString(IDS_DISPLAYNAME);
67 if (pszDisplayName == NULL)
68 {
69 DPRINT1("ReadString(IDS_DISPLAYNAME) failed!\n");
70 hresult = E_FAIL;
71 goto done;
72 }
73
74 pszDescription = ReadString(IDS_DESCRIPTION);
75 if (pszDescription == NULL)
76 {
77 DPRINT1("ReadString(IDS_DESCRIPTION) failed!\n");
78 hresult = E_FAIL;
79 goto done;
80 }
81
82 hService = CreateServiceW(hServiceManager,
84 pszDisplayName,
89 L"%SystemRoot%\\system32\\svchost.exe -k netsvcs",
90 L"Time", NULL, NULL, L"LocalSystem", NULL);
91
92 if (!hService && GetLastError() == ERROR_SERVICE_EXISTS)
93 {
94 hService = OpenServiceW(hServiceManager, g_pszServiceName, SERVICE_CHANGE_CONFIG);
95 }
96 if (hService == NULL)
97 {
98 DPRINT1("CreateService() failed!\n");
99 hresult = E_FAIL;
100 goto done;
101 }
102
103 ServiceDescription.lpDescription = pszDescription;
104 if (ChangeServiceConfig2W(hService,
106 &ServiceDescription) == FALSE)
107 {
108 DPRINT1("ChangeServiceConfig2() failed!\n");
109 hresult = E_FAIL;
110 goto done;
111 }
112
114 L"System\\CurrentControlSet\\Services\\W32Time\\Parameters",
115 0,
116 NULL,
118 KEY_WRITE,
119 NULL,
120 &hKey,
121 &dwDisposition);
122 if (dwError != ERROR_SUCCESS)
123 {
124 DPRINT1("RegCreateKeyEx() failed!\n");
125 hresult = E_FAIL;
126 goto done;
127 }
128
129 pszValue = L"%SystemRoot%\\system32\\w32time.dll";
130 dwError = RegSetValueExW(hKey,
131 L"ServiceDll",
132 0,
135 (wcslen(pszValue) + 1) * sizeof(WCHAR));
136 if (dwError != ERROR_SUCCESS)
137 {
138 DPRINT1("RegSetValueEx() failed!\n");
139 hresult = E_FAIL;
140 goto done;
141 }
142
143 pszValue = L"SvchostEntry_W32Time";
144 dwError = RegSetValueExW(hKey,
145 L"ServiceMain",
146 0,
147 REG_SZ,
149 (wcslen(pszValue) + 1) * sizeof(WCHAR));
150 if (dwError != ERROR_SUCCESS)
151 {
152 DPRINT1("RegSetValueEx() failed!\n");
153 hresult = E_FAIL;
154 goto done;
155 }
156
157done:
158 if (hKey)
160
161 if (hService)
162 CloseServiceHandle(hService);
163
164 if (hServiceManager)
165 CloseServiceHandle(hServiceManager);
166
167 if (pszDescription)
168 HeapFree(GetProcessHeap(), 0, pszDescription);
169
170 if (pszDisplayName)
171 HeapFree(GetProcessHeap(), 0, pszDisplayName);
172
173 return hresult;
174}
#define IDS_DESCRIPTION
Definition: resource.h:4
#define DPRINT1
Definition: precomp.h:8
static PWSTR ReadString(_In_ UINT uID)
Definition: register.c:18
PCWSTR g_pszServiceName
Definition: register.c:14
#define IDS_DISPLAYNAME
Definition: resource.h:3
#define RegCloseKey(hKey)
Definition: registry.h:49
#define E_FAIL
Definition: ddrawi.h:102
#define ERROR_SUCCESS
Definition: deptool.c:10
#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:4882
#define HeapFree(x, y, z)
Definition: compat.h:735
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2988
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
#define S_OK
Definition: intsafe.h:52
#define REG_SZ
Definition: layer.c:22
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1060
#define KEY_WRITE
Definition: nt_native.h:1034
#define GENERIC_WRITE
Definition: nt_native.h:90
#define REG_EXPAND_SZ
Definition: nt_native.h:1497
_In_opt_ LPCSTR _In_opt_ LPCSTR pszValue
Definition: shlwapi.h:783
#define SERVICE_CONFIG_DESCRIPTION
Definition: winsvc.h:71
#define SC_MANAGER_CREATE_SERVICE
Definition: winsvc.h:15
#define SERVICE_CHANGE_CONFIG
Definition: winsvc.h:60
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2107
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2199
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 DPRINT
Definition: sndvol32.h:73
LPWSTR lpDescription
Definition: winsvc.h:202
unsigned char * LPBYTE
Definition: typedefs.h:53
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ERROR_SERVICE_EXISTS
Definition: winerror.h:948
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define SERVICE_WIN32_SHARE_PROCESS
Definition: cmtypes.h:964
#define SERVICE_AUTO_START
Definition: cmtypes.h:978
#define SERVICE_ERROR_NORMAL
Definition: cmtypes.h:983

Referenced by DllRegisterServer().

◆ SetNtpClientValues()

static HRESULT SetNtpClientValues ( VOID  )
static

Definition at line 224 of file register.c.

225{
226 HKEY hKey = NULL;
227 DWORD dwValue;
228 DWORD dwDisposition, dwError;
229 HRESULT hresult = S_OK;
230
232 L"System\\CurrentControlSet\\Services\\W32Time\\TimeProviders\\NtpClient",
233 0,
234 NULL,
236 KEY_WRITE,
237 NULL,
238 &hKey,
239 &dwDisposition);
240 if (dwError != ERROR_SUCCESS)
241 {
242 DPRINT1("RegCreateKeyEx() failed!\n");
243 goto done;
244 }
245
246 dwValue = W32TIME_POLL_INTERVAL;
247 dwError = RegSetValueExW(hKey,
248 L"SpecialPollInterval",
249 0,
250 REG_DWORD,
251 (LPBYTE)&dwValue,
252 sizeof(dwValue));
253 if (dwError != ERROR_SUCCESS)
254 {
255 DPRINT1("RegSetValueEx() failed!\n");
256 goto done;
257 }
258
259done:
260 if (hKey)
262
263 return hresult;
264}
#define REG_DWORD
Definition: sdbapi.c:615
#define W32TIME_POLL_INTERVAL
Definition: w32time.h:19

Referenced by DllRegisterServer().

◆ SetParametersValues()

static HRESULT SetParametersValues ( VOID  )
static

Definition at line 179 of file register.c.

180{
181 HKEY hKey = NULL;
183 DWORD dwDisposition, dwError;
184 HRESULT hresult = S_OK;
185
187 L"System\\CurrentControlSet\\Services\\W32Time\\Parameters",
188 0,
189 NULL,
191 KEY_WRITE,
192 NULL,
193 &hKey,
194 &dwDisposition);
195 if (dwError != ERROR_SUCCESS)
196 {
197 DPRINT1("RegCreateKeyEx() failed!\n");
198 goto done;
199 }
200
201 pszValue = L"NTP";
202 dwError = RegSetValueExW(hKey,
203 L"Type",
204 0,
205 REG_SZ,
207 (wcslen(pszValue) + 1) * sizeof(WCHAR));
208 if (dwError != ERROR_SUCCESS)
209 {
210 DPRINT1("RegSetValueEx() failed!\n");
211 goto done;
212 }
213
214done:
215 if (hKey)
217
218 return hresult;
219}

Referenced by DllRegisterServer().

◆ UnregisterService()

static UINT UnregisterService ( VOID  )
static

Definition at line 289 of file register.c.

290{
291 UINT error;
292 SC_HANDLE hServiceManager, hService;
293
295 if (!hServiceManager)
296 {
298 DPRINT1("OpenSCManager() failed, error %lu\n", error);
299 return error;
300 }
301
302 hService = OpenServiceW(hServiceManager, g_pszServiceName, DELETE);
304 CloseServiceHandle(hServiceManager);
305 if (!hService)
306 {
308 return ERROR_SUCCESS;
309 DPRINT1("OpenService() failed, error %lu\n", error);
310 }
311 else
312 {
316 if (error)
317 DPRINT1("DeleteService() failed, error %lu\n", error);
318
319 CloseServiceHandle(hService);
320 }
321 return error;
322}
#define DELETE
Definition: nt_native.h:57
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:921
#define ERROR_SERVICE_MARKED_FOR_DELETE
Definition: winerror.h:947
#define ERROR_SERVICE_DOES_NOT_EXIST
Definition: winerror.h:935

Referenced by DllUnregisterServer().

Variable Documentation

◆ g_pszServiceName

PCWSTR g_pszServiceName = L"W32Time"

Definition at line 14 of file register.c.

Referenced by RegisterService(), and UnregisterService().