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