ReactOS  0.4.13-dev-101-g0ca4b50
internettime.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Timedate Control Panel
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: dll/cpl/timedate/internettime.c
5  * PURPOSE: Internet Time property page
6  * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
7  *
8  */
9 
10 #include "timedate.h"
11 
12 static VOID
14 {
15  HWND hList;
16  WCHAR szValName[MAX_VALUE_NAME];
17  WCHAR szData[256];
18  DWORD dwIndex = 0;
19  DWORD dwValSize;
20  DWORD dwNameSize;
21  DWORD dwDefault = 1;
22  LONG lRet;
23  HKEY hKey;
24 
27 
29  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
30  0,
32  &hKey);
33  if (lRet != ERROR_SUCCESS)
34  return;
35 
36  while (TRUE)
37  {
38  dwValSize = MAX_VALUE_NAME * sizeof(WCHAR);
39  szValName[0] = L'\0';
40  lRet = RegEnumValueW(hKey,
41  dwIndex,
42  szValName,
43  &dwValSize,
44  NULL,
45  NULL,
46  (LPBYTE)szData,
47  &dwNameSize);
48  if (lRet == ERROR_SUCCESS)
49  {
50  /* Get date from default reg value */
51  if (wcscmp(szValName, L"") == 0) // if (Index == 0)
52  {
53  dwDefault = _wtoi(szData);
54  dwIndex++;
55  }
56  else
57  {
60  0,
61  (LPARAM)szData);
62  dwIndex++;
63  }
64  }
65  else if (lRet != ERROR_MORE_DATA)
66  {
67  break;
68  }
69  }
70 
71  if (dwDefault < 1 || dwDefault > dwIndex)
72  dwDefault = 1;
73 
74  /* Server reg entries count from 1,
75  * Combo boxes count from 0 */
76  dwDefault--;
77 
80  dwDefault,
81  0);
82 
83  RegCloseKey(hKey);
84 }
85 
86 
87 /* Set the selected server in the registry */
88 static VOID
90 {
91  HKEY hKey;
92  HWND hList;
93  UINT uSel;
94  WCHAR szSel[4];
95  LONG lRet;
96 
99 
100  uSel = (UINT)SendMessageW(hList, CB_GETCURSEL, 0, 0);
101 
102  /* Server reg entries count from 1,
103  * Combo boxes count from 0 */
104  uSel++;
105 
106  /* Convert to wide char */
107  _itow(uSel, szSel, 10);
108 
110  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
111  0,
113  &hKey);
114  if (lRet != ERROR_SUCCESS)
115  {
116  DisplayWin32Error(lRet);
117  return;
118  }
119 
120  lRet = RegSetValueExW(hKey,
121  L"",
122  0,
123  REG_SZ,
124  (LPBYTE)szSel,
125  (wcslen(szSel) + 1) * sizeof(WCHAR));
126  if (lRet != ERROR_SUCCESS)
127  DisplayWin32Error(lRet);
128 
129  RegCloseKey(hKey);
130 }
131 
132 
133 /* Get the domain name from the registry */
134 static BOOL
136 {
137  HKEY hKey;
138  WCHAR szSel[4];
139  DWORD dwSize;
140  LONG lRet;
141 
143  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
144  0,
146  &hKey);
147  if (lRet != ERROR_SUCCESS)
148  goto fail;
149 
150  /* Get data from default value */
151  dwSize = 4 * sizeof(WCHAR);
152  lRet = RegQueryValueExW(hKey,
153  NULL,
154  NULL,
155  NULL,
156  (LPBYTE)szSel,
157  &dwSize);
158  if (lRet != ERROR_SUCCESS)
159  goto fail;
160 
161  dwSize = 0;
162  lRet = RegQueryValueExW(hKey,
163  szSel,
164  NULL,
165  NULL,
166  NULL,
167  &dwSize);
168  if (lRet != ERROR_SUCCESS)
169  goto fail;
170 
171  (*lpAddress) = (LPWSTR)HeapAlloc(GetProcessHeap(),
172  0,
173  dwSize);
174  if ((*lpAddress) == NULL)
175  {
177  goto fail;
178  }
179 
180  lRet = RegQueryValueExW(hKey,
181  szSel,
182  NULL,
183  NULL,
184  (LPBYTE)*lpAddress,
185  &dwSize);
186  if (lRet != ERROR_SUCCESS)
187  goto fail;
188 
189  RegCloseKey(hKey);
190 
191  return TRUE;
192 
193 fail:
194  DisplayWin32Error(lRet);
195  if (hKey)
196  RegCloseKey(hKey);
197  HeapFree(GetProcessHeap(), 0, *lpAddress);
198  return FALSE;
199 }
200 
201 
202 /* Request the time from the current NTP server */
203 static ULONG
205 {
206  LPWSTR lpAddress = NULL;
207  ULONG ulTime = 0;
208 
209  if (GetNTPServerAddress(&lpAddress))
210  {
211  ulTime = GetServerTime(lpAddress);
212 
214  0,
215  lpAddress);
216  }
217 
218  return ulTime;
219 }
220 
221 /*
222  * NTP servers state the number of seconds passed since
223  * 1st Jan, 1900. The time returned from the server
224  * needs adding to that date to get the current Gregorian time
225  */
226 static VOID
228 {
229  FILETIME ftNew;
230  LARGE_INTEGER li;
231  SYSTEMTIME stNew;
232 
233  /* Time at 1st Jan 1900 */
234  stNew.wYear = 1900;
235  stNew.wMonth = 1;
236  stNew.wDay = 1;
237  stNew.wHour = 0;
238  stNew.wMinute = 0;
239  stNew.wSecond = 0;
240  stNew.wMilliseconds = 0;
241 
242  /* Convert to a file time */
243  if (!SystemTimeToFileTime(&stNew, &ftNew))
244  {
246  return;
247  }
248 
249  /* Add on the time passed since 1st Jan 1900 */
250  li = *(LARGE_INTEGER *)&ftNew;
251  li.QuadPart += (LONGLONG)10000000 * ulTime;
252  ftNew = * (FILETIME *)&li;
253 
254  /* Convert back to a system time */
255  if (!FileTimeToSystemTime(&ftNew, &stNew))
256  {
258  return;
259  }
260 
261  /* Use SystemSetTime with SystemTime = TRUE to set System Time */
262  if (!SystemSetTime(&stNew, TRUE))
264 }
265 
266 
267 static VOID
269 {
270  BOOL bChecked;
271  UINT uCheck;
272 
274  bChecked = (uCheck == BST_CHECKED) ? TRUE : FALSE;
275 
281 }
282 
283 
284 static VOID
286 {
287  HKEY hKey;
288  WCHAR szData[8];
289  DWORD dwSize;
290 
292  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Parameters",
293  0,
295  &hKey) == ERROR_SUCCESS)
296  {
297  dwSize = 8 * sizeof(WCHAR);
298  if (RegQueryValueExW(hKey,
299  L"Type",
300  NULL,
301  NULL,
302  (LPBYTE)szData,
303  &dwSize) == ERROR_SUCCESS)
304  {
305  if (wcscmp(szData, L"NTP") == 0)
307  }
308 
309  RegCloseKey(hKey);
310  }
311 }
312 
313 
314 static VOID
316 {
320 }
321 
322 
323 /* Property page dialog callback */
326  UINT uMsg,
327  WPARAM wParam,
328  LPARAM lParam)
329 {
330  switch (uMsg)
331  {
332  case WM_INITDIALOG:
333  OnInitDialog(hwndDlg);
334  break;
335 
336  case WM_COMMAND:
337  switch(LOWORD(wParam))
338  {
339  case IDC_UPDATEBUTTON:
340  {
341  ULONG ulTime;
342 
343  SetNTPServer(hwndDlg);
344 
345  ulTime = GetTimeFromServer();
346  if (ulTime != 0)
347  UpdateSystemTime(ulTime);
348  }
349  break;
350 
351  case IDC_SERVERLIST:
352  if (HIWORD(wParam) == CBN_SELCHANGE)
353  {
354  /* Enable the 'Apply' button */
355  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
356  }
357  break;
358 
359  case IDC_AUTOSYNC:
360  if (HIWORD(wParam) == BN_CLICKED)
361  {
362  EnableDialogText(hwndDlg);
363 
364  /* Enable the 'Apply' button */
365  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
366  }
367  break;
368  }
369  break;
370 
371  case WM_DESTROY:
372  break;
373 
374  case WM_NOTIFY:
375  {
376  LPNMHDR lpnm = (LPNMHDR)lParam;
377 
378  switch (lpnm->code)
379  {
380  case PSN_APPLY:
381  SetNTPServer(hwndDlg);
382  return TRUE;
383 
384  default:
385  break;
386  }
387  }
388  break;
389  }
390 
391  return FALSE;
392 }
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define TRUE
Definition: types.h:120
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define IDC_AUTOSYNC
Definition: resource.h:24
static VOID SetNTPServer(HWND hwnd)
Definition: internettime.c:89
WORD wMonth
Definition: winbase.h:871
#define IDC_NEXTSYNC
Definition: resource.h:26
_Check_return_ _CRTIMP int __cdecl _wtoi(_In_z_ const wchar_t *_Str)
static const WCHAR szData[]
Definition: msipriv.h:1195
#define CALLBACK
Definition: compat.h:27
static VOID UpdateSystemTime(ULONG ulTime)
Definition: internettime.c:227
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define IDC_SUCSYNC
Definition: resource.h:25
UINT_PTR WPARAM
Definition: windef.h:207
_CRTIMP wchar_t *__cdecl _itow(_In_ int _Value, _Pre_notnull_ _Post_z_ wchar_t *_Dest, _In_ int _Radix)
#define PSN_APPLY
Definition: prsht.h:117
int32_t INT_PTR
Definition: typedefs.h:62
#define IDC_SERVERTEXT
Definition: resource.h:27
#define CB_GETCURSEL
Definition: winuser.h:1904
WPARAM wParam
Definition: combotst.c:138
BOOL SystemSetTime(LPSYSTEMTIME lpSystemTime, BOOL SystemTime)
Definition: dateandtime.c:17
struct tagNMHDR * LPNMHDR
UINT code
Definition: winuser.h:3112
VOID DisplayWin32Error(DWORD dwErrorCode)
Definition: timedate.c:28
WORD wYear
Definition: winbase.h:870
static VOID EnableDialogText(HWND hwnd)
Definition: internettime.c:268
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
static VOID CreateNTPServerList(HWND hwnd)
Definition: internettime.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
INT_PTR CALLBACK InetTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: internettime.c:325
static BOOL GetNTPServerAddress(LPWSTR *lpAddress)
Definition: internettime.c:135
#define CB_ADDSTRING
Definition: winuser.h:1897
WORD wMinute
Definition: winbase.h:875
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2867
smooth NULL
Definition: ftsmooth.c:416
#define MAX_VALUE_NAME
Definition: control.c:23
LONG_PTR LPARAM
Definition: windef.h:208
#define BM_GETCHECK
Definition: winuser.h:1879
#define WM_DESTROY
Definition: winuser.h:1591
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:4917
int64_t LONGLONG
Definition: typedefs.h:66
#define BM_SETCHECK
Definition: winuser.h:1882
BOOL WINAPI FileTimeToSystemTime(IN CONST FILETIME *lpFileTime, OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:178
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4134
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define PropSheet_Changed(d, w)
Definition: prsht.h:326
#define IDC_UPDATEBUTTON
Definition: resource.h:23
#define BN_CLICKED
Definition: winuser.h:1886
unsigned long DWORD
Definition: ntddk_ex.h:95
LRESULT WINAPI SendDlgItemMessageW(_In_ HWND, _In_ int, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
WORD wSecond
Definition: winbase.h:876
WORD wMilliseconds
Definition: winbase.h:877
static const WCHAR L[]
Definition: oid.c:1250
#define IDC_SERVERLIST
Definition: resource.h:22
#define CB_SETCURSEL
Definition: winuser.h:1922
#define WM_COMMAND
Definition: winuser.h:1716
#define ERROR_MORE_DATA
Definition: dderror.h:13
BOOL WINAPI EnableWindow(_In_ HWND, _In_ BOOL)
HWND hList
Definition: livecd.c:10
WORD wDay
Definition: winbase.h:873
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
HWND WINAPI GetParent(_In_ HWND)
BOOL WINAPI SystemTimeToFileTime(IN CONST SYSTEMTIME *lpSystemTime, OUT LPFILETIME lpFileTime)
Definition: time.c:148
static VOID GetSyncSetting(HWND hwnd)
Definition: internettime.c:285
ULONG GetServerTime(LPWSTR lpAddress)
Definition: ntpclient.c:140
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
WORD wHour
Definition: winbase.h:874
unsigned int UINT
Definition: ndis.h:50
#define CBN_SELCHANGE
Definition: winuser.h:1940
#define BST_CHECKED
Definition: winuser.h:197
#define HIWORD(l)
Definition: typedefs.h:246
unsigned int ULONG
Definition: retypes.h:1
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3381
WCHAR * LPWSTR
Definition: xmlstorage.h:184
static VOID OnInitDialog(HWND hwnd)
Definition: internettime.c:315
#define WM_INITDIALOG
Definition: winuser.h:1715
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
static ULONG GetTimeFromServer(VOID)
Definition: internettime.c:204
#define WM_NOTIFY
Definition: richedit.h:61
LONGLONG QuadPart
Definition: typedefs.h:112
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define REG_SZ
Definition: layer.c:22