-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update OS version detection to get version directly from Windows
- Loading branch information
Showing
3 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#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 NUnit.Framework; | ||
using OperatingSystem = Grpc.Net.Client.Internal.OperatingSystem; | ||
|
||
namespace Grpc.Net.Client.Tests; | ||
|
||
public class OperatingSystemTests | ||
{ | ||
#if NET5_0_OR_GREATER | ||
[Test] | ||
public void OSVersion_ModernDotNet_MatchesEnvironment() | ||
{ | ||
// Environment.OSVersion and OperatingSystem.OSVersion should match in .NET 5 and greater. | ||
Assert.AreEqual(Environment.OSVersion.Version, OperatingSystem.Instance.OSVersion); | ||
} | ||
#endif | ||
} |