From 66e69c4c25ef2554b3ed44378dc5fc465ab47781 Mon Sep 17 00:00:00 2001 From: Phyxion <7643972+Phyxion@users.noreply.github.com> Date: Sat, 10 Feb 2018 14:01:52 +0100 Subject: [PATCH] Pr/30 (#32) * Unified operating system type detection * Revert "Unified operating system type detection" This reverts commit 3edf0edfceaca259e041e23b9930d78531366fd5. * Introduced OperatingSystem.IsLinux instead of constant check on Os version. * Misc --- Hardware/CPU/AMD10CPU.cs | 4 +-- Hardware/HDD/HarddriveGroup.cs | 4 +-- Hardware/Heatmaster/HeatmasterGroup.cs | 4 +-- Hardware/Mainboard/Mainboard.cs | 4 +-- Hardware/Opcode.cs | 11 ++++---- Hardware/OperatingSystem.cs | 38 ------------------------- Hardware/RAM/RAMGroup.cs | 4 +-- Hardware/Ring0.cs | 6 ++-- Hardware/SMBIOS.cs | 8 ++++-- Hardware/ThreadAffinity.cs | 31 ++++++++++---------- OpenHardwareMonitorLib.csproj | 2 +- Software/OperatingSystem.cs | 39 ++++++++++++++++++++++++++ 12 files changed, 76 insertions(+), 79 deletions(-) delete mode 100644 Hardware/OperatingSystem.cs create mode 100644 Software/OperatingSystem.cs diff --git a/Hardware/CPU/AMD10CPU.cs b/Hardware/CPU/AMD10CPU.cs index 7d919db..c5eae3d 100644 --- a/Hardware/CPU/AMD10CPU.cs +++ b/Hardware/CPU/AMD10CPU.cs @@ -144,8 +144,8 @@ namespace OpenHardwareMonitor.Hardware.CPU { // the file reader for lm-sensors support on Linux temperatureStream = null; - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) { + + if (Software.OperatingSystem.IsLinux) { string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/"); foreach (string path in devicePaths) { string name = null; diff --git a/Hardware/HDD/HarddriveGroup.cs b/Hardware/HDD/HarddriveGroup.cs index 65ea0dd..34cfb13 100644 --- a/Hardware/HDD/HarddriveGroup.cs +++ b/Hardware/HDD/HarddriveGroup.cs @@ -24,8 +24,8 @@ namespace OpenHardwareMonitor.Hardware.HDD { new List(); public HarddriveGroup(ISettings settings) { - int p = (int)Environment.OSVersion.Platform; - if (p == 4 || p == 128) return; + + if (Software.OperatingSystem.IsLinux) return; ISmart smart = new WindowsSmart(); diff --git a/Hardware/Heatmaster/HeatmasterGroup.cs b/Hardware/Heatmaster/HeatmasterGroup.cs index f65a015..7174d19 100644 --- a/Hardware/Heatmaster/HeatmasterGroup.cs +++ b/Hardware/Heatmaster/HeatmasterGroup.cs @@ -67,9 +67,7 @@ namespace OpenHardwareMonitor.Hardware.Heatmaster { public HeatmasterGroup(ISettings settings) { // No implementation for Heatmaster on Unix systems - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) - return; + if (Software.OperatingSystem.IsLinux) return; string[] portNames = GetRegistryPortNames(); for (int i = 0; i < portNames.Length; i++) { diff --git a/Hardware/Mainboard/Mainboard.cs b/Hardware/Mainboard/Mainboard.cs index 9912636..377371c 100644 --- a/Hardware/Mainboard/Mainboard.cs +++ b/Hardware/Mainboard/Mainboard.cs @@ -50,8 +50,8 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { new Identifier(Identifier, "name").ToString(), name); ISuperIO[] superIO; - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) { + + if (Software.OperatingSystem.IsLinux) { this.lmSensors = new LMSensors(); superIO = lmSensors.SuperIO; } else { diff --git a/Hardware/Opcode.cs b/Hardware/Opcode.cs index 178d13c..305698d 100644 --- a/Hardware/Opcode.cs +++ b/Hardware/Opcode.cs @@ -19,8 +19,7 @@ namespace OpenHardwareMonitor.Hardware { private static ulong size; public static void Open() { - int p = (int)Environment.OSVersion.Platform; - + byte[] rdtscCode; byte[] cpuidCode; if (IntPtr.Size == 4) { @@ -29,7 +28,7 @@ namespace OpenHardwareMonitor.Hardware { } else { rdtscCode = RDTSC_64; - if ((p == 4) || (p == 128)) { // Unix + if (Software.OperatingSystem.IsLinux) { // Unix cpuidCode = CPUID_64_LINUX; } else { // Windows cpuidCode = CPUID_64_WINDOWS; @@ -38,7 +37,7 @@ namespace OpenHardwareMonitor.Hardware { size = (ulong)(rdtscCode.Length + cpuidCode.Length); - if ((p == 4) || (p == 128)) { // Unix + if (Software.OperatingSystem.IsLinux) { // Unix Assembly assembly = Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " + "PublicKeyToken=0738eb9f132ed756"); @@ -81,8 +80,8 @@ namespace OpenHardwareMonitor.Hardware { Rdtsc = null; Cpuid = null; - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) { // Unix + + if (Software.OperatingSystem.IsLinux) { // Unix Assembly assembly = Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " + "PublicKeyToken=0738eb9f132ed756"); diff --git a/Hardware/OperatingSystem.cs b/Hardware/OperatingSystem.cs deleted file mode 100644 index 6cc2b34..0000000 --- a/Hardware/OperatingSystem.cs +++ /dev/null @@ -1,38 +0,0 @@ -/* - - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. - - Copyright (C) 2012 Michael Möller - -*/ - -using System; -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace OpenHardwareMonitor.Hardware { - internal static class OperatingSystem { - - public static bool Is64BitOperatingSystem() { - if (IntPtr.Size == 8) - return true; - - try { - bool wow64Process; - bool result = IsWow64Process( - Process.GetCurrentProcess().Handle, out wow64Process); - - return result && wow64Process; - } catch (EntryPointNotFoundException) { - return false; - } - } - - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool IsWow64Process(IntPtr hProcess, - out bool wow64Process); - } -} diff --git a/Hardware/RAM/RAMGroup.cs b/Hardware/RAM/RAMGroup.cs index 58fc4b4..882e86f 100644 --- a/Hardware/RAM/RAMGroup.cs +++ b/Hardware/RAM/RAMGroup.cs @@ -18,8 +18,8 @@ namespace OpenHardwareMonitor.Hardware.RAM { public RAMGroup(SMBIOS smbios, ISettings settings) { // No implementation for RAM on Unix systems - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) { + + if (Software.OperatingSystem.IsLinux) { hardware = new Hardware[0]; return; } diff --git a/Hardware/Ring0.cs b/Hardware/Ring0.cs index c8524ff..caefeb8 100644 --- a/Hardware/Ring0.cs +++ b/Hardware/Ring0.cs @@ -80,7 +80,7 @@ namespace OpenHardwareMonitor.Hardware { private static bool ExtractDriver(string fileName) { string resourceName = "OpenHardwareMonitor.Hardware." + - (OperatingSystem.Is64BitOperatingSystem() ? "WinRing0x64.sys" : + (Software.OperatingSystem.Is64Bit ? "WinRing0x64.sys" : "WinRing0.sys"); string[] names = GetAssembly().GetManifestResourceNames(); @@ -129,9 +129,7 @@ namespace OpenHardwareMonitor.Hardware { public static void Open() { // no implementation for unix systems - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) - return; + if (Software.OperatingSystem.IsLinux) return; if (driver != null) return; diff --git a/Hardware/SMBIOS.cs b/Hardware/SMBIOS.cs index 36f799e..c38d92c 100644 --- a/Hardware/SMBIOS.cs +++ b/Hardware/SMBIOS.cs @@ -41,9 +41,11 @@ namespace OpenHardwareMonitor.Hardware { } } - public SMBIOS() { - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) { + public SMBIOS() + { + + if (Software.OperatingSystem.IsLinux) + { this.raw = null; this.table = null; diff --git a/Hardware/ThreadAffinity.cs b/Hardware/ThreadAffinity.cs index a4b8560..58bed84 100755 --- a/Hardware/ThreadAffinity.cs +++ b/Hardware/ThreadAffinity.cs @@ -19,26 +19,25 @@ namespace OpenHardwareMonitor.Hardware { if (mask == 0) return 0; - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) { // Unix - ulong result = 0; - if (NativeMethods.sched_getaffinity(0, (IntPtr)Marshal.SizeOf(result), - ref result) != 0) - return 0; - if (NativeMethods.sched_setaffinity(0, (IntPtr)Marshal.SizeOf(mask), - ref mask) != 0) - return 0; - return result; - } else { // Windows + if (Software.OperatingSystem.IsLinux) + { // Unix + ulong result = 0; + if (NativeMethods.sched_getaffinity(0, (IntPtr)Marshal.SizeOf(result), + ref result) != 0) + return 0; + if (NativeMethods.sched_setaffinity(0, (IntPtr)Marshal.SizeOf(mask), + ref mask) != 0) + return 0; + return result; + } // Windows UIntPtr uIntPtrMask; try { - uIntPtrMask = (UIntPtr)mask; + uIntPtrMask = (UIntPtr)mask; } catch (OverflowException) { - throw new ArgumentOutOfRangeException("mask"); - } + throw new ArgumentOutOfRangeException("mask"); + } return (ulong)NativeMethods.SetThreadAffinityMask( - NativeMethods.GetCurrentThread(), uIntPtrMask); - } + NativeMethods.GetCurrentThread(), uIntPtrMask); } private static class NativeMethods { diff --git a/OpenHardwareMonitorLib.csproj b/OpenHardwareMonitorLib.csproj index 89d3e8b..295cb5b 100644 --- a/OpenHardwareMonitorLib.csproj +++ b/OpenHardwareMonitorLib.csproj @@ -65,7 +65,6 @@ - @@ -114,6 +113,7 @@ + diff --git a/Software/OperatingSystem.cs b/Software/OperatingSystem.cs new file mode 100644 index 0000000..a54967f --- /dev/null +++ b/Software/OperatingSystem.cs @@ -0,0 +1,39 @@ +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace OpenHardwareMonitor.Software +{ + public static class OperatingSystem + { + static OperatingSystem() + { + // The operating system doesn't change during execution so let's query it just one time. + var platform = Environment.OSVersion.Platform; + IsLinux = platform == PlatformID.Unix || platform == PlatformID.MacOSX; + + + if (IntPtr.Size == 8) + Is64Bit = true; + + try + { + var result = IsWow64Process(Process.GetCurrentProcess().Handle, out bool wow64Process); + + Is64Bit = result && wow64Process; + } + catch (EntryPointNotFoundException) + { + Is64Bit = false; + } + } + + public static bool Is64Bit { get; } + + public static bool IsLinux { get; } + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process); + } +} \ No newline at end of file