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 5899bdb525967..0000000000000 --- 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 049a68ca446b1..0000000000000 --- 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 17f0521504a65..d01a65913e86a 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 static unsafe int RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi) 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 4d45110d37908..559cca7f20346 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 7b306a46df9ca..b5a654a61e63b 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 e4ac0a0e83211..6a7bd32e4ccce 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 @@ private static string ExpandEnvironmentVariablesCore(string name) 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 4b8fb9f1a6d2b..4536cd4853ea0 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 @@ -31,14 +31,6 @@ Common\Interop\Unix\Interop.Libraries.cs - - - Common\Interop\Windows\NtDll\Interop.RtlGetVersion.cs - - - Common\Interop\Windows\NtDll\Interop.RTL_OSVERSIONINFOEX.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 bc733dfa47bed..c5818719918e6 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 @@ public static bool IsOSPlatform(OSPlatform osPlatform) 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 {