Skip to content

Commit

Permalink
Change Environment.OSVersion to use RtlGetVersion (dotnet#33651)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Mar 20, 2020
1 parent c9edf7e commit 70193ba
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 70 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetCurrentProcess_IntPtr.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetCurrentProcess_IntPtr.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.RTL_OSVERSIONINFOEX.cs">
<Link>Common\Interop\Windows\NtDll\Interop.RTL_OSVERSIONINFOEX.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.RtlGetVersion.cs">
<Link>Common\Interop\Windows\NtDll\Interop.RtlGetVersion.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,9 +1293,6 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetTempPathW.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetTempPathW.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetVersionExW.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetVersionExW.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.Globalization.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.Globalization.cs</Link>
</Compile>
Expand Down Expand Up @@ -1422,6 +1419,9 @@
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.NtQuerySystemInformation.cs">
<Link>Common\Interop\Windows\NtDll\Interop.NtQuerySystemInformation.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.RtlGetVersion.cs">
<Link>Common\Interop\Windows\NtDll\Interop.RtlGetVersion.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.SYSTEM_LEAP_SECOND_INFORMATION.cs">
<Link>Common\Interop\Windows\NtDll\Interop.SYSTEM_LEAP_SECOND_INFORMATION.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@
<Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.RtlGetVersion.cs">
<Link>Common\Interop\Windows\NtDll\Interop.RtlGetVersion.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.RTL_OSVERSIONINFOEX.cs">
<Link>Common\Interop\Windows\NtDll\Interop.RTL_OSVERSIONINFOEX.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="System\Runtime\InteropServices\RuntimeInformation\RuntimeInformation.Windows.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 70193ba

Please sign in to comment.