From 1b6f76d11ea2c48e92b4239da67fc820945fc607 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Thu, 16 Jun 2022 21:06:52 +0300 Subject: [PATCH] Simplify `Environment.IsWindows8OrAbove`. (#70818) * Simplify Environment.IsWindows8OrAbove. Since .NET 5, the regular Windows version detecton code always returns the correct version. * Remove two unused interop files. --- .../Kernel32/Interop.VerSetConditionMask.cs | 13 --------- .../Kernel32/Interop.VerifyVersionExW.cs | 20 -------------- .../System.Private.CoreLib.Shared.projitems | 6 ----- .../src/System/Environment.Win32.cs | 27 ++----------------- 4 files changed, 2 insertions(+), 64 deletions(-) delete mode 100644 src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs delete mode 100644 src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs deleted file mode 100644 index 791124d11839fe..00000000000000 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.InteropServices; - -internal static partial class Interop -{ - internal static partial class Kernel32 - { - [LibraryImport(Libraries.Kernel32)] - internal static partial ulong VerSetConditionMask(ulong ConditionMask, uint TypeMask, byte Condition); - } -} diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs deleted file mode 100644 index aa4536511e1b46..00000000000000 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.InteropServices; - -internal static partial class Interop -{ - internal static partial class Kernel32 - { - internal const byte VER_GREATER_EQUAL = 0x3; - internal const uint VER_MAJORVERSION = 0x0000002; - internal const uint VER_MINORVERSION = 0x0000001; - internal const uint VER_SERVICEPACKMAJOR = 0x0000020; - internal const uint VER_SERVICEPACKMINOR = 0x0000010; - - [LibraryImport(Libraries.Kernel32)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static partial bool VerifyVersionInfoW(ref OSVERSIONINFOEX lpVersionInfo, uint dwTypeMask, ulong dwlConditionMask); - } -} diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index ebdb96165b0116..d0040d54427692 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -1756,12 +1756,6 @@ Common\Interop\Windows\Kernel32\Interop.TzSpecificLocalTimeToSystemTime.cs - - Common\Interop\Windows\Kernel32\Interop.VerifyVersionExW.cs - - - Common\Interop\Windows\Kernel32\Interop.VerSetConditionMask.cs - Common\Interop\Windows\Kernel32\Interop.VirtualAlloc_Ptr.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs index 07af75661e9c5a..032f4b16ad3b6d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs @@ -4,7 +4,6 @@ using System.Collections; using System.Diagnostics; using System.IO; -using System.Reflection; using System.Runtime.InteropServices; using System.Text; using Internal.Win32; @@ -383,30 +382,8 @@ private static string GetKnownFolderPath(string folderGuid, SpecialFolderOption private static class WindowsVersion { // Cache the value in static readonly that can be optimized out by the JIT - internal static readonly bool IsWindows8OrAbove = GetIsWindows8OrAbove(); - - private static bool GetIsWindows8OrAbove() - { - ulong conditionMask = Interop.Kernel32.VerSetConditionMask(0, Interop.Kernel32.VER_MAJORVERSION, Interop.Kernel32.VER_GREATER_EQUAL); - conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, Interop.Kernel32.VER_MINORVERSION, Interop.Kernel32.VER_GREATER_EQUAL); - conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, Interop.Kernel32.VER_SERVICEPACKMAJOR, Interop.Kernel32.VER_GREATER_EQUAL); - conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, Interop.Kernel32.VER_SERVICEPACKMINOR, Interop.Kernel32.VER_GREATER_EQUAL); - - // Windows 8 version is 6.2 - Interop.Kernel32.OSVERSIONINFOEX version = default; - unsafe - { - version.dwOSVersionInfoSize = sizeof(Interop.Kernel32.OSVERSIONINFOEX); - } - version.dwMajorVersion = 6; - version.dwMinorVersion = 2; - version.wServicePackMajor = 0; - version.wServicePackMinor = 0; - - return Interop.Kernel32.VerifyVersionInfoW(ref version, - Interop.Kernel32.VER_MAJORVERSION | Interop.Kernel32.VER_MINORVERSION | Interop.Kernel32.VER_SERVICEPACKMAJOR | Interop.Kernel32.VER_SERVICEPACKMINOR, - conditionMask); - } + // Windows 8 version is 6.2 + internal static readonly bool IsWindows8OrAbove = OperatingSystem.IsWindowsVersionAtLeast(6, 2); } } }