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.

*  Just call RtlGetVersion() once, when needed, as that is all that is needed.
   We could re-use the same function once to compare what we got when we
   called RtlGetVersion() and do what is necessary there.
This commit is contained in:
Chun-wei Fan 2021-11-19 11:50:26 +08:00
parent 674966bb6e
commit 045b4b80df
2 changed files with 44 additions and 23 deletions

View File

@ -39,6 +39,7 @@ void g_console_win32_init (void);
void g_clock_win32_init (void); void g_clock_win32_init (void);
void g_crash_handler_win32_init (void); void g_crash_handler_win32_init (void);
void g_crash_handler_win32_deinit (void); void g_crash_handler_win32_deinit (void);
gboolean _g_win32_call_rtl_version (OSVERSIONINFOEXW *info);
extern HMODULE glib_dll; extern HMODULE glib_dll;
#endif #endif

View File

@ -491,6 +491,48 @@ G_GNUC_END_IGNORE_DEPRECATIONS
return dirname; 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)
{
static OSVERSIONINFOEXW result;
static gsize inited = 0;
g_return_val_if_fail (info != NULL, FALSE);
if (g_once_init_enter (&inited))
{
#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
memset (&result, 0, sizeof (OSVERSIONINFOEXW));
result.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
RtlGetVersion (&result);
#if WINAPI_FAMILY != MODERN_API_FAMILY
FreeLibrary (hmodule);
#endif
g_once_init_leave (&inited, TRUE);
}
*info = result;
return TRUE;
}
/** /**
* g_win32_check_windows_version: * g_win32_check_windows_version:
* @major: major version of Windows * @major: major version of Windows
@ -526,31 +568,13 @@ g_win32_check_windows_version (const gint major,
gboolean is_ver_checked = FALSE; gboolean is_ver_checked = FALSE;
gboolean is_type_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 */ /* 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 && (major <= 6 || major == 10), FALSE);
g_return_val_if_fail ((major >= 5 && minor >= 1) || major >= 6, FALSE); g_return_val_if_fail ((major >= 5 && minor >= 1) || major >= 6, FALSE);
/* Check for Service Pack Version >= 0 */ /* Check for Service Pack Version >= 0 */
g_return_val_if_fail (spver >= 0, FALSE); g_return_val_if_fail (spver >= 0, FALSE);
g_return_val_if_fail (_g_win32_call_rtl_version (&osverinfo), 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);
/* check the OS and Service Pack Versions */ /* check the OS and Service Pack Versions */
if (osverinfo.dwMajorVersion > (DWORD) major) if (osverinfo.dwMajorVersion > (DWORD) major)
@ -588,10 +612,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; return is_ver_checked && is_type_checked;
} }