[glib/improve-win32-version] gwin32.c: Split out call to RtlGetVersion()




commit 74fca8d83ea87fd3ea3898e080f8167ff8f66a20
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Fri Nov 19 11:50:26 2021 +0800

    gwin32.c: Split out call to RtlGetVersion()
    
    Unfortunately, we may well be likely to need to call RtlGetVersion() via
    GetModuleHandle() + GetProcAddress(), so split out the call to RtlGetVersion()
    into a private function of its own, so that we can reuse the same code in other
    parts of GLib, so that we can:
    
    *  Determine better in a more fine-tuned way to determine whether we are on
       Windows 10/11 and/or Server 2016/2019/2022, since we need to rely on the
       build number.

 glib/gwin32.c | 49 +++++++++++++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 20 deletions(-)
---
diff --git a/glib/gwin32.c b/glib/gwin32.c
index 30c60575e..6636f9cca 100644
--- a/glib/gwin32.c
+++ b/glib/gwin32.c
@@ -491,6 +491,34 @@ G_GNUC_END_IGNORE_DEPRECATIONS
   return dirname;
 }
 
+/*
+ * private API to call Windows's RtlGetVersion(), which may need to be called
+ * via GetProcAddress()
+ */
+gboolean
+_g_win32_call_rtl_version (OSVERSIONINFOEXW *info)
+{
+#if WINAPI_FAMILY != MODERN_API_FAMILY
+  /* For non-modern UI Apps, use the LoadLibraryW()/GetProcAddress() thing */
+  typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
+
+  fRtlGetVersion *RtlGetVersion;
+  HMODULE hmodule = LoadLibraryW (L"ntdll.dll");
+  g_return_val_if_fail (hmodule != NULL, FALSE);
+
+  RtlGetVersion = (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
+  g_return_val_if_fail (RtlGetVersion != NULL, FALSE);
+#endif
+
+  RtlGetVersion (info);
+
+#if WINAPI_FAMILY != MODERN_API_FAMILY
+  FreeLibrary (hmodule);
+#endif
+
+  return TRUE;
+}
+
 /**
  * g_win32_check_windows_version:
  * @major: major version of Windows
@@ -526,13 +554,6 @@ g_win32_check_windows_version (const gint major,
   gboolean is_ver_checked = FALSE;
   gboolean is_type_checked = FALSE;
 
-#if WINAPI_FAMILY != MODERN_API_FAMILY
-  /* For non-modern UI Apps, use the LoadLibraryW()/GetProcAddress() thing */
-  typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
-
-  fRtlGetVersion *RtlGetVersion;
-  HMODULE hmodule;
-#endif
   /* We Only Support Checking for XP or later */
   g_return_val_if_fail (major >= 5 && (major <= 6 || major == 10), FALSE);
   g_return_val_if_fail ((major >= 5 && minor >= 1) || major >= 6, FALSE);
@@ -540,17 +561,9 @@ g_win32_check_windows_version (const gint major,
   /* Check for Service Pack Version >= 0 */
   g_return_val_if_fail (spver >= 0, FALSE);
 
-#if WINAPI_FAMILY != MODERN_API_FAMILY
-  hmodule = LoadLibraryW (L"ntdll.dll");
-  g_return_val_if_fail (hmodule != NULL, FALSE);
-
-  RtlGetVersion = (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
-  g_return_val_if_fail (RtlGetVersion != NULL, FALSE);
-#endif
-
   memset (&osverinfo, 0, sizeof (OSVERSIONINFOEXW));
   osverinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
-  RtlGetVersion (&osverinfo);
+  g_return_val_if_fail (_g_win32_call_rtl_version (&osverinfo), FALSE);
 
   /* check the OS and Service Pack Versions */
   if (osverinfo.dwMajorVersion > (DWORD) major)
@@ -588,10 +601,6 @@ g_win32_check_windows_version (const gint major,
         }
     }
 
-#if WINAPI_FAMILY != MODERN_API_FAMILY
-  FreeLibrary (hmodule);
-#endif
-
   return is_ver_checked && is_type_checked;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]