Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add new environment apis proposed to
Browse files Browse the repository at this point in the history
System.Runtime.InteropServices.RuntimeInformation.
  • Loading branch information
Lakshmi Priya Sekar committed Nov 5, 2015
1 parent 6c106c3 commit 97d52c2
Show file tree
Hide file tree
Showing 27 changed files with 1,663 additions and 42 deletions.
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
}
}
}
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();
}
}
}
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);
}
}
15 changes: 0 additions & 15 deletions src/Common/src/Interop/Windows/mincore/Interop.GetSystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,5 @@ internal partial class mincore
{
[DllImport(Libraries.SystemInfo)]
internal extern static void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);

[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
internal int dwOemId;
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;
}
}
}
28 changes: 28 additions & 0 deletions src/Common/src/Interop/Windows/mincore/Interop.OSVERSIONINFOEX.cs
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 src/Common/src/Interop/Windows/mincore/Interop.SYSTEM_INFO.cs
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 src/Common/src/Interop/Windows/mincore/Interop.VersionHelpers.cs
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);
}
}
}
2 changes: 2 additions & 0 deletions src/Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ endif ()

if (CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
add_definitions(-DBIT64=1)
add_definitions(-DX64=1)
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
add_definitions(-DBIT32=1)
add_definitions(-DARM=1)
# Because we don't use CMAKE_C_COMPILER/CMAKE_CXX_COMPILER to use clang
# we have to set the triple by adding a compiler argument
add_compile_options(-target armv7-linux-gnueabihf)
Expand Down
1 change: 1 addition & 0 deletions src/Native/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(NATIVE_SOURCES
pal_networking.cpp
pal_networkstatistics.cpp
pal_process.cpp
pal_runtimeinformation.cpp
pal_string.cpp
pal_time.cpp
pal_uid.cpp
Expand Down
39 changes: 39 additions & 0 deletions src/Native/System.Native/pal_runtimeinformation.cpp
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
}
17 changes: 17 additions & 0 deletions src/Native/System.Native/pal_runtimeinformation.h
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
};
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.GetSystemInfo.cs">
<Link>Common\Interop\Windows\Interop.GetSystemInfo.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.SYSTEM_INFO.cs">
<Link>Common\Interop\Windows\Interop.SYSTEM_INFO.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.GlobalMemoryStatusEx.cs">
<Link>Common\Interop\Windows\Interop.GlobalMemoryStatusEx.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22609.0
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.InteropServices.RuntimeInformation", "src\System.Runtime.InteropServices.RuntimeInformation.csproj", "{F9DF2357-81B4-4317-908E-512DA9395583}"
EndProject
Expand All @@ -21,6 +21,7 @@ Global
Windows_Release|Any CPU = Windows_Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F9DF2357-81B4-4317-908E-512DA9395583}.Debug|Any CPU.ActiveCfg = Windows_Debug|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.FreeBSD_Debug|Any CPU.ActiveCfg = FreeBSD_Debug|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.FreeBSD_Debug|Any CPU.Build.0 = FreeBSD_Debug|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.FreeBSD_Release|Any CPU.ActiveCfg = FreeBSD_Release|Any CPU
Expand All @@ -33,10 +34,12 @@ Global
{F9DF2357-81B4-4317-908E-512DA9395583}.OSX_Debug|Any CPU.Build.0 = OSX_Debug|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.OSX_Release|Any CPU.ActiveCfg = OSX_Release|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.OSX_Release|Any CPU.Build.0 = OSX_Release|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.Release|Any CPU.ActiveCfg = Windows_Release|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.Windows_Debug|Any CPU.ActiveCfg = Windows_Debug|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.Windows_Debug|Any CPU.Build.0 = Windows_Debug|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.Windows_Release|Any CPU.ActiveCfg = Windows_Release|Any CPU
{F9DF2357-81B4-4317-908E-512DA9395583}.Windows_Release|Any CPU.Build.0 = Windows_Release|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.FreeBSD_Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.FreeBSD_Debug|Any CPU.Build.0 = Debug|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.FreeBSD_Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -49,6 +52,7 @@ Global
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.OSX_Debug|Any CPU.Build.0 = Debug|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.OSX_Release|Any CPU.ActiveCfg = Release|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.OSX_Release|Any CPU.Build.0 = Release|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.Windows_Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.Windows_Debug|Any CPU.Build.0 = Debug|Any CPU
{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}.Windows_Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ public partial struct OSPlatform : System.IEquatable<System.Runtime.InteropServi
public static bool operator !=(System.Runtime.InteropServices.OSPlatform left, System.Runtime.InteropServices.OSPlatform right) { return default(bool); }
public override string ToString() { return default(string); }
}

public enum Architecture
{
x86,
x64,
Arm
}

public static partial class RuntimeInformation
{
public static Architecture ProcessArchitecture { get { return default(Architecture); } }
public static Architecture OSArchitecture { get { return default(Architecture); } }
public static string OSDescription { get { return default(string); } }
public static string FrameworkDescription { get { return default(string); } }
public static bool IsOSPlatform(System.Runtime.InteropServices.OSPlatform osPlatform) { return default(bool); }
}
}
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
}
}
Loading

0 comments on commit 97d52c2

Please sign in to comment.