Hardware/RAM/GenericRAM.cs
author moel.mich
Tue, 24 Jul 2012 16:45:48 +0000
changeset 372 2de3a3e5f0b5
parent 370 8e4dedc41924
child 377 6022d558ef7d
permissions -rw-r--r--
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.
moel@370
     1
/*
moel@370
     2
 
moel@370
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@370
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@370
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@370
     6
 
moel@370
     7
  Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@370
     8
	
moel@370
     9
*/
moel@370
    10
moel@372
    11
using System.Runtime.InteropServices;
moel@370
    12
moel@370
    13
namespace OpenHardwareMonitor.Hardware.RAM {
moel@370
    14
  internal class GenericRAM : Hardware {
moel@370
    15
moel@370
    16
    private Sensor loadSensor;
moel@370
    17
    private Sensor availableMemory;
moel@370
    18
moel@370
    19
    public GenericRAM(string name, ISettings settings)
moel@370
    20
      : base(name, new Identifier("ram"), settings)
moel@370
    21
    {   
moel@370
    22
      loadSensor = new Sensor("Memory", 0, SensorType.Load, this, settings);
moel@370
    23
      ActivateSensor(loadSensor);
moel@370
    24
moel@372
    25
      availableMemory = new Sensor("Available Memory", 0, SensorType.Data, this, 
moel@372
    26
        settings);
moel@370
    27
      ActivateSensor(availableMemory);
moel@370
    28
    }
moel@370
    29
moel@370
    30
    public override HardwareType HardwareType {
moel@370
    31
      get {
moel@370
    32
        return HardwareType.RAM;
moel@370
    33
      }
moel@370
    34
    }
moel@370
    35
moel@370
    36
    public override void Update() {
moel@372
    37
      NativeMethods.MemoryStatusEx status = new NativeMethods.MemoryStatusEx();
moel@372
    38
      status.Length = checked((uint)Marshal.SizeOf(
moel@372
    39
          typeof(NativeMethods.MemoryStatusEx)));
moel@370
    40
moel@372
    41
      if (!NativeMethods.GlobalMemoryStatusEx(ref status))
moel@372
    42
        return;
moel@372
    43
moel@372
    44
      loadSensor.Value = 100.0f -
moel@372
    45
        (100.0f * status.AvailablePhysicalMemory) /
moel@372
    46
        status.TotalPhysicalMemory;
moel@372
    47
moel@372
    48
      availableMemory.Value = (float)status.AvailablePhysicalMemory /
moel@370
    49
        (1024 * 1024 * 1024);
moel@370
    50
    }
moel@372
    51
moel@372
    52
    private class NativeMethods {
moel@372
    53
      [StructLayout(LayoutKind.Sequential)]
moel@372
    54
      public struct MemoryStatusEx {
moel@372
    55
        public uint Length;
moel@372
    56
        public uint MemoryLoad;
moel@372
    57
        public ulong TotalPhysicalMemory;
moel@372
    58
        public ulong AvailablePhysicalMemory;
moel@372
    59
        public ulong TotalPageFile;
moel@372
    60
        public ulong AvailPageFile;
moel@372
    61
        public ulong TotalVirtual;
moel@372
    62
        public ulong AvailVirtual;
moel@372
    63
        public ulong AvailExtendedVirtual;
moel@372
    64
      }
moel@372
    65
moel@372
    66
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
moel@372
    67
      [return: MarshalAs(UnmanagedType.Bool)]
moel@372
    68
      internal static extern bool GlobalMemoryStatusEx(
moel@372
    69
        ref NativeMethods.MemoryStatusEx buffer);
moel@372
    70
    }
moel@370
    71
  }
moel@370
    72
}