[TIMEDATE] Avoid the use of DisplayWin32Error in time sync utility functions. CORE...
authorThomas Faber <thomas.faber@reactos.org>
Sun, 3 Jun 2018 13:35:42 +0000 (15:35 +0200)
committerThomas Faber <thomas.faber@reactos.org>
Sun, 7 Jul 2019 10:44:51 +0000 (12:44 +0200)
dll/cpl/timedate/internettime.c
dll/cpl/timedate/timedate.h
dll/cpl/timedate/w32time.c

index 5a419e4..7c97184 100644 (file)
@@ -204,8 +204,15 @@ InetTimePageProc(HWND hwndDlg,
             {
                 case IDC_UPDATEBUTTON:
                 {
             {
                 case IDC_UPDATEBUTTON:
                 {
+                    DWORD dwError;
+
                     SetNTPServer(hwndDlg);
                     SetNTPServer(hwndDlg);
-                    SyncTimeNow();
+
+                    dwError = SyncTimeNow();
+                    if (dwError != ERROR_SUCCESS)
+                    {
+                        DisplayWin32Error(dwError);
+                    }
                 }
                 break;
 
                 }
                 break;
 
index 2fdddbd..73b2a3a 100644 (file)
@@ -95,7 +95,7 @@ ULONG GetServerTime(LPWSTR lpAddress);
 
 
 /* w32time.c */
 
 
 /* w32time.c */
-VOID SyncTimeNow(VOID);
+DWORD SyncTimeNow(VOID);
 
 
 /* monthcal.c */
 
 
 /* monthcal.c */
index a8aed3f..dfbf143 100644 (file)
@@ -8,7 +8,7 @@
 #include "timedate.h"
 
 /* Get the domain name from the registry */
 #include "timedate.h"
 
 /* Get the domain name from the registry */
-static BOOL
+static DWORD
 GetNTPServerAddress(LPWSTR *lpAddress)
 {
     HKEY hKey;
 GetNTPServerAddress(LPWSTR *lpAddress)
 {
     HKEY hKey;
@@ -16,13 +16,16 @@ GetNTPServerAddress(LPWSTR *lpAddress)
     DWORD dwSize;
     LONG lRet;
 
     DWORD dwSize;
     LONG lRet;
 
+    *lpAddress = NULL;
+    hKey = NULL;
+
     lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
                          L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
                          0,
                          KEY_QUERY_VALUE,
                          &hKey);
     if (lRet != ERROR_SUCCESS)
     lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
                          L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
                          0,
                          KEY_QUERY_VALUE,
                          &hKey);
     if (lRet != ERROR_SUCCESS)
-        goto fail;
+        goto Exit;
 
     /* Get data from default value */
     dwSize = 4 * sizeof(WCHAR);
 
     /* Get data from default value */
     dwSize = 4 * sizeof(WCHAR);
@@ -33,7 +36,7 @@ GetNTPServerAddress(LPWSTR *lpAddress)
                             (LPBYTE)szSel,
                             &dwSize);
     if (lRet != ERROR_SUCCESS)
                             (LPBYTE)szSel,
                             &dwSize);
     if (lRet != ERROR_SUCCESS)
-        goto fail;
+        goto Exit;
 
     dwSize = 0;
     lRet = RegQueryValueExW(hKey,
 
     dwSize = 0;
     lRet = RegQueryValueExW(hKey,
@@ -43,7 +46,7 @@ GetNTPServerAddress(LPWSTR *lpAddress)
                             NULL,
                             &dwSize);
     if (lRet != ERROR_SUCCESS)
                             NULL,
                             &dwSize);
     if (lRet != ERROR_SUCCESS)
-        goto fail;
+        goto Exit;
 
     (*lpAddress) = (LPWSTR)HeapAlloc(GetProcessHeap(),
                                      0,
 
     (*lpAddress) = (LPWSTR)HeapAlloc(GetProcessHeap(),
                                      0,
@@ -51,7 +54,7 @@ GetNTPServerAddress(LPWSTR *lpAddress)
     if ((*lpAddress) == NULL)
     {
         lRet = ERROR_NOT_ENOUGH_MEMORY;
     if ((*lpAddress) == NULL)
     {
         lRet = ERROR_NOT_ENOUGH_MEMORY;
-        goto fail;
+        goto Exit;
     }
 
     lRet = RegQueryValueExW(hKey,
     }
 
     lRet = RegQueryValueExW(hKey,
@@ -61,38 +64,42 @@ GetNTPServerAddress(LPWSTR *lpAddress)
                             (LPBYTE)*lpAddress,
                             &dwSize);
     if (lRet != ERROR_SUCCESS)
                             (LPBYTE)*lpAddress,
                             &dwSize);
     if (lRet != ERROR_SUCCESS)
-        goto fail;
-
-    RegCloseKey(hKey);
+        goto Exit;
 
 
-    return TRUE;
-
-fail:
-    DisplayWin32Error(lRet);
+Exit:
     if (hKey)
         RegCloseKey(hKey);
     if (hKey)
         RegCloseKey(hKey);
-    HeapFree(GetProcessHeap(), 0, *lpAddress);
-    return FALSE;
+    if (lRet != ERROR_SUCCESS)
+        HeapFree(GetProcessHeap(), 0, *lpAddress);
+
+    return lRet;
 }
 
 
 /* Request the time from the current NTP server */
 }
 
 
 /* Request the time from the current NTP server */
-static ULONG
-GetTimeFromServer(VOID)
+static DWORD
+GetTimeFromServer(PULONG pulTime)
 {
 {
-    LPWSTR lpAddress = NULL;
-    ULONG ulTime = 0;
+    LPWSTR lpAddress;
+    DWORD dwError;
 
 
-    if (GetNTPServerAddress(&lpAddress))
+    dwError = GetNTPServerAddress(&lpAddress);
+    if (dwError != ERROR_SUCCESS)
     {
     {
-        ulTime = GetServerTime(lpAddress);
+        return dwError;
+    }
 
 
-        HeapFree(GetProcessHeap(),
-                 0,
-                 lpAddress);
+    *pulTime = GetServerTime(lpAddress);
+    if (*pulTime == 0)
+    {
+        dwError = ERROR_GEN_FAILURE;
     }
 
     }
 
-    return ulTime;
+    HeapFree(GetProcessHeap(),
+             0,
+             lpAddress);
+
+    return dwError;
 }
 
 
 }
 
 
@@ -158,7 +165,7 @@ SystemSetTime(LPSYSTEMTIME lpSystemTime)
  * 1st Jan, 1900. The time returned from the server
  * needs adding to that date to get the current Gregorian time
  */
  * 1st Jan, 1900. The time returned from the server
  * needs adding to that date to get the current Gregorian time
  */
-static VOID
+static DWORD
 UpdateSystemTime(ULONG ulTime)
 {
     FILETIME ftNew;
 UpdateSystemTime(ULONG ulTime)
 {
     FILETIME ftNew;
@@ -177,8 +184,7 @@ UpdateSystemTime(ULONG ulTime)
     /* Convert to a file time */
     if (!SystemTimeToFileTime(&stNew, &ftNew))
     {
     /* Convert to a file time */
     if (!SystemTimeToFileTime(&stNew, &ftNew))
     {
-        DisplayWin32Error(GetLastError());
-        return;
+        return GetLastError();
     }
 
     /* Add on the time passed since 1st Jan 1900 */
     }
 
     /* Add on the time passed since 1st Jan 1900 */
@@ -189,23 +195,34 @@ UpdateSystemTime(ULONG ulTime)
     /* Convert back to a system time */
     if (!FileTimeToSystemTime(&ftNew, &stNew))
     {
     /* Convert back to a system time */
     if (!FileTimeToSystemTime(&ftNew, &stNew))
     {
-        DisplayWin32Error(GetLastError());
-        return;
+        return GetLastError();
     }
 
     if (!SystemSetTime(&stNew))
     {
     }
 
     if (!SystemSetTime(&stNew))
     {
-        DisplayWin32Error(GetLastError());
+        return GetLastError();
     }
     }
+
+    return ERROR_SUCCESS;
 }
 
 
 }
 
 
-VOID
+DWORD
 SyncTimeNow(VOID)
 {
 SyncTimeNow(VOID)
 {
+    DWORD dwError;
     ULONG ulTime;
     ULONG ulTime;
-    ulTime = GetTimeFromServer();
+    dwError = GetTimeFromServer(&ulTime);
+    if (dwError != ERROR_SUCCESS)
+    {
+        return dwError;
+    }
+
     if (ulTime != 0)
     if (ulTime != 0)
-        UpdateSystemTime(ulTime);
+    {
+        dwError = UpdateSystemTime(ulTime);
+    }
+
+    return dwError;
 }
 
 }