This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new environment apis proposed to
System.Runtime.InteropServices.RuntimeInformation.
- Loading branch information
Lakshmi Priya Sekar
committed
Nov 5, 2015
1 parent
6c106c3
commit 97d52c2
Showing
27 changed files
with
1,663 additions
and
42 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Common/src/Interop/Unix/System.Native/Interop.GetUnixArchitecture.cs
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,21 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[DllImport(Libraries.SystemNative, SetLastError = true)] | ||
internal static extern int GetUnixArchitecture(); | ||
|
||
internal enum ProcessorArchitecture | ||
{ | ||
x86, | ||
x64, | ||
ARM | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Common/src/Interop/Unix/System.Native/Interop.GetUnixVersion.cs
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,35 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[DllImport(Libraries.SystemNative, CharSet = CharSet.Ansi, SetLastError = true)] | ||
private static extern int GetUnixVersion(StringBuilder version, out int capacity); | ||
|
||
internal static string GetUnixVersion() | ||
{ | ||
// max value of _UTSNAME_LENGTH on known Unix platforms is 1024. | ||
const int _UTSNAME_LENGTH = 1024; | ||
int capacity = _UTSNAME_LENGTH * 3 + 2; | ||
StringBuilder version = new StringBuilder(capacity); | ||
|
||
if (GetUnixVersion(version, out capacity) != 0) | ||
{ | ||
// Check if the function failed due to insufficient buffer. | ||
if (capacity > version.Capacity) | ||
{ | ||
version.Capacity = capacity; | ||
GetUnixVersion(version, out capacity); | ||
} | ||
} | ||
|
||
return version.ToString(); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Common/src/Interop/Windows/mincore/Interop.GetNativeSystemInfo.cs
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,14 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class mincore | ||
{ | ||
[DllImport(Libraries.SystemInfo)] | ||
internal extern static void GetNativeSystemInfo(out SYSTEM_INFO lpSystemInfo); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/Common/src/Interop/Windows/mincore/Interop.OSVERSIONINFOEX.cs
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,28 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class mincore | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct OSVERSIONINFOEX | ||
{ | ||
internal uint dwOSVersionInfoSize; | ||
internal uint dwMajorVersion; | ||
internal uint dwMinorVersion; | ||
internal uint dwBuildNumber; | ||
internal uint dwPlatformId; | ||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] | ||
internal string szCSDVersion; | ||
internal ushort wServicePackMajor; | ||
internal ushort wServicePackMinor; | ||
internal ushort wSuiteMask; | ||
internal byte wProductType; | ||
internal byte wReserved; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Common/src/Interop/Windows/mincore/Interop.SYSTEM_INFO.cs
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,36 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class mincore | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct SYSTEM_INFO | ||
{ | ||
internal ushort wProcessorArchitecture; | ||
internal ushort wReserved; | ||
internal int dwPageSize; | ||
internal IntPtr lpMinimumApplicationAddress; | ||
internal IntPtr lpMaximumApplicationAddress; | ||
internal IntPtr dwActiveProcessorMask; | ||
internal int dwNumberOfProcessors; | ||
internal int dwProcessorType; | ||
internal int dwAllocationGranularity; | ||
internal short wProcessorLevel; | ||
internal short wProcessorRevision; | ||
} | ||
|
||
internal enum ProcessorArchitecture : ushort | ||
{ | ||
Processor_Architecture_INTEL = 0, | ||
Processor_Architecture_ARM = 5, | ||
Processor_Architecture_IA64 = 6, | ||
Processor_Architecture_AMD64 = 9, | ||
Processor_Architecture_UNKNOWN = 0xFFFF | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Common/src/Interop/Windows/mincore/Interop.VersionHelpers.cs
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,76 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class mincore | ||
{ | ||
[DllImport(Libraries.Kernel32)] | ||
private static extern bool VerifyVersionInfo([In] ref OSVERSIONINFOEX lpVersionInfo, uint dwTypeMask, ulong dwlConditionMask); | ||
|
||
[DllImport(Libraries.Kernel32)] | ||
private static extern ulong VerSetConditionMask(ulong dwlConditionMask, uint dwTypeBitMask, byte dwConditionMask); | ||
|
||
private const int VER_MAJORVERSION = 2; | ||
private const int VER_MINORVERSION = 1; | ||
private const int VER_GREATER_EQUAL = 3; | ||
|
||
private static bool IsVersionOrGreater(uint major, uint minor) | ||
{ | ||
OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); | ||
osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi); | ||
osvi.dwMajorVersion = major; | ||
osvi.dwMinorVersion = minor; | ||
ulong dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), | ||
VER_MINORVERSION, VER_GREATER_EQUAL); | ||
uint dwTypeMask = VER_MAJORVERSION | VER_MINORVERSION; | ||
|
||
return VerifyVersionInfo(ref osvi, dwTypeMask, dwlConditionMask); | ||
} | ||
|
||
internal static bool IsWindowsXPOrGreater() | ||
{ | ||
return IsVersionOrGreater(5, 1); | ||
} | ||
|
||
internal static bool IsWindowsVistaOrGreater() | ||
{ | ||
return IsVersionOrGreater(6, 0); | ||
} | ||
|
||
internal static bool IsWindows7OrGreater() | ||
{ | ||
return IsVersionOrGreater(6, 1); | ||
} | ||
|
||
internal static bool IsWindows8OrGreater() | ||
{ | ||
return IsVersionOrGreater(6, 2); | ||
} | ||
|
||
internal static bool IsWindows8Point1OrGreater() | ||
{ | ||
return IsVersionOrGreater(6, 3); | ||
} | ||
|
||
internal static bool IsWindows10OrGreater() | ||
{ | ||
return IsVersionOrGreater(10, 0); | ||
} | ||
|
||
internal static bool IsWindowsServer() | ||
{ | ||
OSVERSIONINFOEX osvi = new OSVERSIONINFOEX(); | ||
osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi); | ||
osvi.wProductType = 1; // VER_NT_WORKSTATION | ||
|
||
// 80 - VER_PRODUCT_TYPE, 1 - VER_EQUAL | ||
ulong dwlConditionMask = VerSetConditionMask(0, dwTypeBitMask: 80, dwConditionMask: 1); | ||
return !VerifyVersionInfo(ref osvi, 80, dwlConditionMask); | ||
} | ||
} | ||
} |
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
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,39 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include "pal_runtimeinformation.h" | ||
#include "pal_types.h" | ||
#include <stdio.h> | ||
#include <sys/utsname.h> | ||
|
||
extern "C" int32_t GetUnixVersion(char* version, int* capacity) | ||
{ | ||
struct utsname _utsname; | ||
if (uname(&_utsname) != -1) | ||
{ | ||
int r = snprintf(version, static_cast<size_t>(*capacity), "%s %s %s", _utsname.sysname, _utsname.release, _utsname.version); | ||
if (r > *capacity) | ||
{ | ||
*capacity = r + 1; | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/* Returns an int representing the OS Architecture: | ||
0 - x86 | ||
1 - x64 | ||
2 - ARM */ | ||
extern "C" int32_t GetUnixArchitecture() | ||
{ | ||
#if defined(ARM) | ||
return ARCH_ARM; | ||
#elif defined(X64) | ||
return ARCH_X64; | ||
#elif defined(X86) | ||
return ARCH_X86; | ||
#error Unidentified Architecture | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#pragma once | ||
|
||
#include "pal_types.h" | ||
|
||
extern "C" int32_t GetUnixVersion(char* version, int* capacity); | ||
|
||
extern "C" int32_t GetUnixArchitecture(); | ||
|
||
enum | ||
{ | ||
ARCH_X86, | ||
ARCH_X64, | ||
ARCH_ARM | ||
}; |
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
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
12 changes: 12 additions & 0 deletions
12
src/System.Runtime.InteropServices.RuntimeInformation/src/Architecture.cs
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,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Runtime.InteropServices | ||
{ | ||
public enum Architecture | ||
{ | ||
x86, | ||
x64, | ||
Arm | ||
} | ||
} |
Oops, something went wrong.