From 70193ba78dc38ee250ace16d3bd0a69b2bbd1edc Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 20 Mar 2020 10:08:17 -0400 Subject: [PATCH] Change Environment.OSVersion to use RtlGetVersion (#33651) --- .../Windows/Kernel32/Interop.GetVersionExW.cs | 14 ------------- .../Windows/NtDll/Interop.RTL_OSVERSIONINFOEX.cs | 23 ---------------------- .../Interop/Windows/NtDll/Interop.RtlGetVersion.cs | 20 ++++++++----------- .../CoreFx.Private.TestUtilities.csproj | 3 --- .../src/System.Private.CoreLib.Shared.projitems | 6 +++--- .../src/System/Environment.Windows.cs | 12 +++++------ ...ntime.InteropServices.RuntimeInformation.csproj | 8 -------- .../RuntimeInformation.Windows.cs | 20 ++++++++++++++++++- 8 files changed, 36 insertions(+), 70 deletions(-) delete mode 100644 src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetVersionExW.cs delete mode 100644 src/libraries/Common/src/Interop/Windows/NtDll/Interop.RTL_OSVERSIONINFOEX.cs diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetVersionExW.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetVersionExW.cs deleted file mode 100644 index 5899bdb..0000000 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetVersionExW.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Runtime.InteropServices; - -internal static partial class Interop -{ - internal static partial class Kernel32 - { - [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)] - internal static extern bool GetVersionExW(ref OSVERSIONINFOEX osvi); - } -} diff --git a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RTL_OSVERSIONINFOEX.cs b/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RTL_OSVERSIONINFOEX.cs deleted file mode 100644 index 049a68c..0000000 --- a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RTL_OSVERSIONINFOEX.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; - -internal partial class Interop -{ - internal partial class NtDll - { - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - internal unsafe struct RTL_OSVERSIONINFOEX - { - internal uint dwOSVersionInfoSize; - internal uint dwMajorVersion; - internal uint dwMinorVersion; - internal uint dwBuildNumber; - internal uint dwPlatformId; - internal fixed char szCSDVersion[128]; - } - } -} diff --git a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs b/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs index 17f0521..d01a659 100644 --- a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs +++ b/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs @@ -18,19 +18,15 @@ internal partial class Interop return RtlGetVersion(ref osvi); } - internal static unsafe string RtlGetVersion() + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + internal unsafe struct RTL_OSVERSIONINFOEX { - const string Version = "Microsoft Windows"; - if (RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi) == 0) - { - return osvi.szCSDVersion[0] != '\0' ? - string.Format("{0} {1}.{2}.{3} {4}", Version, osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber, new string(&(osvi.szCSDVersion[0]))) : - string.Format("{0} {1}.{2}.{3}", Version, osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber); - } - else - { - return Version; - } + internal uint dwOSVersionInfoSize; + internal uint dwMajorVersion; + internal uint dwMinorVersion; + internal uint dwBuildNumber; + internal uint dwPlatformId; + internal fixed char szCSDVersion[128]; } } } diff --git a/src/libraries/Common/tests/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.csproj b/src/libraries/Common/tests/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.csproj index 4d45110..559cca7 100644 --- a/src/libraries/Common/tests/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.csproj +++ b/src/libraries/Common/tests/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.csproj @@ -35,9 +35,6 @@ Common\Interop\Windows\Kernel32\Interop.GetCurrentProcess_IntPtr.cs - - Common\Interop\Windows\NtDll\Interop.RTL_OSVERSIONINFOEX.cs - Common\Interop\Windows\NtDll\Interop.RtlGetVersion.cs 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 7b306a4..b5a654a 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 @@ -1293,9 +1293,6 @@ Common\Interop\Windows\Kernel32\Interop.GetTempPathW.cs - - Common\Interop\Windows\Kernel32\Interop.GetVersionExW.cs - Common\Interop\Windows\Kernel32\Interop.Globalization.cs @@ -1422,6 +1419,9 @@ Common\Interop\Windows\NtDll\Interop.NtQuerySystemInformation.cs + + Common\Interop\Windows\NtDll\Interop.RtlGetVersion.cs + Common\Interop\Windows\NtDll\Interop.SYSTEM_LEAP_SECOND_INFORMATION.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs index e4ac0a0..6a7bd32 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs @@ -89,16 +89,16 @@ namespace System private static unsafe OperatingSystem GetOSVersion() { - var version = new Interop.Kernel32.OSVERSIONINFOEX { dwOSVersionInfoSize = sizeof(Interop.Kernel32.OSVERSIONINFOEX) }; - if (!Interop.Kernel32.GetVersionExW(ref version)) + if (Interop.NtDll.RtlGetVersionEx(out Interop.NtDll.RTL_OSVERSIONINFOEX osvi) != 0) { throw new InvalidOperationException(SR.InvalidOperation_GetVersion); } - return new OperatingSystem( - PlatformID.Win32NT, - new Version(version.dwMajorVersion, version.dwMinorVersion, version.dwBuildNumber, (version.wServicePackMajor << 16) | version.wServicePackMinor), - Marshal.PtrToStringUni((IntPtr)version.szCSDVersion)); + var version = new Version((int)osvi.dwMajorVersion, (int)osvi.dwMinorVersion, (int)osvi.dwBuildNumber, 0); + + return osvi.szCSDVersion[0] != '\0' ? + new OperatingSystem(PlatformID.Win32NT, version, new string(&osvi.szCSDVersion[0])) : + new OperatingSystem(PlatformID.Win32NT, version); } public static string SystemDirectory diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj index 4b8fb9f..4536cd4 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj @@ -32,14 +32,6 @@ - - Common\Interop\Windows\NtDll\Interop.RtlGetVersion.cs - - - Common\Interop\Windows\NtDll\Interop.RTL_OSVERSIONINFOEX.cs - - - Common\Interop\Windows\Interop.Libraries.cs diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs index bc733df..c581871 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs @@ -19,7 +19,25 @@ namespace System.Runtime.InteropServices return OSPlatform.Windows == osPlatform; } - public static string OSDescription => s_osDescription ??= Interop.NtDll.RtlGetVersion(); + public static string OSDescription + { + get + { + string? osDescription = s_osDescription; + if (osDescription is null) + { + OperatingSystem os = Environment.OSVersion; + Version v = os.Version; + + const string Version = "Microsoft Windows"; + s_osDescription = osDescription = string.IsNullOrEmpty(os.ServicePack) ? + $"{Version} {(uint)v.Major}.{(uint)v.Minor}.{(uint)v.Build}" : + $"{Version} {(uint)v.Major}.{(uint)v.Minor}.{(uint)v.Build} {os.ServicePack}"; + } + + return osDescription; + } + } public static Architecture OSArchitecture { -- 2.7.4