Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/Grpc.Net.Client/Internal/NtDll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

#if !NET5_0_OR_GREATER

using System.Runtime.InteropServices;

namespace Grpc.Net.Client.Internal;

internal static class NtDll
{
[DllImport("ntdll.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern NTSTATUS RtlGetVersion(ref OSVERSIONINFOEX versionInfo);

internal static Version DetectWindowsVersion()
{
var osVersionInfo = new OSVERSIONINFOEX { OSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) };

if (RtlGetVersion(ref osVersionInfo) != NTSTATUS.STATUS_SUCCESS)
{
throw new InvalidOperationException($"Failed to call internal {nameof(RtlGetVersion)}.");
}

return new Version(osVersionInfo.MajorVersion, osVersionInfo.MinorVersion, osVersionInfo.BuildNumber, 0);
}

internal enum NTSTATUS : uint
{
/// <summary>
/// The operation completed successfully.
/// </summary>
STATUS_SUCCESS = 0x00000000
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct OSVERSIONINFOEX
{
// The OSVersionInfoSize field must be set to Marshal.SizeOf(typeof(OSVERSIONINFOEX))
public int OSVersionInfoSize;
public int MajorVersion;
public int MinorVersion;
public int BuildNumber;
public int PlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string CSDVersion;
public ushort ServicePackMajor;
public ushort ServicePackMinor;
public short SuiteMask;
public byte ProductType;
public byte Reserved;
}
}

#endif
16 changes: 13 additions & 3 deletions src/Grpc.Net.Client/Internal/OperatingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ internal sealed class OperatingSystem : IOperatingSystem

private OperatingSystem()
{
IsBrowser = RuntimeInformation.IsOSPlatform(OSPlatform.Create("browser"));
#if NET5_0_OR_GREATER
IsAndroid = System.OperatingSystem.IsAndroid();
IsWindows = System.OperatingSystem.IsWindows();
IsBrowser = System.OperatingSystem.IsBrowser();
OSVersion = Environment.OSVersion.Version;
#else
IsAndroid = false;
#endif
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
OSVersion = Environment.OSVersion.Version;
IsBrowser = RuntimeInformation.IsOSPlatform(OSPlatform.Create("browser"));

// Older versions of .NET report an OSVersion.Version based on Windows compatibility settings.
// For example, if an app running on Windows 11 is configured to be "compatible" with Windows 10
// then the version returned is always Windows 10.
//
// Get correct Windows version directly from Windows by calling RtlGetVersion.
// https://www.pinvoke.net/default.aspx/ntdll/RtlGetVersion.html
OSVersion = IsWindows ? NtDll.DetectWindowsVersion() : Environment.OSVersion.Version;
#endif
}
}
43 changes: 43 additions & 0 deletions test/Grpc.Net.Client.Tests/OperatingSystemTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using Grpc.Net.Client.Internal;
using NUnit.Framework;
using OperatingSystem = Grpc.Net.Client.Internal.OperatingSystem;

namespace Grpc.Net.Client.Tests;

public class OperatingSystemTests
{
#if !NET5_0_OR_GREATER
[Test]
[Platform("Windows", Reason = "Only runs on Windows where ntdll.dll is present.")]
public void DetectWindowsVersion_Windows_MatchesEnvironment()
{
// It is safe to compare Environment.OSVersion.Version on netfx because tests have no compatibilty setting.
Assert.AreEqual(Environment.OSVersion.Version, NtDll.DetectWindowsVersion());
}
#endif

[Test]
public void OSVersion_ModernDotNet_MatchesEnvironment()
{
// It is safe to compare Environment.OSVersion.Version on netfx because tests have no compatibilty setting.
Assert.AreEqual(Environment.OSVersion.Version, OperatingSystem.Instance.OSVersion);
}
}