Changed the RAM implementation (removed Microsoft.VisualBasic dependency which is not implemented in Mono) and added code to prevent RAM hardware from loading on Linux.
authormoel.mich
Tue, 24 Jul 2012 16:45:48 +0000
changeset 3722de3a3e5f0b5
parent 371 c1a0d321e646
child 373 3b8443edb0e6
Changed the RAM implementation (removed Microsoft.VisualBasic dependency which is not implemented in Mono) and added code to prevent RAM hardware from loading on Linux.
Hardware/RAM/GenericRAM.cs
Hardware/RAM/RAMGroup.cs
OpenHardwareMonitorLib.csproj
     1.1 --- a/Hardware/RAM/GenericRAM.cs	Tue Jul 24 16:04:30 2012 +0000
     1.2 +++ b/Hardware/RAM/GenericRAM.cs	Tue Jul 24 16:45:48 2012 +0000
     1.3 @@ -8,7 +8,7 @@
     1.4  	
     1.5  */
     1.6  
     1.7 -using Microsoft.VisualBasic.Devices;
     1.8 +using System.Runtime.InteropServices;
     1.9  
    1.10  namespace OpenHardwareMonitor.Hardware.RAM {
    1.11    internal class GenericRAM : Hardware {
    1.12 @@ -16,16 +16,14 @@
    1.13      private Sensor loadSensor;
    1.14      private Sensor availableMemory;
    1.15  
    1.16 -    private ComputerInfo computerInfo;
    1.17 -
    1.18      public GenericRAM(string name, ISettings settings)
    1.19        : base(name, new Identifier("ram"), settings)
    1.20      {   
    1.21 -      computerInfo = new ComputerInfo();
    1.22        loadSensor = new Sensor("Memory", 0, SensorType.Load, this, settings);
    1.23        ActivateSensor(loadSensor);
    1.24  
    1.25 -      availableMemory = new Sensor("Available Memory", 0, SensorType.Data, this, settings);
    1.26 +      availableMemory = new Sensor("Available Memory", 0, SensorType.Data, this, 
    1.27 +        settings);
    1.28        ActivateSensor(availableMemory);
    1.29      }
    1.30  
    1.31 @@ -36,12 +34,39 @@
    1.32      }
    1.33  
    1.34      public override void Update() {
    1.35 -      loadSensor.Value = 100.0f - 
    1.36 -        (100.0f * computerInfo.AvailablePhysicalMemory) / 
    1.37 -        computerInfo.TotalPhysicalMemory;
    1.38 +      NativeMethods.MemoryStatusEx status = new NativeMethods.MemoryStatusEx();
    1.39 +      status.Length = checked((uint)Marshal.SizeOf(
    1.40 +          typeof(NativeMethods.MemoryStatusEx)));
    1.41  
    1.42 -      availableMemory.Value = (float)computerInfo.AvailablePhysicalMemory /
    1.43 +      if (!NativeMethods.GlobalMemoryStatusEx(ref status))
    1.44 +        return;
    1.45 +
    1.46 +      loadSensor.Value = 100.0f -
    1.47 +        (100.0f * status.AvailablePhysicalMemory) /
    1.48 +        status.TotalPhysicalMemory;
    1.49 +
    1.50 +      availableMemory.Value = (float)status.AvailablePhysicalMemory /
    1.51          (1024 * 1024 * 1024);
    1.52      }
    1.53 +
    1.54 +    private class NativeMethods {
    1.55 +      [StructLayout(LayoutKind.Sequential)]
    1.56 +      public struct MemoryStatusEx {
    1.57 +        public uint Length;
    1.58 +        public uint MemoryLoad;
    1.59 +        public ulong TotalPhysicalMemory;
    1.60 +        public ulong AvailablePhysicalMemory;
    1.61 +        public ulong TotalPageFile;
    1.62 +        public ulong AvailPageFile;
    1.63 +        public ulong TotalVirtual;
    1.64 +        public ulong AvailVirtual;
    1.65 +        public ulong AvailExtendedVirtual;
    1.66 +      }
    1.67 +
    1.68 +      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    1.69 +      [return: MarshalAs(UnmanagedType.Bool)]
    1.70 +      internal static extern bool GlobalMemoryStatusEx(
    1.71 +        ref NativeMethods.MemoryStatusEx buffer);
    1.72 +    }
    1.73    }
    1.74  }
     2.1 --- a/Hardware/RAM/RAMGroup.cs	Tue Jul 24 16:04:30 2012 +0000
     2.2 +++ b/Hardware/RAM/RAMGroup.cs	Tue Jul 24 16:45:48 2012 +0000
     2.3 @@ -8,12 +8,22 @@
     2.4  	
     2.5  */
     2.6  
     2.7 +using System;
     2.8 +
     2.9  namespace OpenHardwareMonitor.Hardware.RAM {
    2.10    internal class RAMGroup : IGroup {
    2.11  
    2.12      private IHardware[] hardware;
    2.13  
    2.14      public RAMGroup(SMBIOS smbios, ISettings settings) {
    2.15 +
    2.16 +      // No implementation for RAM on Unix systems
    2.17 +      int p = (int)Environment.OSVersion.Platform;
    2.18 +      if ((p == 4) || (p == 128)) {
    2.19 +        hardware = new IHardware[0];
    2.20 +        return;
    2.21 +      }
    2.22 +
    2.23        string name;
    2.24        if (smbios.MemoryDevices.Length > 0) {
    2.25          name = smbios.MemoryDevices[0].ManufacturerName + " " + 
     3.1 --- a/OpenHardwareMonitorLib.csproj	Tue Jul 24 16:04:30 2012 +0000
     3.2 +++ b/OpenHardwareMonitorLib.csproj	Tue Jul 24 16:45:48 2012 +0000
     3.3 @@ -52,7 +52,6 @@
     3.4      <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
     3.5    </PropertyGroup>
     3.6    <ItemGroup>
     3.7 -    <Reference Include="Microsoft.VisualBasic" />
     3.8      <Reference Include="System" />
     3.9      <Reference Include="System.Management" />
    3.10    </ItemGroup>