From 136f8fb55037a06e7e3ce9f09fa786099d87745b Mon Sep 17 00:00:00 2001 From: Rod Widdowson Date: Sat, 3 Mar 2018 15:22:55 +0000 Subject: [PATCH] Windows: Avoid using deprecated function. In VC15 GetVersionEx has been deprecated. In order to continue to support Win2K use the undeprecated VerifyVersionInfoW API (available since Win2K). Inline helper functions used in latest Win10 SDK to simplify code. --- lib/roken/rand.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/roken/rand.c b/lib/roken/rand.c index 03239d7dc..148074b52 100644 --- a/lib/roken/rand.c +++ b/lib/roken/rand.c @@ -36,6 +36,33 @@ #ifdef HAVE_WIN32_RAND_S static int hasRand_s = 1; + +// The Following is BackPorted from VersionHelpers.h in the 10.0.15063.0 SDK +static FORCEINLINE BOOL +IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor) +{ + OSVERSIONINFOEXW osvi ={ sizeof(osvi), 0, 0, 0, 0,{ 0 }, 0, 0 }; + DWORDLONG const dwlConditionMask = VerSetConditionMask( + VerSetConditionMask( + VerSetConditionMask( + 0, VER_MAJORVERSION, VER_GREATER_EQUAL), + VER_MINORVERSION, VER_GREATER_EQUAL), + VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); + + osvi.dwMajorVersion = wMajorVersion; + osvi.dwMinorVersion = wMinorVersion; + osvi.wServicePackMajor = wServicePackMajor; + + return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE; +} + +static FORCEINLINE BOOL +IsWindowsXPOrGreater() +{ + // Assume that _WIN32_WINNT_WINXP (0x0501) not available so use contants. + return IsWindowsVersionOrGreater(5, 1, 0); +} + #endif void ROKEN_LIB_FUNCTION @@ -49,13 +76,7 @@ rk_random_init(void) srandom(time(NULL)); #else # ifdef HAVE_WIN32_RAND_S - OSVERSIONINFO osInfo; - - osInfo.dwOSVersionInfoSize = sizeof(osInfo); - hasRand_s = - (GetVersionEx(&osInfo) - && ((osInfo.dwMajorVersion > 5) || - (osInfo.dwMajorVersion == 5) && (osInfo.dwMinorVersion >= 1))); + hasRand_s = IsWindowsXPOrGreater(); # endif srand (time(NULL)); #endif