Hardware/RAM/GenericRAM.cs
changeset 372 2de3a3e5f0b5
parent 370 8e4dedc41924
child 377 6022d558ef7d
     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  }